自訂顏色#
介紹#
VEX AIR 無人機支援使用自訂顏色繪製和列印文字。自訂顏色可以使用 RGB 值或十六進位代碼建立。自訂顏色也包含在專案期間更新顏色物件的方法。
以下是可用建構函式的列表:
建構函式 – 建立一個新的 Color 物件。
Color(value)– 使用十六進位字串(例如"#FFF700")或十六進位整數(例如0xFFF700)建立顏色。Color(r, g, b)– 使用紅色、綠色和藍色值建立顏色。
Mutators – 更新現有的「Color」物件。
rgb– 使用 RGB 值將建立的顏色物件設定為新顏色。hsv– 使用色調、飽和度和亮度值將建立的顏色物件設定為新顏色。web– 使用 web 十六進位顏色字串(例如"#32C8B6")將建立的顏色物件設定為新顏色。
Getters – 傳回顏色物件的色調或飽和度值。
saturation– 傳回自訂顏色的飽和度值。hue– 傳回自訂顏色的色調值。
建立自訂顏色#
若要使用自訂顏色,必須先使用下列建構函數之一建立 Color 物件:
Hexadecimal String#
使用 Web 顏色字串(十六進位代碼)建立顏色。
用法:
Color(value)
範圍 |
描述 |
|---|---|
|
一個六位數的十六進位代碼字串(例如, |
# Construct a yellow Color "yellow" using a
# hexadecimal string
yellow = Color("#FFF700")
controller.screen.set_pen_color(yellow)
controller.screen.print("My Yellow")
Hexadecimal Integer#
使用六位十六進制整數建立顏色。
用法:
Color(value)
範圍 |
描述 |
|---|---|
|
十六進位格式的六位整數(例如, |
# Construct a yellow Color "yellow" using a
# hexadecimal integer
yellow = Color(0xFFF700)
controller.screen.set_pen_color(yellow)
controller.screen.print("My Yellow")
RGB#
使用單獨的紅色、綠色和藍色值來建立顏色。
用法:
Color(r, g, b)
範圍 |
描述 |
|---|---|
|
代表紅色成分的 0 到 255 之間的整數。 |
|
代表綠色成分的 0 到 255 之間的整數。 |
|
代表藍色成分的 0 到 255 之間的整數。 |
# Construct a yellow Color "yellow" using
# RGB values
yellow = Color(255, 247, 0)
controller.screen.set_pen_color(yellow)
controller.screen.print("My Yellow")
修改器#
這些方法用於更新現有 Color 物件的顏色。若要使用它們,請在建立變數時將其賦值為 Color ,然後呼叫該變數的方法來變更其顏色。
rgb#
rgb 使用 RGB 值更新現有 Color 物件的顏色。
用法:
variable_name.rgb(r, g, b)
參數 |
描述 |
|---|---|
|
0 到 255 之間的整數,表示顏色的紅色成分。 |
|
0 到 255 之間的整數,表示顏色的綠色成分。 |
|
0 到 255 之間的整數,表示顏色的藍色成分。 |
# Create a custom teal color
my_fill_color = Color(50, 200, 180)
# Draw a teal rectangle
controller.screen.draw_rectangle(x=30, y=70, width=80, height=50, color=my_fill_color)
# Draw a magenta rectangle
my_fill_color.rgb(170, 40, 150)
controller.screen.draw_rectangle(x=130, y=70, width=80, height=50, color=my_fill_color)
hsv#
hsv 使用 HSV 值更新現有 Color 物件的顏色。
注意: hsv 只能用於更改已建立的 Color 物件。它不能用於創建新的 Color 物件。
用法:
variable_name.hsv(h, s, v)
參數 |
描述 |
|---|---|
|
一個介於 0 到 359 之間的整數,表示顏色的色調。 |
|
0.0 到 1.0 之間的浮點數,表示顏色的飽和度。 |
|
0.0 到 1.0 之間的浮點數,表示顏色的亮度。 |
# Create a custom teal color
my_fill_color = Color(50, 200, 180)
# Draw a teal rectangle
controller.screen.draw_rectangle(x=30, y=70, width=80, height=50, color=my_fill_color)
# Draw a magenta rectangle
my_fill_color.hsv(300, 0.75, 0.78)
controller.screen.draw_rectangle(x=130, y=70, width=80, height=50, color=my_fill_color)
web#
web 使用 web 顏色(十六進位代碼)更新現有 Color 物件的顏色。
用法:
variable_name.web(value)
參數 |
描述 |
|---|---|
|
網頁顏色以字串(十六進位代碼)表示(例如, |
# Create a custom teal color
my_fill_color = Color("#32C8B6")
# Draw a teal rectangle
controller.screen.draw_rectangle(x=30, y=70, width=80, height=50, color=my_fill_color)
# Draw a magenta rectangle
my_fill_color.web("#AA2896")
controller.screen.draw_rectangle(x=130, y=70, width=80, height=50, color=my_fill_color)
吸氣劑#
saturation#
saturation 傳回自訂顏色的飽和度值,以 0 到 1 之間的浮點數表示。
用法:
my_color.saturation()
將 my_color 替換為自訂顏色物件的名稱。
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Create a custom teal color
color_teal = Color(50, 200, 180)
# Display the saturation value of teal
controller.screen.print(color_teal.saturation())
hue#
hue 傳回自訂顏色的色調值,該值是介於 0 到 359 之間的浮點數。
用法:
my_color.hue()
將 my_color 替換為自訂顏色物件的名稱。
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Create a custom teal color
color_teal = Color(50, 200, 180)
# Display the hue value of teal
controller.screen.print(color_teal.hue())