Pogosim
Loading...
Searching...
No Matches
colormaps.h File Reference
#include <stdint.h>

Go to the source code of this file.

Classes

struct  color_t
 Structure representing a color with red, green, and blue components. More...

Macros

#define SCALE_0_255_TO_0_25(x)
#define SCALE_0_25_TO_0_255(x)

Functions

void hsv_to_rgb (float h, float s, float v, uint8_t *r, uint8_t *g, uint8_t *b)
 HSV → RGB conversion helper.
void qualitative_colormap (uint8_t const value, uint8_t *r, uint8_t *g, uint8_t *b)
 Maps a given value to a qualitative colormap.
void rainbow_colormap (uint8_t const value, uint8_t *r, uint8_t *g, uint8_t *b)
 Maps a given value to a rainbow colormap with smooth interpolation.

Macro Definition Documentation

◆ SCALE_0_255_TO_0_25

#define SCALE_0_255_TO_0_25 ( x)
Value:
((uint8_t)(((x) * (25.0f / 255.0f)) + 0.5f))

◆ SCALE_0_25_TO_0_255

#define SCALE_0_25_TO_0_255 ( x)
Value:
((uint8_t)(x*10))

Function Documentation

◆ hsv_to_rgb()

void hsv_to_rgb ( float h,
float s,
float v,
uint8_t * r,
uint8_t * g,
uint8_t * b )

HSV → RGB conversion helper.

This routine converts a colour expressed in HSV (Hue, Saturation, Value) space to its equivalent RGB (Red, Green, Blue) representation with 8-bit integer channels (0 – 255).

The implementation follows the classical algorithm described in Foley & van Dam Computer Graphics: Principles and Practice
(section “Converting HSV to RGB”), using the “hexcone” model:

  • Hue (h) is interpreted as an angle around the colour wheel, measured in degrees in [0 … 360).
  • Saturation (s) and Value (v) are floating-point fractions in [0.0 … 1.0], where 0 is fully desaturated (grey / black) and 1 is maximum chroma / brightness.

Internally, the algorithm:

  1. Computes chroma c = v·s.
  2. Finds the auxiliary value x = c·(1 − |((h/60) mod 2) − 1|) to locate the point inside the RGB cube for the current hue sector.
  3. Adds the “match” term m = v − c to re-translate the temporary colour into the cube whose origin is black.

The six hue sectors [0°,60°), [60°,120°), …, [300°,360°) are handled explicitly to avoid expensive divisions at run-time (important on the robot’s MCU). The resulting floating-point RGB components are finally scaled to integers in 0 … 255 and written back through the output pointers.

Typical use

uint8_t r, g, b;
hsv_to_rgb(210.0f, 1.0f, 1.0f, &r, &g, &b); // vivid blue
pogobot_led_setColors(r, g, b, 0); // light the main LED
void hsv_to_rgb(float h, float s, float v, uint8_t *r, uint8_t *g, uint8_t *b)
HSV → RGB conversion helper.
Definition colormaps.c:9
void pogobot_led_setColors(const uint8_t r, const uint8_t g, const uint8_t b, uint8_t id)
Definition spogobot.cpp:269

Numerical accuracy

  • The conversion preserves full saturation and value in the sense that at least one output channel will equal 255 whenever v == 1.0f and s > 0.
  • Rounding is done with a simple cast – values midway between two integers are rounded down (e.g. 254.7 → 254). If you need round-to-nearest, add 0.5 f before the cast.
Parameters
[in]hHue angle in degrees (wraps outside [0,360) without UB)
[in]sSaturation fraction in the closed interval [0.0, 1.0]
[in]vValue / brightness fraction in [0.0, 1.0]
[out]rPointer to destination for the red component (0 – 255)
[out]gPointer to destination for the green component (0 – 255)
[out]bPointer to destination for the blue component (0 – 255)
Warning
The function assumes the output pointers are non-NULL.
Passing NULL is undefined behaviour.
See also
rainbow_colormap(), qualitative_colormap()

◆ qualitative_colormap()

void qualitative_colormap ( uint8_t const value,
uint8_t * r,
uint8_t * g,
uint8_t * b )

Maps a given value to a qualitative colormap.

This function assigns a fixed color from a predefined qualitative colormap based on the provided value. The colormap is defined as an array of 10 distinct colors. The input value is mapped to a color index using modulo arithmetic, ensuring that it wraps around if the value exceeds the number of available colors.

Parameters
valueThe input value used to select a color from the colormap.
rPointer to a uint8_t variable where the red component of the selected color will be stored.
gPointer to a uint8_t variable where the green component of the selected color will be stored.
bPointer to a uint8_t variable where the blue component of the selected color will be stored.

◆ rainbow_colormap()

void rainbow_colormap ( uint8_t const value,
uint8_t * r,
uint8_t * g,
uint8_t * b )

Maps a given value to a rainbow colormap with smooth interpolation.

This function computes a rainbow color based on the input value. The value, expected in the range [0, 255], is normalized to a range [0, 6] to determine the color segment and the interpolation factor. Depending on the segment, the function interpolates between two adjacent colors in the rainbow spectrum.

The segments are as follows:

  • Region 0: Red to Yellow
  • Region 1: Yellow to Green
  • Region 2: Green to Cyan
  • Region 3: Cyan to Blue
  • Region 4: Blue to Magenta
  • Region 5: Magenta to Red
Parameters
valueThe input value (0-255) that determines the color.
rPointer to a uint8_t variable where the red component of the computed color will be stored.
gPointer to a uint8_t variable where the green component of the computed color will be stored.
bPointer to a uint8_t variable where the blue component of the computed color will be stored.