自定义颜色#

介绍#

VEX V5 supports the use of custom colors for drawing and text. Custom colors can be created using RGB values, hex codes, HSV values, or predefined constants. The color class includes methods for both creating and updating color objects.

以下是可用方法列表:

Constructors — Create a new color object.

Mutators — Update an existing color object.

  • rgb — Updates a color using new RGB values.

  • hsv — Updates a color using hue, saturation, and brightness.

  • web — Updates a color using a web hex color string (e.g. “#32C8B6”).

Getters — Retrieve information from a color object.

构造函数#

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).

int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!

  // Create the color yellow using a hexadecimal value
  color myYellow = color(0xFFF700);

  Brain.Screen.setPenColor(myYellow);
  Brain.Screen.print("My Yellow");
}

RGB Constructor#

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

Usage:
color(r, g, b)

范围

描述

r

一个介于 0 到 255 之间的整数,表示红色分量。

g

一个介于 0 到 255 之间的整数,表示绿色分量。

b

一个介于 0 到 255 之间的整数,表示蓝色分量。

// Create the color yellow using RGB values
int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!

  // Construct a yellow color "Yellow" using a
  // RGB values
  color myYellow = color(255, 247, 0);

  Brain.Screen.setPenColor(myYellow);
  Brain.Screen.print("My Yellow");
}

Predefined Color#

使用预定义的颜色常量创建颜色。

Usage:
color(value)

范围

描述

value

A built-in color constant:

  • black
  • blue
  • blue_green
  • blue_violet
  • cyan
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • transparent
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange

int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!

  // Create the color yellow using a predefined color constant
  color myYellow = color(yellow);

  Brain.Screen.setPenColor(myYellow);
  Brain.Screen.print("My Yellow");
}

变异体#

These mutators allow you to modify or query a color object after it has been created.

rgb#

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

Usage:
rgb(r, g, b)

参数

描述

r

一个介于 0 到 255 之间的整数,表示颜色中的红色分量。

g

一个介于 0 到 255 之间的整数,表示颜色中的绿色分量。

b

一个介于 0 到 255 之间的整数,表示颜色中的蓝色分量。

int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!

  // Draw a rectangle with a custom teal color
  color brainColor = color(50, 200, 180);
  Brain.Screen.setFillColor(brainColor);
  Brain.Screen.drawRectangle(0, 0, 480, 120);

  // Change the color to magenta for a second rectangle
  brainColor.rgb(170, 40, 150);

  Brain.Screen.setFillColor(brainColor);
  Brain.Screen.drawRectangle(0, 120, 480, 120);
}

hsv#

hsv changes an existing color object using HSV values.

Usage:
hsv(h, s, v)

参数

描述

h

一个介于 0 到 360 之间的整数,表示颜色的色调。

s

一个介于 0.0 到 1.0 之间的双精度浮点数,表示颜色的饱和度。

v

一个介于 0.0 到 1.0 之间的双精度浮点数,表示颜色的亮度。

int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!

  // Draw a rectangle with a custom teal color
  color brainColor = color(50, 200, 180);
  Brain.Screen.setFillColor(brainColor);
  Brain.Screen.drawRectangle(0, 0, 480, 120);

  // Change the color to magenta for a second rectangle
  brainColor.hsv(300, 0.75, 0.78);

  Brain.Screen.setFillColor(brainColor);
  Brain.Screen.drawRectangle(0, 120, 480, 120);
}

web#

web changes an existing color object using a web color (hex code).

Usage:
web(value)

参数

描述

value

用于更新现有颜色对象的网页颜色(十六进制代码)字符串。

int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!

  // Draw a rectangle with a custom teal color
  color brainColor = color(50, 200, 180);
  Brain.Screen.setFillColor(brainColor);
  Brain.Screen.drawRectangle(0, 0, 480, 120);

  // Change the color to magenta for a second rectangle
  brainColor.web("#AA2896");

  Brain.Screen.setFillColor(brainColor);
  Brain.Screen.drawRectangle(0, 120, 480, 120);
}

获取器#

isTransparent#

isTransparent returns whether the color is transparent or not.

  • true — The color is transparent.

  • false — The color is not transparent.

Usage:
isTransparent()

参数

描述

此方法没有参数。