自定义颜色#

介绍#

color 类用于在 IQ(第一代)大脑中定义和修改颜色。颜色可以使用 RGB 值、十六进制颜色代码、HSV 值或预定义的颜色常量来创建。

类构造函数#

1 — 创建一个没有指定颜色的 color 对象。

color();

2 — 根据十六进制或整数值创建一个 color 对象。

color( 
  int value );

3 — 根据 RGB 值创建 color 对象。

color( 
  uint8_t r, 
  uint8_t g, 
  uint8_t b );

类析构函数#

销毁 color 对象并释放相关资源。

~color();

参数#

范围

类型

描述

value

int

颜色值可以是十六进制 RGB 整数(例如, 0xFFF700),也可以是下面列出的预定义颜色常量之一。

r

uint8_t

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

g

uint8_t

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

b

uint8_t

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

笔记#

  • The following predefined color constants are supported:

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

例子#

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

// Create a color using RGB values
color myBlue = color(0, 100, 255);

// Create a color using a predefined constant
color myRed = color(red);

成员功能#

color 类包含以下成员函数:

  • rgb — 使用 RGB 值更新颜色。

  • hsv — 使用 HSV 值更新颜色。

  • web — 使用 web 十六进制颜色字符串更新颜色。

在调用任何 color 成员函数之前,必须先创建一个 color 实例,如下所示:

// Create a color using RGB values
color brainColor = color(255, 128, 0);

rgb#

使用 RGB 值更新现有 color 对象的颜色。

Available Functions

1 — 使用打包的 RGB 值更新颜色对象。

uint32_t rgb( 
  uint32_t value );

2 — 使用单独的 RGB 值更新颜色对象。

uint32_t rgb( 
  uint8_t r, 
  uint8_t g, 
  uint8_t b );

Parameters

范围

类型

描述

value

uint32_t

包含所有三个颜色分量的打包 RGB 值。

r

uint8_t

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

g

uint8_t

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

b

uint8_t

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

Return Values

返回一个 uint32_t ,表示更新颜色的打包 RGB 值。

Examples
// Drive until a teal object is found
color brainColor = color(50, 200, 180);
Drivetrain.drive(forward);
while (true) {
    if (Color1.detects(brainColor)) {
        Drivetrain.stop();
        break;
    }
    wait(0.5, seconds);
}

hsv#

使用色调、饱和度和亮度值更新现有 color 对象的颜色。

Available Functions
color& hsv( 
  uint32_t hue, 
  double   sat, 
  double   value );

Parameters

范围

类型

描述

hue

uint32_t

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

sat

double

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

value

double

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

Return Values

返回对已更新的 color 对象的引用。

Examples
// Create a custom teal color
color brainColor = color(50, 200, 180);
// Change the color to magenta
brainColor.hsv(300, 0.75, 0.78);

// Drive forward until a magenta object is found
Drivetrain.drive(forward);
while (true) {
    if (Color1.detects(brainColor)) {
        Drivetrain.stop();
        break;
    }
    wait(0.5, seconds);
}

web#

使用十六进制或网页颜色值更新现有 color 对象的颜色。

Available Functions
color& web( 
  const char* color );

Parameters

范围

类型

描述

color

const char*

定义特定颜色的十六进制或网页颜色值(例如, “#FF0000” 表示红色)。

Return Values

返回对已更新的 color 对象的引用。

Examples
// Create a custom teal color
color brainColor = color(50, 200, 180);
// Change the color to magenta
brainColor.web("#AA2896");

// Drive forward until a magenta object is found
Drivetrain.drive(forward);
while (true) {
    if (Color1.detects(brainColor)) {
        Drivetrain.stop();
        break;
    }
    wait(0.5, seconds);
}