自定义颜色#
介绍#
VEX AIM 编码机器人支持使用自定义颜色进行绘图、显示及其 LED 灯。您可以使用 RGB 值、十六进制代码、HSV 值或预定义常量来创建自定义颜色。自定义颜色包含创建和更新颜色对象的方法。以下是可用方法的列表:
构造函数——创建一个新的Color
对象。
Color(value) – Accepts a predefined constant, hex string (e.g.
"#FFF700"
), or hex integer (e.g.0xFFF700
).Color(r, g, b) – Creates a color using red, green, and blue values (0–255).
Mutators – 更新现有的Color
对象。
创建自定义颜色#
要使用自定义颜色,您必须首先使用以下构造函数之一创建一个Color
对象:
Hexadecimal Integer#
使用六位十六进制整数创建颜色。
用法:
Color(value)
范围 |
描述 |
---|---|
|
十六进制格式的六位整数(例如,黄色为 |
# 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)
范围 |
描述 |
---|---|
|
表示红色成分的 0 至 255 之间的整数。 |
|
表示绿色成分的 0 至 255 之间的整数。 |
|
表示蓝色成分的 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#
使用 Web 颜色字符串(十六进制代码)创建颜色。
用法:
Color(value)
范围 |
描述 |
---|---|
|
以字符串(十六进制代码)表示的 Web 颜色(例如, |
# 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)
范围 |
描述 |
---|---|
|
内置颜色常量: |
# 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
对象后对其进行修改。
rgb#
rgb
使用 RGB 值更新现有 Color
对象的颜色。
用法:
rgb(r, g, b)
参数 |
描述 |
---|---|
|
0 至 255 之间的整数,表示颜色的红色成分。 |
|
0 至 255 之间的整数,表示颜色的绿色成分。 |
|
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
对象。
用法:
hsv(h, s, v)
参数 |
描述 |
---|---|
|
表示颜色色调的 0 至 360 之间的整数。 |
|
0.0 到 1.0 之间的浮点数,表示颜色的饱和度。 |
|
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
使用网页颜色(十六进制代码)更新现有 Color
对象的颜色。
用法:
web(value)
参数 |
描述 |
---|---|
|
用于更新现有颜色实例的字符串形式的 Web 颜色(十六进制代码)。 |
# 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)