自訂顏色#

介紹#

VEX AIM 編程機器人支援使用自訂顏色進行繪圖、顯示和 LED 控制。自訂顏色可以使用 RGB 值、十六進位代碼或預先定義常數來建立。自訂顏色功能包含用於建立和更新顏色物件的方法。

以下是所有方法的清單:

建構函式 – 建立一個新的 Color 物件。

  • Color(value) – 接受預先定義的常數、十六進位字串或十六進位整數。

  • Color(r, g, b) – 使用紅色、綠色和藍色值建立顏色。

變異器 – 更新現有的 Color 物件。

  • rgb – 使用新的 RGB 值更新顏色。

  • hsv – 使用色調、飽和度和亮度更新顏色。

  • web – 使用 web 十六進位顏色字串更新顏色。

建立自訂顏色#

若要使用自訂顏色,必須先使用下列建構函數之一建立 Color 物件:

Hexadecimal Integer#

使用六位十六進制整數建立顏色。

用法:
Color(value)

範圍

描述

value

十六進位格式的六位整數(例如, 0xFFF700 表示黃色)。

# Construct a yellow Color "yellow" using a
# hexadecimal value
yellow = Color(0xFFF700)

robot.screen.set_pen_color(yellow)
robot.screen.print("My Yellow")

RGB#

使用單獨的紅色、綠色和藍色值來建立顏色。

用法:
Color(r, g, b)

範圍

描述

r

一個介於 0 到 255 之間的整數,表示紅色分量。

g

一個介於 0 到 255 之間的整數,表示綠色分量。

b

一個介於 0 到 255 之間的整數,表示藍色分量。

# Construct a yellow Color "yellow" using
# RGB values
yellow = Color(255, 247, 0)

robot.screen.set_pen_color(yellow)
robot.screen.print("My Yellow")

Web Color#

使用網頁顏色字串(十六進位代碼)建立顏色。

用法:
Color(value)

範圍

描述

value

網頁顏色以字串(十六進位代碼)表示(例如, “#FFF700”)。

# Construct a yellow Color "yellow" using a
# web string
yellow = Color("#FFF700")

robot.screen.set_pen_color(yellow)
robot.screen.print("My Yellow")

Predefined Color#

使用預先定義的 Color 常數建立顏色。

用法:
Color(value)

範圍

描述

value

内置颜色常数:

  • BLACK
  • BLUE
  • CYAN
  • GREEN
  • ORANGE
  • PURPLE
  • RED
  • TRANSPARENT
  • WHITE
  • YELLOW
# Construct a yellow Color "yellow" using a
# predefined Color constant
yellow = Color(YELLOW)

robot.screen.set_pen_color(yellow)
robot.screen.print("My Yellow")

突變體#

這些方法用於更新現有 Color 物件的顏色。若要使用它們,請在建立變數時將其賦值為 Color ,然後呼叫該變數的方法來變更其顏色。

rgb#

rgb 使用 RGB 值更新現有 Color 物件的顏色。

用法:
variable_name.rgb(r, g, b)

參數

描述

r

一個介於 0 到 255 之間的整數,表示顏色中的紅色分量。

g

一個介於 0 到 255 之間的整數,表示顏色中的綠色分量。

b

一個介於 0 到 255 之間的整數,表示顏色中的藍色分量。

# Create a custom teal color and set it as the pen color
robot_color = Color(50, 200, 180)
robot.screen.set_pen_color(robot_color)

# Draw a rectangle with the teal outline
robot.screen.draw_rectangle(30, 70, 80, 50)

# Draw another rectangle with a magenta outline
robot_color.rgb(170, 40, 150)
robot.screen.set_pen_color(robot_color)
robot.screen.draw_rectangle(130, 70, 80, 50)

hsv#

hsv 使用 HSV 值更新現有 Color 物件的顏色。

注意: hsv 只能用於更改已建立的 Color 物件。它不能用於創建新的 Color 物件。

用法:
variable_name.hsv(h, s, v)

參數

描述

h

一個介於 0 到 359 之間的整數,表示顏色的色調。

s

一個介於 0.0 到 1.0 之間的浮點數,表示顏色的飽和度。

v

一個介於 0.0 到 1.0 之間的浮點數,表示顏色的亮度。

# Create a custom teal color using RGB (HSV can't be used)
robot_color = Color(50, 200, 180)
robot.screen.set_pen_color(robot_color)

# Draw a rectangle with the teal outline
robot.screen.draw_rectangle(30, 70, 80, 50)

# Draw another rectangle with a magenta outline
robot_color.hsv(300, 0.75, 0.78)
robot.screen.set_pen_color(robot_color)
robot.screen.draw_rectangle(130, 70, 80, 50)

web#

web 使用 web 顏色(十六進位代碼)更新現有 Color 物件的顏色。

用法:
variable_name.web(value)

參數

描述

value

網頁顏色以字串(十六進位代碼)表示(例如, “#FFF700”)。

# Create a custom teal color and set it as the pen color
robot_color = Color("#32C8B6")
robot.screen.set_pen_color(robot_color)

# Draw a rectangle with the teal outline
robot.screen.draw_rectangle(30, 70, 80, 50)

# Draw another rectangle with the new magenta outline
robot_color.web("#AA2896")
robot.screen.set_pen_color(robot_color)
robot.screen.draw_rectangle(130, 70, 80, 50)