# esp-idf-lib/color
[](https://github.com/esp-idf-lib/color/actions/workflows/build.yml)
[](https://github.com/esp-idf-lib/color/actions/workflows/build-docs.yml)
[](https://github.com/esp-idf-lib/color/actions/workflows/validate-component.yml)
Common library for RGB and HSV colors.
The component is a set of functions and small inline helpers: no hardware, no
I2C, no allocation, nothing to initialise. It is used by the LED and display
drivers of the project, and it is useful on its own whenever a program has to
generate or mix colors.
* [Documentation](https://esp-idf-lib.github.io/color/)
* [Repository](https://github.com/esp-idf-lib/color)
* [Issues](https://github.com/esp-idf-lib/color/issues)
* [Discussions and questions](https://github.com/esp-idf-lib/core/discussions)
* [Component page at the ESP Component Registry](https://components.espressif.com/components/esp-idf-lib/color)
## Color types
`rgb_t` keeps three 8-bit channels (`r`, `g`, `b`, also reachable as `red`,
`green`, `blue`), and `hsv_t` keeps hue, saturation and value (`h`/`hue`,
`s`/`sat`, `v`/`val`). Both are plain structs, compared and copied by value.
```c
rgb_t red = rgb_from_code(0xFF0000); // 0x00RRGGBB
hsv_t cyan = hsv_from_values(128, 255, 255);
bool dark = rgb_is_zero(black);
```
The hue constants `HUE_RED`, `HUE_ORANGE`, `HUE_YELLOW`, `HUE_GREEN`,
`HUE_AQUA`, `HUE_BLUE`, `HUE_PURPLE`, `HUE_PINK` are spaced 32 apart on the
0…255 hue circle.
## HSV to RGB
Three conversions differ in speed and in how the hue circle is mapped:
| Function | Hue range | Character |
|----------|-----------|-----------|
| `hsv2rgb_rainbow()` | 0…255 | Visually balanced rainbow, the best yellows and oranges |
| `hsv2rgb_spectrum()` | 0…255 | Mathematically straight spectrum, more green and blue |
| `hsv2rgb_raw()` | 0…191 | Same straight spectrum, fastest, hue must be scaled to 0…191 |
Saturation and value are always 0…255.
`rgb2hsv_approximate()` goes back from RGB to HSV. The two 8-bit spaces are not
a bijection (for example, no HSV value converts to RGB 255,255,0), so the result
is an approximation. It is most accurate for fully saturated colors that came
from the rainbow conversion, and it is noticeably slower than the forward
conversion.
`rgb_heat_color()` maps a heat level 0…255 to an approximation of the black body
spectrum, which is handy for fire effects.
## Filling arrays
Solid fills:
```c
rgb_fill_solid_rgb(leds, count, rgb_from_code(0x002040));
rgb_fill_solid_hsv(leds, count, hsv_from_values(HUE_AQUA, 255, 64));
hsv_fill_solid_hsv(target, count, color);
```
Gradients come in two families. The HSV family takes a direction, because hue
is a circle:
- `COLOR_FORWARD_HUES` — from the start hue upwards,
- `COLOR_BACKWARD_HUES` — downwards,
- `COLOR_SHORTEST_HUES` — the shorter of the two paths,
- `COLOR_LONGEST_HUES` — the longer one.
`rgb_fill_gradient_hsv(target, startpos, startcolor, endpos, endcolor, direction)`
fills from position `startpos` to `endpos`; the wrappers
`rgb_fill_gradient2_hsv()`, `...3_hsv()` and `...4_hsv()` fill one, two and
three consecutive segments, so the 2-colour helper builds a single ramp. The `hsv_fill_gradient*_hsv()` variants do the same with
HSV output. The RGB family has no direction, because RGB has no color wheel:
`rgb_fill_gradient_rgb()` and its `...2/3/4_rgb()` wrappers.
Positions are array indices and must stay inside the array; to fill only part of
a strip, pass that part with its own offsets. If the end position is lower than
the start, the two colors are swapped and the range is filled anyway.
## Palettes
```c
rgb_t palette[3] = { rgb_from_code(0x200000), rgb_from_code(0x002000), rgb_from_code(0x000020) };
rgb_t c = color_from_palette_rgb(palette, 3, index, 255, true);
```
`color_from_palette_hsv()` and `color_from_palette_rgb()` take an `index` of
0…255 and spread the `pal_size` explicit entries evenly over that range, so a
three-entry palette works for a full 256-step ramp. With `blend` set, the values
between entries are interpolated (including the wrap from the last entry back to
the first); without it, the entry covering the index is returned (the lower one of the two
neighbours). `brightness` scales the
result.
`blend()` mixes two HSV colors by an amount of 0…255 and honours the same
direction constants.
## Blur
```c
blur1d(leds, count, 64);
blur2d(leds, width, height, 128, xy_to_offset, &layout);
```
`blur1d()` spreads light to the two neighbours; 0 keeps the array untouched,
64 is moderate, about 172 gives a smooth even spread.
`blur_rows()`, `blur_columns()` and `blur2d()` work on a rectangular matrix and
need the application to map `(x, y)` to an array offset through the
`xy_to_offs_cb` callback. That is what makes serpentine (zigzag) and other
non-linear layouts work: the filter asks for the neighbour of a pixel and the
callback answers where that pixel lives in memory.
## Gamma
```c
uint8_t b = apply_gamma2brightness(128, 2.2f);
rgb_t c = apply_gamma2rgb(color, 2.2f);
rgb_t g = apply_gamma2rgb_channels(color, 2.2f, 2.4f, 2.0f);
```
The gamma functions raise a channel to the given power through `powf()`. They
never turn a non-zero channel into zero, so a dim color does not disappear.
Per-channel gamma corrects the different curves of individual LEDs.
## Dependencies
The component depends on [lib8tion](https://github.com/esp-idf-lib/lib8tion) for
the fixed-point helpers (`fract8`, scaling and addition of 8-bit values).
## Example
The component has no runnable example: it needs no hardware and no setup. The
library in action can be seen in
[led_strip_spi](https://github.com/esp-idf-lib/led_strip_spi) and in the
`pixelfont` examples.
## Installation
```sh
idf.py add-dependency esp-idf-lib/color
```
## Support
For questions and discussions about the component, please use
[Discussions](https://github.com/esp-idf-lib/core/discussions)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).
## Contributing
Please read [CONTRIBUTING.md](https://github.com/esp-idf-lib/core/blob/main/CONTRIBUTING.md)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).
dc9f89c70d9e7b167dd3361c31665df677211fdd
idf.py add-dependency "esp-idf-lib/color^1.0.9"