自定义颜色#

介绍#

VEX IQ (2nd gen) 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. Below is a list of available methods:

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 (0–360), saturation, and brightness (0.0–1.0).

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

吸气剂

构造函数#

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
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 之间的整数。

// Construct a yellow color "Yellow" using
// 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

// Construct a yellow color "Yellow" using a
// predefined color constant
color Yellow = color(yellow);

Brain.Screen.setPenColor(Yellow);
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 之间的整数,表示颜色的蓝色成分。

// Create a custom teal color and set it as the pen color
color brainColor = color(50, 200, 180);
Brain.Screen.setPenColor(brainColor);

// Draw a rectangle with the teal outline
Brain.Screen.drawRectangle(5, 10, 80, 40);

// Draw another rectangle with a magenta outline
brainColor.rgb(170, 40, 150);
Brain.Screen.setPenColor(brainColor);
Brain.Screen.drawRectangle(5, 60, 80, 40);

hsv#

hsv(hue, saturation, value) changes the existing color instance using HSV values.

Usage:
hsv(h, s, v)

参数

描述

h

表示颜色色调的 0 至 360 之间的整数。

s

从 0.0 到 1.0 的双精度值,表示颜色的饱和度。

v

从 0.0 到 1.0 的双精度值,表示颜色的亮度。

**返回:**表示颜色的整数值。

// Create a custom teal color and set it as the pen color
color brainColor = color(50, 200, 180);
Brain.Screen.setPenColor(brainColor);

// Draw a rectangle with the teal outline
Brain.Screen.drawRectangle(5, 10, 80, 40);

// Draw another rectangle with a magenta outline
brainColor.hsv(300, 0.75, 0.78);
Brain.Screen.setPenColor(brainColor);
Brain.Screen.drawRectangle(5, 60, 80, 40);

web#

web(value) changes the existing color instance using a web color (hex code).

Usage:
web(value)

参数

描述

value

用于更新现有颜色实例的字符串形式的 Web 颜色(十六进制代码)。

**返回:**表示颜色的整数值。

// Create a custom teal color and set it as the pen color
color brainColor = color();
brainColor.web("#32C8B6");
Brain.Screen.setPenColor(brainColor);

// Draw a rectangle with the teal outline
Brain.Screen.drawRectangle(5, 10, 80, 40);

// Draw another rectangle with the new magenta outline
brainColor.web("#AA2896");
Brain.Screen.setPenColor(brainColor);
Brain.Screen.drawRectangle(5, 60, 80, 40);

吸气剂#

isTransparent#

isTransparent() returns whether the color is transparent or not.

  • true - The color is transparent.

  • false - The color is not transparent.

Usage:
isTransparent()

// Create a transparent color
color transparentColor = color(transparent);

// Check if the color is transparent
if (transparentColor.isTransparent()) {
    Brain.Screen.print("Transparent");
} else {
    Brain.Screen.print("Not transparent");
}