Skip to main content
Version: Matrix OS 4.0 🚧

Class: Color

The Color class provides methods for handling RGBW colors, scaling brightness, and converting between color formats.

The header file for this class is located in OS/Framework/Color/Color.h and the implementation is in OS/Framework/Color/Color.cpp.


Constructors​

Default Constructor​

Color();

Default constructor. Initializes the color to black (R = 0, G = 0, B = 0, W = 0).


From 32-bit WRGB​

Color(uint32_t WRGB);

Constructs a color using a 32-bit WRGB value.

Parameters:

  • WRGB (uint32_t): A 32-bit value containing W, R, G, and B components.

From R, G, B, W​

Color(uint8_t nR, uint8_t nG, uint8_t nB, uint8_t nW = 0);

Constructs a color using individual RGBW values. W defaults to 0 if not specified.

Parameters:

  • nR (uint8_t): Red component.
  • nG (uint8_t): Green component.
  • nB (uint8_t): Blue component.
  • nW (uint8_t, optional): White component. Defaults to 0.

Member Functions​

Update​

virtual void Update();

A virtual method for updating the color. Can be overridden in derived classes.


RGB​

uint32_t RGB(uint8_t brightness = 255);

Returns the RGB value as a 32-bit integer, scaled by brightness.

Parameters:

  • brightness (uint8_t, optional): Brightness scale (0-255). Defaults to 255.

Returns:

  • uint32_t: The scaled RGB value.

GRB​

uint32_t GRB(uint8_t brightness = 255);

Returns the GRB value as a 32-bit integer, scaled by brightness.

Parameters:

  • brightness (uint8_t, optional): Brightness scale (0-255). Defaults to 255.

Returns:

  • uint32_t: The scaled GRB value.

Scale​

Color Scale(uint8_t scale);

Scales the color by the specified factor.

Parameters:

  • scale (uint8_t): The scale factor (0-255).

Returns:

  • Color: The scaled color.

Dim​

Color Dim(uint8_t scale = COLOR_LOW_STATE_SCALE);

Dims the color by the specified factor.

Parameters:

  • scale (uint8_t, optional): The dimming factor. Defaults to COLOR_LOW_STATE_SCALE.

Returns:

  • Color: The dimmed color.

DimIfNot​

Color DimIfNot(bool not_dim = false, uint8_t scale = COLOR_LOW_STATE_SCALE);

Dims the color conditionally. This is useful to pass in a boolean to determine if the color should be dimmed. Like UI elements that are disabled.

Parameters:

  • not_dim (bool, optional): If true, the color will not be dimmed. Defaults to false.
  • scale (uint8_t, optional): The dimming factor. Defaults to COLOR_LOW_STATE_SCALE.

Returns:

  • Color: The resulting color.

Gamma​

Color Gamma();

Applies gamma correction to the color.

Returns:

  • Color: The gamma-corrected color.

Color::scale8​

static uint8_t scale8(uint8_t i, uint8_t scale);

Scales a value using an 8-bit factor.

Parameters:

  • i (uint8_t): The value to scale.
  • scale (uint8_t): The scale factor.

Returns:

  • uint8_t: The scaled value.

Color::scale8_video​

static uint8_t scale8_video(uint8_t i, uint8_t scale);

Scales a value using an 8-bit factor, ensuring the result is never zero unless the input is zero.

Parameters:

  • i (uint8_t): The value to scale.
  • scale (uint8_t): The scale factor.

Returns:

  • uint8_t: The scaled value.

Color::HsvToRgb​

static Color HsvToRgb(float h, float s, float v);

Converts an HSV value to an RGB color.

Parameters:

  • h (float): Hue (0-360).
  • s (float): Saturation (0-1).
  • v (float): Value (0-1).

Returns:

  • Color: The RGB color.

Color::RgbToHsv​

static void RgbToHsv(Color rgb, float* h, float* s, float* v);

Converts an RGB color to HSV.

Parameters:

  • rgb (Color): The RGB color.
  • h (float*): Pointer to store the hue.
  • s (float*): Pointer to store the saturation.
  • v (float*): Pointer to store the value.

Color::Crossfade​

static Color Crossfade(Color color1, Color color2, Fract16 ratio);

Crossfades between two colors.

Parameters:

  • color1 (Color): The starting color.
  • color2 (Color): The ending color.
  • ratio (Fract16): The crossfade ratio (0-65535).

Returns:

  • Color: The resulting color.

Operators​

Conversion to bool​

operator bool();

Checks if the color is non-zero.

Returns:

  • bool: true if any component (R, G, B, or W) is non-zero, otherwise false.

Comments