Color#

Introducción#

El sensor de color proporciona métodos para obtener datos de diferentes objetos. Esto permite al cerebro detectar colores, así como su tono y brillo.

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.

A continuación se muestra una lista de todos los métodos disponibles:

Acciones: Detectar objetos con el sensor de color.

Mutadores: modifican las luces del sensor de color.

  • setLight – Turns the Color Sensor’s LED on, off, or to a set brightness level.

Obtenedores: devuelven datos del objeto con el sensor de color.

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

Constructores: inicializan manualmente el sensor de color.

Comportamiento#

objectDetected#

objectDetected registers a function to be called when the Color Sensor detects an object.

Usage:
Color1.objectDetected(callback);

Parámetros

Descripción

callback

Una función que se define previamente para ejecutarse cuando el sensor de color detecta un objeto.

Mutadores#

setLight#

setLight sets the state of the Color Sensor LED.

Default Usage:
Color1.setLight(value, units);

Overload Usages:
Color1.setLight(state);

Parámetros

Descripción

value

El valor de la intensidad para establecer la luz como un porcentaje de 0 a 100.

units

The unit that represents the intensity:

  • percent

state

A valid LED state:

  • ledState::off
  • ledState::on
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);
  }
}

Captadores#

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

Parámetros

Descripción

Este método no tiene parámetros.

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)

Parámetros

Descripción

color

A valid color:

  • blue
  • blue_green
  • blue_violet
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
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.

Cuando se imprime directamente, muestra el valor numérico asociado a ese color:

Color

Valor numérico

none

0

red

1

green

2

blue

3

white

4

yellow

5

orange

6

purple

7

cyan

8

red_violet

9

violet

10

blue_violet

11

blue_green

12

yellow_green

13

yellow_orange

14

red_orange

15

black

16

transparent

17

Usage:
Color1.colorname()

Parámetros

Descripción

Este método no tiene parámetros.

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 convertColorToString is 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 colorname outside of VEXcode, comparisons should be made directly with a colorType value — any color that begins with colorType:: (for example, colorType::green or colorType::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)

Parámetros

Descripción

bRaw

Optional. A Boolean indicating whether to return the raw value detected by the Color Sensor:

  • true – Return the raw value.
  • false (default) – Do not return the raw value.
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()

Parámetros

Descripción

Este método no tiene parámetros.

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

Color

Valor numérico

red

1

green

2

blue

3

Usage:
Color1.colorname3()

Parámetros

Descripción

Este método no tiene parámetros.

color#

color returns the closest predefined color object detected by the Color Sensor:

  • red

  • green

  • blue

  • white

  • yellow

  • orange

  • purple

  • cyan

  • red_violet

  • violet

  • blue_violet

  • blue_green

  • yellow_green

  • yellow_orange

  • red_orange

  • black

  • transparent

Usage:
Color1.color()

Parámetros

Descripción

Este método no tiene parámetros.

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 convertColorToString is 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 color outside of VEXcode, comparisons should be made directly with a colorType value — any color that begins with colorType:: (for example, colorType::green or colorType::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()

Parámetros

Descripción

Este método no tiene parámetros.

Constructores#

colorsensor#

colorsensor creates an object of the colorsensor Class in the specified port.

Usage:
colorsensor Color1 = colorsensor(port);

Parámetro

Descripción

port

Which Smart Port that the Color Sensor is connected to as PORT followed by the port number, ranging from 1 to 12.

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

  // Create a Color Sensor in Port 3
  colorsensor myColorSensor = colorsensor(PORT3);
}