自訂顏色#
介紹#
VEX EXP 支援使用自訂顏色進行繪圖、文字和 LED 控制。自訂顏色可以使用 RGB 值、十六進位代碼、HSV 值或預先定義常數來建立。自訂顏色包含用於建立和更新顏色物件的方法。以下是可用方法的清單:
建構函式 – 建立一個新的 Color 物件。
Color(value)– 接受預先定義的常數、十六進位字串(例如“#FFF700”)或十六進位整數(例如0xFFF700)。Color(r, g, b)– 使用紅色、綠色和藍色值 (0–255) 建立顏色。
變異器 – 更新現有的 Color 物件。
建立自訂顏色#
若要使用自訂顏色,必須先使用下列建構函數之一建立 Color 物件:
Hexadecimal Integer#
使用六位十六進制整數建立顏色。
Usage:
Color(value)
範圍 |
描述 |
|---|---|
|
十六進位格式的六位整數(例如, |
# Construct a yellow Color "yellow" using a
# hexadecimal value
yellow = Color(0xFFF700)
brain.screen.set_pen_color(yellow)
brain.screen.print("My Yellow")
RGB#
使用單獨的紅色、綠色和藍色值來建立顏色。
Usage:
Color(r, g, b)
範圍 |
描述 |
|---|---|
|
一個介於 0 到 255 之間的整數,表示紅色分量。 |
|
一個介於 0 到 255 之間的整數,表示綠色分量。 |
|
一個介於 0 到 255 之間的整數,表示藍色分量。 |
# Construct a yellow Color "yellow" using
# RGB values
yellow = Color(255, 247, 0)
brain.screen.set_pen_color(yellow)
brain.screen.print("My Yellow")
Web Color#
使用網頁顏色字串(十六進位代碼)建立顏色。
Usage:
Color(value)
範圍 |
描述 |
|---|---|
|
網頁顏色以字串(十六進位代碼)表示(例如, |
# Construct a yellow Color "yellow" using a
# web string
yellow = Color("#FFF700")
brain.screen.set_pen_color(yellow)
brain.screen.print("My Yellow")
Predefined Color#
使用預先定義的 Color 常數建立顏色。
Usage:
Color(value)
範圍 |
描述 |
|---|---|
|
內建顏色常數: |
# Construct a yellow Color "yellow" using a
# predefined Color constant
yellow = Color.YELLOW
brain.screen.set_pen_color(yellow)
brain.screen.print("My Yellow")
變異體#
這些方法用於更新現有 Color 物件的顏色。若要使用它們,請在建立變數時將其賦值為 Color ,然後呼叫該變數的方法來變更其顏色。
rgb#
rgb 使用 RGB 值更新現有 Color 物件的顏色。
Usage:
variable_name.rgb(r, g, b)
參數 |
描述 |
|---|---|
|
一個介於 0 到 255 之間的整數,表示顏色中的紅色分量。 |
|
一個介於 0 到 255 之間的整數,表示顏色中的綠色分量。 |
|
一個介於 0 到 255 之間的整數,表示顏色中的藍色分量。 |
# Create a custom teal color and set it as the pen color
brain_color = Color(50, 200, 180)
brain.screen.set_pen_color(brain_color)
# Draw a rectangle with the teal outline
brain.screen.draw_rectangle(5, 10, 80, 40)
# Draw another rectangle with a magenta outline
brain_color.rgb(170, 40, 150)
brain.screen.set_pen_color(brain_color)
brain.screen.draw_rectangle(5, 60, 80, 40)
hsv#
hsv 使用 HSV 值更新現有 Color 物件的顏色。
注意: hsv 只能用於更改已建立的 Color 物件。它不能用於創建新的 Color 物件。
Usage:
variable_name.hsv(h, s, v)
參數 |
描述 |
|---|---|
|
一個介於 0 到 359 之間的整數,表示顏色的色調。 |
|
一個介於 0.0 到 1.0 之間的浮點數,表示顏色的飽和度。 |
|
一個介於 0.0 到 1.0 之間的浮點數,表示顏色的亮度。 |
# Create a custom teal color using RGB (HSV can't be used)
brain_color = Color(50, 200, 180)
brain.screen.set_pen_color(brain_color)
# Draw a rectangle with the teal outline
brain.screen.draw_rectangle(5, 10, 80, 40)
# Draw another rectangle with a magenta outline
brain_color.hsv(300, 0.75, 0.78)
brain.screen.set_pen_color(brain_color)
brain.screen.draw_rectangle(5, 60, 80, 40)
web#
web 使用 web 顏色(十六進位代碼)更新現有 Color 物件的顏色。
Usage:
variable_name.web(value)
參數 |
描述 |
|---|---|
|
網頁顏色以字串(十六進位代碼)表示(例如, |
# Create a custom teal color and set it as the pen color
brain_color = Color("#32C8B6")
brain.screen.set_pen_color(brain_color)
# Draw a rectangle with the teal outline
brain.screen.draw_rectangle(5, 10, 80, 40)
# Draw another rectangle with the new magenta outline
brain_color.web("#AA2896")
brain.screen.set_pen_color(brain_color)
brain.screen.draw_rectangle(5, 60, 80, 40)