自定义颜色#

简介#

The VEX AIM Coding Robot supports the use of custom colors for drawing, display, and its LEDs. Custom colors can be created using RGB values, hex codes, or predefined constants. Custom Colors includes methods for both creating and updating color objects.

以下是所有功能的列表:

Constructors – Create a new Color object.

  • [Color(value)] (#creating-a-custom-color) –接受预定义的常量、十六进制字符串或十六进制整数。

  • [Color(r, g, b)] (#rgb) –使用红色、绿色和蓝色值创建颜色。

Mutators – Update an existing Color object.

  • rgb – Updates a color using new RGB values.

  • [hsv] (#hsv) –使用色调、饱和度和亮度更新颜色。

  • [web] (#web) –使用web十六进制颜色字符串更新颜色。

创建自定义颜色#

To use a custom color, you must first create a Color object using one of the following constructors:

Hexadecimal Integer#

使用六位十六进制整数创建颜色。

Usage:
Color(value)

参数

描述

value

A six-digit integer in hexadecimal format (e.g., 0xFFF700 for yellow).

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

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

RGB#

使用单独的红色、绿色和蓝色值创建颜色。

Usage:
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#

使用 Web 颜色字符串(十六进制代码)创建颜色。

Usage:
Color(value)

参数

描述

value

A web color as a string (hex code) (e.g., “#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#

Creates a color using a predefined Color constant.

Usage:
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")

修改器#

These methods update the color of an existing Color object. To use them, assign a Color to a variable when creating it, then call the method on that variable to change its color.

rgb#

rgb updates the color of an existing Color object using RGB values.

Usage:
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 updates the color of an existing Color object using HSV values.

Note: hsv can only be used to change a Color object that has already been created. It cannot be used to create a new Color object.

Usage:
variable_name.hsv(h, s, v)

参数

描述

h

An integer from 0 to 359 representing the hue of the color.

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 updates the color of an existing Color object using a web color (hex code).

Usage:
variable_name.web(value)

参数

描述

value

A web color as a string (hex code) (e.g., “#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)