Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
UppHub
Status & Roadmap
FAQ
Authors & License
Forums
Funding U++
Search on this site











SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo

Color

 

class Color : private AssignValueTypeNo<Color, COLOR_V, Moveable<Color> > 

Color type representing RGB color. All components are in the range 0 through 255. Null value is defined for Color. Color is Value compatible as a Rich-Value type (supports Value comparison, serialization etc.).

 

 

Constructor Detail

 

Color()

Default constructor. Leaves component values uninitialized.

 


 

Color(int r, int g, int b)

Constructor.

r

Red channel.

g

Green channel.

b

Blue channel.

 


 

Color(const Nuller&)

Null constructor.

 


 

Color(const Value& q)

Conversion from Value.

q

Value.

 


 

Color(Color (*fn)())

[deprecated]

This constructor allows using a pointer to a function returning Colors instead of Colors in many places. Global functions returning Color are needed instead of global Color constants on many platforms, because often global variables are not allowed when using dynamic libraries.

fn

Pointer to a function returning Color.

 


 

Color(RGBA rgba)

Conversion from a RGBA structure rgba.

 

 

Public Member List

 

operator Value() const

Conversion to Value.

Return value

Value.

 


 

dword GetRaw() const

Return value

Platform specific dword representing the color.

 


 

int GetR() const

Return value

The red component.

 


 

int GetG() const

Return value

The green component.

 


 

int GetB() const

Return value

The blue component.

 


 

void SetNull()

Sets the Color to Null.

 


 

bool IsNullInstance() const

Return value

True if Color is Null. Used to implement IsNull for Color.

 


 

unsigned GetHashValue() const

Return value

Hash-value for Color.

 


 

bool operator==(Color cconst

c

Color to compare with.

Return value

True if Color is equal to c.

 


 

bool operator!=(Color cconst

c

Color to compare with.

Return value

True if Color is not equal to c.

 


 

void Serialize(Stream& s)

Serializes the color to the Stream s.

 


 

static Color FromRaw(dword co)

Creates a Color based on a platform specific raw dword value.

co

Raw dword value.

Return value

Color.

 


 

static Color Special(int n)

Creates special color n. Special colors are application defined, sort of "escape" color in situation where something else has to be stored in Color, e.g. index of some color palette.

 


 

int GetSpecial() const

Returns special color value or -1 if Color is not special.

 


 

operator COLORREF() const

Win32 specific.

Return COLORREF for Color.

Return value

COLORREF.

 


 

static Color FromCR(COLORREF cr)

Win32 specific.

Creates Color from COLORREF.

cr

COLORREF.

Return value

Color.

 


 

operator dword() const

Not available on Win32

Return value

GetRaw().

 


 

operator RGBA() const

Returns a RGBA structure containing the color information.

 

 

 

 

Utility functions

 

int GetRValue(dword c)

Returns red component from a platform specific value.

c

Platform specific value.

Return value

Red component.

 


 

int GetGValue(dword c)

Returns green component from a platform specific value.

c

Platform specific value.

Return value

Green component.

 


 

int GetBValue(dword c)

Returns blue component from a platform specific value.

c

Platform specific value.

Return value

Blue component.

 


 

dword RGB(byte r, byte g, byte b)

Calculates platform specific value from components.

r

Red component.

g

Green component.

b

Blue component.

Return value

Platform specific value.

 


 

unsigned GetHashValue(Color c)

Returns a hash value for the color.

c

Color.

Return value

Hash value.

 


 

Color Nvl(Color a, Color b)

Returns first parameter if it is not Null, second otherwise.

a

Color.

b

Color.

Return value

a if not Null, b otherwise.

 


 

template <> String AsString(const Color& c)

Converts Color to textual form.

c

Color.

Return value

Textual form.

 


 

void RGBtoHSV(double r, double g, double b, double& h, double& s, double& v)

Transforms an [R,G,B] color triplet (each of the components is supposed to be in the range [0..1]) into an [H,S,V] triplet (again all components are in the range [0..1]). This effectively transforms the RGB cube to a cone in which color selection more intuitively matches our our visual perception (hue corresponds to the tint of the color, saturation to the richness of the color hue and value corresponds to luminance or brightness).

 

The mathematical formula used to convert between the two color spaces are:

 

v = max(r, g, b)

s = 1 - min(r, g, b) / v

h = (x - min(r, g, b)) / (max(r, g, b) - min(r, g, b))

 

where x is the color component which is neither minimum nor maximum. There are six combinations for this, in every even h is taken, in every odd 1-h. The six combinations correspond to six points on the color wheel forming the base of the HSV cone - pure red, yellow, green, cyan, blue, and magenta. The color component ranges along the circumference are the following (capital letters denote maximum component value, minuscule letters minimum): Rgb -> RGb -> rGb -> rGB -> rgB -> RgB.

 

An easy way to visualize the HSV transform based on the RGB cube is: imagine a slight deformation of the cube where the tip of the cone corresponds to the black RBG cube vertex, the opposite (white) RGB cube vertex corresponds to the center of the base circle of the HSV cone and the remaining six RGB cube vertices get projected on the circumference of the cone base as explained in the preceding paragraph.

 


 

void HSVtoRGB(double h, double s, double v, double& r, double& g, double& b)

Transformes an HSV triplet (each of the components is supposed to lie in the interval [0..1]) into an RGB triplet (again with component ranges [0..1]). This projects the HSV cone (as explained in the preceding section) back onto the RGB cube.

 


 

Color HsvColorf(double h, double s, double v)

Converts an HSV color triplet into a Color object. The H, S, and V components are supposed to be in the range [0..1].

 


 

void CMYKtoRGB(double c, double m, double y, double k, double& r, double& g, double& b)

Converts CMYK color definition to RGB (all components are in [0..1] range).

 


 

void RGBtoCMYK(double r, double g, double b, double& c, double& m, double& y, double& k)

Converts RGB color definition to CMYK (all components are in [0..1] range).

 


 

Color CmykColorf(double c, double m, double y, double k)

Converts CMYK color definition to RGB (all components are in [0..1] range).

 


 

Color CmyColorf(double c, double m, double y)

Converts CMY color definition to RGB (all components are in [0..1] range).

 


 

Color Blend(Color c1, Color c2, int alpha = 128)

Blends two colors.

c1

First color.

c2

Second color.

alpha

Blending factor in the range 0..255.

Return value

Blended color - (255 - alpha) / 255.0 * c1 + alpha / 255.0 * c2.

 


 

String ColorToHtml(Color color)

Converts Color to the textual format used in HTML (into hexadecimal form like #ffffff for white).

color

Color.

Return value

HTML text.

 


 

Color ColorFromText(const char *s)

Converts text to Color. Allowed formats are 3 decimal numbers in range 0-255, representing r, g, b colors and separated by ',' or ';' or '.' or ' ', or the text must exactly contain 6 hexadecimal digits representing RRGGBB values.

 


 

Color GrayColor(int a = 128)

Returns a gray color of intensity a.

 


 

int Grayscale(const Color& c)

Converts a color c to grayscale.

 


 

bool IsDark(Color c)

True if c grayscale level is lower than 80.

 


 

bool IsLight(Color c)

True if c grayscale is higher than 175 (255 - 80).

 


 

Color DarkTheme(Color c)

'Dark Theme conversion'. Converts dark colors to light ones and vice versa, sustaining the color hue.

 


 

Color DarkThemeCached(Color c)

Cached variant of DarkTheme, caches up to 4 repeating colors.

 


 

Color StraightColor(RGBA rgba)

Converts a RGBA structure rgba to a Color.

 

 

Color constants

 

Predefined colors are represented by functions that return the predefined color value.

 

 

Color Black()

Black.

 


 

Color Gray()

Gray.

 


 

Color LtGray()

Light gray.

 


 

Color WhiteGray()

White gray.

 


 

Color White()

White.

 


 

Color Red()

Red.

 


 

Color Green()

Green.

 


 

Color Brown()

Brown.

 


 

Color Blue()

Blue.

 


 

Color Magenta()

Magenta.

 


 

Color Cyan()

Cyan.

 


 

Color Yellow()

Yellow.

 


 

Color LtRed()

Light red.

 


 

Color LtGreen()

Light green.

 


 

Color LtYellow()

Light yellow.

 


 

Color LtBlue()

Light blue.

 


 

Color LtMagenta()

Light magenta.

 


 

Color LtCyan()

Light cyan.

 

 

Do you want to contribute?