CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::Color Class Reference

24-bit RGB color with mixing, inversion, distance and palette-generation helpers. More...

#include <MathClasses.h>

Public Member Functions

 Color ()
 Black (0,0,0).
 Color (uint8 red, uint8 green, uint8 blue)
 From RGB components.
 Color (const char *name)
 From a named color in ColorMap (case-insensitive); black when unknown.
 Color (const Color &color)
 Copy constructor.
virtual ~Color ()
Color mixOn (Color &color, double weight)
 Blend this color onto a background color.
Color getReverseColor ()
 Component-wise inverse (255-r, 255-g, 255-b).
uint8 getGreyValue ()
 Grey value as the plain average (r+g+b)/3.
double distance (Color &otherColor)
 Euclidean distance to another color in RGB space.
std::string toRGBString ()
 Format as "r,g,b" decimal text.

Static Public Member Functions

static std::vector< ColorcreateColorsMaxDifference (uint32 count, bool mix=false)
 Generate count colors spread for maximum mutual visual difference (e.g.

Public Attributes

uint8 r
uint8 g
uint8 b
uint32 size

Detailed Description

24-bit RGB color with mixing, inversion, distance and palette-generation helpers.

A Color is three 8-bit components (r, g, b) with no alpha; transparency is handled separately by the drawing code (e.g. Bitmap::putTransPixel()'s weight parameter). Colors can be constructed from ~200 CSS/X11 names via ColorMap, blended with mixOn(), compared with distance(), and generated as maximally distinguishable palettes for charts/overlays with createColorsMaxDifference().

using namespace cmlabs;
Color red("red"); // from ColorMap; unknown names give black
Color bg(30, 30, 30);
Color blended = red.mixOn(bg, 0.25); // 25% red over dark grey background
double d = red.distance(bg); // RGB-space Euclidean distance
std::vector<Color> palette = Color::createColorsMaxDifference(8);
static std::vector< Color > createColorsMaxDifference(uint32 count, bool mix=false)
Generate count colors spread for maximum mutual visual difference (e.g.
Color()
Black (0,0,0).

Definition at line 287 of file MathClasses.h.

Constructor & Destructor Documentation

◆ Color() [1/4]

cmlabs::Color::Color ( )

Black (0,0,0).

Definition at line 1286 of file MathClasses.cpp.

Referenced by Color(), createColorsMaxDifference(), distance(), getReverseColor(), and mixOn().

◆ Color() [2/4]

cmlabs::Color::Color ( uint8 red,
uint8 green,
uint8 blue )

From RGB components.

Definition at line 1290 of file MathClasses.cpp.

◆ Color() [3/4]

cmlabs::Color::Color ( const char * name)

From a named color in ColorMap (case-insensitive); black when unknown.

Definition at line 1299 of file MathClasses.cpp.

◆ Color() [4/4]

cmlabs::Color::Color ( const Color & color)

Copy constructor.

Definition at line 1294 of file MathClasses.cpp.

References b, Color(), g, r, and size.

◆ ~Color()

cmlabs::Color::~Color ( )
virtual

Definition at line 1303 of file MathClasses.cpp.

Member Function Documentation

◆ createColorsMaxDifference()

std::vector< Color > cmlabs::Color::createColorsMaxDifference ( uint32 count,
bool mix = false )
static

Generate count colors spread for maximum mutual visual difference (e.g.

for chart series).

Parameters
countNumber of colors.
mixInterleave rather than order by hue.
Returns
Generated palette.

Definition at line 1388 of file MathClasses.cpp.

References Color(), and g.

Referenced by _wrap_Color_createColorsMaxDifference__SWIG_0(), and _wrap_Color_createColorsMaxDifference__SWIG_1().

◆ distance()

double cmlabs::Color::distance ( Color & otherColor)

Euclidean distance to another color in RGB space.

Parameters
otherColorOther color.
Returns
Distance in [0, ~441.7].

Definition at line 1384 of file MathClasses.cpp.

References b, Color(), g, and r.

Referenced by _wrap_Color_distance().

◆ getGreyValue()

uint8 cmlabs::Color::getGreyValue ( )

Grey value as the plain average (r+g+b)/3.

Note
Not perceptually weighted (no 0.299/0.587/0.114 luma coefficients).

Definition at line 1372 of file MathClasses.cpp.

References b, g, and r.

Referenced by _wrap_Color_getGreyValue().

◆ getReverseColor()

Color cmlabs::Color::getReverseColor ( )

Component-wise inverse (255-r, 255-g, 255-b).

Definition at line 1376 of file MathClasses.cpp.

References b, Color(), g, and r.

◆ mixOn()

Color cmlabs::Color::mixOn ( Color & color,
double weight )

Blend this color onto a background color.

Computes this*weight + color*(1-weight) per component, i.e. weight is the fraction of this color in the result and color acts as the background. Weight is clamped to [0,1].

Parameters
colorBackground color.
weightFraction [0,1] of this color in the result.
Returns
Mixed color (neither input is modified).

Definition at line 1359 of file MathClasses.cpp.

References b, Color(), g, and r.

◆ toRGBString()

std::string cmlabs::Color::toRGBString ( )
inline

Format as "r,g,b" decimal text.

Returns
RGB string.

Definition at line 319 of file MathClasses.h.

References b, g, r, and cmlabs::utils::StringFormat().

Member Data Documentation

◆ b

◆ g

◆ r

◆ size

uint32 cmlabs::Color::size

Definition at line 307 of file MathClasses.h.

Referenced by Color().


The documentation for this class was generated from the following files: