colorLeds[i].color()

Category:Circuit

Changes the color of the color LED specified.

Each color LED has three parts (Red, Green, and Blue) to the light itself. If the light is just on, the default color is white. colorLeds[i].color() on its own both turns on the color LED and changes its color.

The .color() block controls the three parts of the LED and has three ways you can change these colors:

  • Using the name of the color (e.g "red", "blue", "purple")
  • The RGB parameters (Any number ranging from 0-255)
  • Hexadecimal codes. Unless you want extremely specific colors, you shouldn't worry too much about using RGB or hexadecimal.

Examples

Example: Rainbow Lights

Turns the lights into a rainbow pattern

//Turns the lights into a rainbow pattern
function rainbow() {
  colorLeds[0].color("red");
  colorLeds[1].color("orange");
  colorLeds[2].color("yellow");
  colorLeds[3].color("green");
  colorLeds[4].color("blue");
  colorLeds[5].color("purple");

}

Example: Random Lights

Every time this program is run the lights turn a random color

//Every time this program is run the lights turn a random color
//Array of random colors
var colors = ["teal", "purple", "red", "green", "blue", "orange", "yellow"];

//Randomly colored lights
colorLeds[0].color(colors[randomNumber(0, colors.length-1)]);
colorLeds[2].color(colors[randomNumber(0, colors.length-1)]);
colorLeds[4].color(colors[randomNumber(0, colors.length-1)]);
colorLeds[6].color(colors[randomNumber(0, colors.length-1)]);
colorLeds[8].color(colors[randomNumber(0, colors.length-1)]);

Example: Color Pattern with Light Switch

Turns the lights different colors around the board by RGB value. The off button turns them all off at the same time.

//Turns the lights different colors around the board by RGB value. The off button turns them all off at the same time.
onEvent("rgb_on_btn", "click", function(event) {
  rgb_on();
});

function rgb_on() {
  //Blue
  colorLeds[0].color(0, 0, 100);
  //Purple
  colorLeds[1].color(100, 0, 100);
  //White
  colorLeds[2].color(100, 100, 100);
  //Green
  colorLeds[3].color(0, 100, 0);
  //Red
  colorLeds[4].color(200, 0, 0);
  //Blue green mix
  colorLeds[5].color(0, 250, 100);
  //Dark yellow
  colorLeds[6].color(15, 15, 10);
  //Pink
  colorLeds[7].color(70, 30, 20);
  //Lime
  colorLeds[8].color(100, 200, 10);
  //Orange
  colorLeds[9].color(250, 100, 10);
}

onEvent("rgb_off_btn", "click", function(event) {
  rgb_off();
});

function rgb_off() {
    colorLeds[0].off();
    colorLeds[1].off();
    colorLeds[2].off();
    colorLeds[3].off();
    colorLeds[4].off();
    colorLeds[5].off();
    colorLeds[6].off();
    colorLeds[7].off();
    colorLeds[8].off();
    colorLeds[9].off();
}

Syntax

colorLeds[index].color(color)

Parameters

NameTypeRequired?Description
color string The color used to light the LED
red number Changes the saturation of red in the LED. Must be a number ranging from 0-255 for this to work.
green number Changes the saturation of green in the LED. Must be a number ranging from 0-255 for this to work.
blue number Changes the saturation of blue in the LED. Must be a number ranging from 0-255 for this to work.

Tips

  • colorLeds[i].color() also accepts hexadecimal strings as a color parameter.

  • You can randomly color the LEDs in a few ways. You can follow example #2 from above and make an array of colors and choose a random index, or you can use the RGB parameters and assign a random number up to 255 to each parameter.

  • Keep in mind: the second method of randomizing colors does not allow much control for what colors you see. If you're looking for certain colors, you should use the array method from example #2.

Found a bug in the documentation? Let us know at documentation@code.org