Language: EN

libreria-esp-color

ESP-Color Library

The ESP_Color library contains functions for color conversion and operation on a processor such as the ESP8266 and ESP32, being the successor to Arduino-ColorConverter.

The Color class includes formats for RGB888, RGB666, RGB565, RGB332, HSV, HSL, GREY256 grayscale, GREY16, GREY4, and black and white BW.

In addition to allowing conversions, ESP_Color incorporates operations such as addition and blending, as well as transparency channel management.

Other utilities are creation from heat temperature, gradient creation, gamma correction, and color palette management.

In other words, basically any function you might ever need in color management. An example of using the library is as follows.

ESP_Color::Color color(1.0f, 0.5f, 0.2f);

Serial.printf("R:%5.2f\tG:%5.2f\tB:%5.2f\tA:%5.2f\n", color.R, color.G, color.B, color.A);
Serial.printf("R:%d\tG:%d\tB:%d\tA:%d\n", color.R_Byte(), color.G_Byte(), color.B_Byte(), color.A_Byte());
Serial.println();

auto hsl = color.ToHsl();
Serial.printf("H:%5.2f\tS:%5.2f\tL:%5.2f\tA:%5.2f\n", hsl.H, hsl.S, hsl.L, hsl.A);

auto hsv = color.ToHsv();
Serial.printf("H:%5.2f\tS:%5.2f\tL:%5.2f\tA:%5.2f\n", hsv.H, hsv.S, hsv.V, hsv.A);

Serial.println();
Serial.printf("RGB8888\t:%x\n", color.ToRgba8888());
Serial.printf("RGB888 \t:%x\n", color.ToRgb888());
Serial.printf("RGB666 \t:%x\n", color.ToRgb666());
Serial.printf("RGB565 \t:%x\n", color.ToRgb565());

Serial.println();
Serial.printf("GREY256\t:%d\n", color.ToGray256());
Serial.printf("GREY16 \t:%d\n", color.ToGray16());
Serial.printf("GREY4  \t:%d\n", color.ToGray4());
Serial.printf("BW     \t:%d\n", color.ToBW());

Color Palettes

The ESP_Color library also incorporates the concept of a palette, an array of colors that allow for efficient user interface generation or effects such as fire.

A large number of predefined palettes of length 64 are included.

ESP_Color::Palette<uint16_t> palette(ESP_Color::Palettes64::MAGMA_64, 64);

for(size_t h = 0; h < screen.height(); h++)
{
  auto index = (int)(64 * (float)h / screen.height());
  screen.drawFastHLine(0, h, screen.width(), palette[index]);
}

However, it is also possible to generate your own palette with any number of elements using the ‘generate’ method.

ESP_Color::Palette<uint16_t> palette(128);

const ESP_Color::Color color1((uint16_t)TFT_YELLOW);
const ESP_Color::Color color2((uint16_t)TFT_CYAN);
const ESP_Color::Color color3((uint16_t)TFT_RED);

palette.Generate([color1, color2, color3](float f) -> uint16_t { 
  return ESP_Color::Color::FromGradient(color1, color2, color3, f).ToRgb565();
} );

Download the Code

All the code is available for download on Github at this link https://github.com/luisllamasbinaburo/ESP-Color github-full