自定义颜色#
介绍#
VEX CTE 支持使用自定义颜色进行绘图、文本和 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.set_pen_color(yellow)
brain.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.set_pen_color(yellow)
brain.print("My Yellow")
Web Color#
使用网页颜色字符串(十六进制代码)创建颜色。
Usage:
Color(value)
范围 |
描述 |
|---|---|
|
网页颜色以字符串(十六进制代码)表示(例如, |
# Construct a yellow Color "yellow" using a
# web string
yellow = Color("#FFF700")
brain.set_pen_color(yellow)
brain.print("My Yellow")
Predefined Color#
使用预定义的 Color 常量创建颜色。
Usage:
Color(value)
范围 |
描述 |
|---|---|
|
内置颜色常量: |
# Construct a yellow Color "yellow" using a
# predefined Color constant
yellow = Color.YELLOW
brain.set_pen_color(yellow)
brain.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.set_pen_color(brain_color)
# Draw a rectangle with the teal outline
brain.draw_rectangle(5, 10, 80, 40)
# Draw another rectangle with a magenta outline
brain_color.rgb(170, 40, 150)
brain.set_pen_color(brain_color)
brain.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.set_pen_color(brain_color)
# Draw a rectangle with the teal outline
brain.draw_rectangle(5, 10, 80, 40)
# Draw another rectangle with a magenta outline
brain_color.hsv(300, 0.75, 0.78)
brain.set_pen_color(brain_color)
brain.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.set_pen_color(brain_color)
# Draw a rectangle with the teal outline
brain.draw_rectangle(5, 10, 80, 40)
# Draw another rectangle with the new magenta outline
brain_color.web("#AA2896")
brain.set_pen_color(brain_color)
brain.draw_rectangle(5, 60, 80, 40)