颜色#
介绍#
颜色传感器提供了从不同对象返回数据的方法。这使得大脑能够检测颜色及其色调和亮度。
For the examples below, the configured Color Sensor will be named Color1 and will be used in all subsequent examples throughout this API documentation when referring to colorsensor class methods.
以下是所有可用方法的列表:
动作——使用颜色传感器检测物体。
objectDetected– Calls a function when an object is detected.
变异器——修改颜色传感器上的灯光。
setLight– Turns the Color Sensor’s LED on, off, or to a set brightness level.
Getters – 使用颜色传感器返回对象数据。
isNearObject– Returns whether or not an object is close to the Color Sensor.detects– Returns whether or not a specified color is detected by the Color Sensor.colorname– Returns the name of the color detected by the Color Sensor.brightness– Returns the detected brightness of an object.hue– Returns the detected hue value as a float from 0 to 359.99 degrees.colorname3– Returns the closest of red, blue or green to the detected color.color– Return the detected color as a predefined color object.installed– Returns whether the Color Sensor is connected to the Brain.
构造函数——手动初始化颜色传感器。
colorsensor– Create a Color Sensor.
行动#
objectDetected#
objectDetected registers a function to be called when the Color Sensor detects an object.
Usage:
Color1.objectDetected(callback);
参数 |
描述 |
|---|---|
|
当颜色传感器检测到物体时执行的先前定义的函数。 |
修改器#
setLight#
setLight sets the state of the Color Sensor LED.
Default Usage:
Color1.setLight(value, units);
Overload Usages:
Color1.setLight(state);
参数 |
描述 |
|---|---|
|
将光的强度设置为 0 - 100 之间的百分比值。 |
|
The unit that represents the intensity:
|
|
A valid LED state:
|
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Blink the Color Sensor LED on and off
while (true) {
Color1.setLight(ledState::on);
wait(0.5, seconds);
Color1.setLight(ledState::off);
wait(0.5, seconds);
}
}
吸气剂#
isNearObject#
isNearObject returns a Boolean indicating whether or not the Color Sensor is close to an object.
1– The Color Sensor detects a close object.0– The Color Sensor does not detect a close object.
Usage:
Color1.isNearObject()
参数 |
描述 |
|---|---|
该方法没有参数。 |
detects#
detects returns if the Color Sensor is detecting a specified color.
1– The Color Sensor detects the color.0– The Color Sensor does not detect the color.
Usage:
Color1.detects(color)
参数 |
描述 |
|---|---|
|
A valid color:
|
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Drive until a red object is seen
Drivetrain.drive(forward);
while (true) {
if (Color1.detects(red)) {
Drivetrain.stop();
break;
wait(0.5, seconds);
}
}
}
colorname#
colorname returns the closest matching color from a preset list shown below.
直接打印时,它会显示与该颜色相关的数值:
颜色 |
数值 |
|---|---|
|
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
十三 |
|
14 |
|
15 |
|
16 |
|
17 |
Usage:
Color1.colorname()
参数 |
描述 |
|---|---|
该方法没有参数。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Continuously check the detected color
while (true) {
// Compare the color name with "red"
if (convertColorToString(Color1.colorname()) == "red") {
Brain.Screen.print("Red object!");
} else {
Brain.Screen.print("Not red");
}
wait(0.5, seconds);
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
}
Note: The function
convertColorToStringis automatically created by VEXcode when a Color Sensor is configured in the Devices window. It is only available inside VEXcode IQ (2nd gen) projects and will not work in other environments.When using
colornameoutside of VEXcode, comparisons should be made directly with acolorTypevalue — any color that begins withcolorType::(for example,colorType::greenorcolorType::red).
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Continuously check the detected color
while (true) {
// Compare the color name with the colorType
if (Color1.colorname() == colorType::red) {
Brain.Screen.print("Red object!");
} else {
Brain.Screen.print("Not red");
}
wait(0.5, seconds);
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
}
brightness#
brightness returns the brightness value from the Color Sensor.
Usage:
Color1.brightness(bRaw)
参数 |
描述 |
|---|---|
|
Optional. A Boolean indicating whether to return the raw value detected by the Color Sensor:
|
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Slowly turn and show brightness values
Drivetrain.setTurnVelocity(20, percent);
Drivetrain.turn(right);
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Brightness: %d", Color1.brightness());
wait(0.25, seconds);
}
}
hue#
hue returns the hue value detected by the Color Sensor as an integer.
Usage:
Color1.hue()
参数 |
描述 |
|---|---|
该方法没有参数。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Slowly turn and show hue values
Drivetrain.setTurnVelocity(20, percent);
Drivetrain.turn(right);
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Hue: %d", Color1.hue());
wait(0.25, seconds);
}
}
colorname3#
colorname3 returns the closest color detected to red, green, or blue as an enumerated value (enum).
颜色 |
数值 |
|---|---|
|
1 |
|
2 |
|
3 |
Usage:
Color1.colorname3()
参数 |
描述 |
|---|---|
该方法没有参数。 |
color#
color returns the closest predefined color object detected by the Color Sensor:
redgreenbluewhiteyelloworangepurplecyanred_violetvioletblue_violetblue_greenyellow_greenyellow_orangered_orangeblacktransparent
Usage:
Color1.color()
参数 |
描述 |
|---|---|
该方法没有参数。 |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Continuously check the detected color
while (true) {
// Compare the color name with "red"
if (convertColorToString(Color1.color()) == "red") {
Brain.Screen.print("Red object!");
} else {
Brain.Screen.print("Not red");
}
wait(0.5, seconds);
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
}
Note: The function
convertColorToStringis automatically created by VEXcode when a Color Sensor is configured in the Devices window. It is only available inside VEXcode IQ (2nd gen) projects and will not work in other environments.When using
coloroutside of VEXcode, comparisons should be made directly with acolorTypevalue — any color that begins withcolorType::(for example,colorType::greenorcolorType::red).
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Continuously check the detected color
while (true) {
// Compare the color name with the colorType
if (Color1.color() == colorType::red) {
Brain.Screen.print("Red object!");
} else {
Brain.Screen.print("Not red");
}
wait(0.5, seconds);
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
}
installed#
installed returns a Boolean indicating whether the Color Sensor is currently connected to the Brain.
1– The Color Sensor is connected to the Brain.0– The Color Sensor is not connected to the Brain.
Usage:
Color1.installed()
参数 |
描述 |
|---|---|
该方法没有参数。 |
构造函数#
colorsensor#
colorsensor creates an object of the colorsensor Class in the specified port.
Usage:
colorsensor Color1 = colorsensor(port);
范围 |
描述 |
|---|---|
|
Which Smart Port that the Color Sensor is connected to as |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Create a Color Sensor in Port 3
colorsensor myColorSensor = colorsensor(PORT3);
}