Simple Arduino library for storing, manipulating, and using colour data.
- Create colours with RGB or hexadecimal notation
- Manipulate colours by blending, scaling, darkening, lightning, inverting, and converting to greyscale
- Output compatible formats for Adafruit GFX and FastLED libraries
ColourKit is available in the Arduino Library Manager. The library can be installed using the standard Arduino library install procedure.
Include the ColourKit.h header file and create your first Colour:
#include <ColourKit.h>
Colour(0xffffff);Below is a simple sketch that demonstrates blending two colours together and outputting the result to an RGB (common cathode) LED on pins 9, 10, and 11.
#include <ColourKit.h>
void setup() {
Colour c1(255, 0, 0); // Red colour
Colour c2(0, 255, 0); // Green colour
// Blend colours together
Colour blended = Colour::lerp(c1, c2, 0.5);
// Write blended colour to LED on PWM pins (R, G, B) = (9, 10, 11)
blended.writeLed(9, 10, 11);
}
void loop() {
// Your loop code here
}There are several ways to create a colour.
Colour myColour(255, 100, 50); // Red = 255, Green = 100, Blue = 50Colour myColour(0xFF6432); // Equivalent to RGB(255, 100, 50)If you're using a hexadecimal value you found elsewhere, make sure to swap the leading # for a 0x.
Colour myColour; // Defaults to RGB(0, 0, 0)myColour.setRed(200);
myColour.setGreen(150);
myColour.setBlue(100);uint8_t red = myColour.getRed();
uint8_t green = myColour.getGreen();
uint8_t blue = myColour.getBlue();Create a new colour by additively blending two existing colours. The blending operation adds the RGB component pairs to create the new RGB value.
Colour c1(100, 50, 25);
Colour c2(150, 200, 100);
Colour c3 = c1 + c2;Subtractive blending works similarly to additive blending.
Colour c1(200, 150, 100);
Colour c2(50, 50, 50);
Colour c3 = c1 - c2;You can also use the += and -= operators to blend a colour value directly.
Colour c1(100, 50, 25);
Colour c2(200, 150, 100);
Colour c3(10, 10, 10);
// Brighten c1
c1 += c3;
// Darken c2
c2 -= c2;Colours can be scaled by a 1 byte value (from 0-255).
Colour myColour(255, 100, 50);
myColour.scale(128); // Scale down the colour components by 50%Interpolation blends the RGB components of two colours with a specified fraction. This sets the bias of the blending.
Colour c1(0, 0, 255);
Colour c2(255, 0, 0);
Colour midColour = Colour::lerp(startColour, endColour, 0.5f); // 50% blendThe brightness of a colour can be increased by a percentage.
Colour darker = myColour.darken(20); // Darken by 20%
Colour lighter = myColour.lighten(20); // Lighten by 20%Colour inverted = myColour.invert();Colour grey = myColour.greyscale();Any colour can be easily output to specified LED pins using PWM.
myColour.writeLed(9, 10, 11); // Write the color to pins 9 (Red), 10 (Green), 11 (Blue)ColourKit also plays nice with the Adafruit GFX and FastLED libraries.
Colours can be used directly in Adafruit GFX graphics functions.
Colour myColour(100, 50, 25);
tft.fillScreen(myColour);Similarly, colours can also be used directly when setting colours with FastLED.
Colour myColour(100, 50, 25);
leds[i] = colour;ColourKit can easily be used to store colour sensor output data.
The library includes a wrapper for the TCS34725 colour sensor, requiring the Adafruit TCS34725 library.
#include <Adafruit_TCS34725.h>
#include <ColourKit.h>
Colour my_colour;
ColourKit_TCS34725 TCS34725;
void setup() {
Serial.begin(9600);
if (!TCS34725.begin()) {
Serial.println("Could not find a valid TCS34725 sensor, check wiring!");
while (1);
}
}
void loop() {
my_colour = TCS34725.getColour();
my_colour.writeLed(9, 10, 11);
delay(50);
}Make sure to include Adafruit_TCS34725.h before the ColourKit library to prevent errors during compile. The Adafruit TCS34725 library must be installed.