3es  0.7
tes::Colour Class Reference

A 32-bit integer colour class. More...

#include <3escolour.h>

Public Types

enum  Channels { A = 0, B = 1, G = 2, R = 3 }
 Channel index enumeration. More...
 
enum  Predefined {
  Gainsboro, LightGrey, Silver, DarkGrey,
  Grey, DimGrey, LightSlateGrey, SlateGrey,
  DarkSlateGrey, Black, White, Snow,
  Honeydew, MintCream, Azure, AliceBlue,
  GhostWhite, WhiteSmoke, Seashell, Beige,
  OldLace, FloralWhite, Ivory, AntiqueWhite,
  Linen, LavenderBlush, MistyRose, Pink,
  LightPink, HotPink, DeepPink, PaleVioletRed,
  MediumVioletRed, LightSalmon, Salmon, DarkSalmon,
  LightCoral, IndianRed, Crimson, FireBrick,
  DarkRed, Red, OrangeRed, Tomato,
  Coral, DarkOrange, Orange, Yellow,
  LightYellow, LemonChiffon, LightGoldenrodYellow, PapayaWhip,
  Moccasin, PeachPuff, PaleGoldenrod, Khaki,
  DarkKhaki, Gold, Cornsilk, BlanchedAlmond,
  Bisque, NavajoWhite, Wheat, BurlyWood,
  Tan, RosyBrown, SandyBrown, Goldenrod,
  DarkGoldenrod, Peru, Chocolate, SaddleBrown,
  Sienna, Brown, Maroon, DarkOliveGreen,
  Olive, OliveDrab, YellowGreen, LimeGreen,
  Lime, LawnGreen, Chartreuse, GreenYellow,
  SpringGreen, MediumSpringGreen, LightGreen, PaleGreen,
  DarkSeaGreen, MediumSeaGreen, SeaGreen, ForestGreen,
  Green, DarkGreen, MediumAquamarine, Aqua,
  Cyan, LightCyan, PaleTurquoise, Aquamarine,
  Turquoise, MediumTurquoise, DarkTurquoise, LightSeaGreen,
  CadetBlue, DarkCyan, Teal, LightSteelBlue,
  PowderBlue, LightBlue, SkyBlue, LightSkyBlue,
  DeepSkyBlue, DodgerBlue, CornflowerBlue, SteelBlue,
  RoyalBlue, Blue, MediumBlue, DarkBlue,
  Navy, MidnightBlue, Lavender, Thistle,
  Plum, Violet, Orchid, Fuchsia,
  Magenta, MediumOrchid, MediumPurple, BlueViolet,
  DarkViolet, DarkOrchid, DarkMagenta, Purple,
  Indigo, DarkSlateBlue, SlateBlue, MediumSlateBlue,
  PredefinedCount
}
 Enumerates a set of predefined colours ("web safe" colours).
 
enum  ColourCycle {
  StandardCycle, DeuteranomalyCycle, ProtanomalyCycle, TritanomalyCycle,
  GreyCycle, CycleCount
}
 Enumerates the various available colour cycles. More...
 

Public Member Functions

 Colour (uint32_t c=0xffffffffu)
 Construct a colour with the given numeric value. More...
 
 Colour (const Colour &other)
 Copy constructor. More...
 
 Colour (const Colour &other, uint8_t a)
 Partial copy constructor with new alpha value. More...
 
 Colour (const Colour &other, int a)
 Partial copy constructor with new alpha value. More...
 
 Colour (const Colour &other, float a)
 Partial copy constructor with new alpha value. More...
 
 Colour (uint8_t r, uint8_t g, uint8_t b, uint8_t a=255u)
 Explicit byte based RGBA colour channel initialisation constructor. More...
 
 Colour (int r, int g, int b, int a=255)
 Integer based RGBA colour channel initialisation constructor. More...
 
 Colour (float r, float g, float b, float a=1.0f)
 Floating point RGBA colour channel initialisation constructor. More...
 
float rf () const
 Get red channel in floating point form. More...
 
float gf () const
 Get green channel in floating point form. More...
 
float bf () const
 Get blue channel in floating point form. More...
 
float af () const
 Get alpha channel in floating point form. More...
 
void setRf (float f)
 Set red channel from a floating point value. More...
 
void setGf (float f)
 Set green channel from a floating point value. More...
 
void setBf (float f)
 Set blue channel from a floating point value. More...
 
void setAf (float f)
 Set alpha channel from a floating point value. More...
 
void setf (float f, int index)
 Set a channel in floating point form. More...
 
float getf (int index) const
 Get a channel in floating point form. More...
 
Colour adjust (float factor) const
 Lighten or darken a colour by factor. More...
 
Colour lighten () const
 Lighten the colour by 1.5. More...
 
Colour darken () const
 Darken the colour by 0.5. More...
 
Colouroperator= (const Colour &other)
 Assignment operator. More...
 
bool operator== (const Colour &other) const
 Precise equality operator. More...
 
bool operator!= (const Colour &other) const
 Precise inequality operator. More...
 

Static Public Member Functions

static void rgbToHsv (float &h, float &s, float &v, const float r, const float g, const float b)
 Convert RGB to HSV form. More...
 
static void hsvToRgb (float &r, float &g, float &b, const float h, const float s, const float v)
 Convert HSV to RGB form. More...
 
static void hsvToRgb (uint8_t &r, uint8_t &g, uint8_t &b, const float h, const float s, const float v)
 Convert HSV to RGB form. More...
 
static const Colourcycle (unsigned number, ColourCycle cycle=StandardCycle)
 A utility function for colour cycling. More...
 

Public Attributes

union {
   uint32_t   c
 Encoded colour value.
 
   struct {
      uint8_t   a
 Alpha channel.
 
      uint8_t   b
 Blue channel.
 
      uint8_t   g
 Green channel.
 
      uint8_t   r
 Red channel.
 
   } 
 
   uint8_t   rgba [4]
 Indexed channels.
 
}; 
 

Static Public Attributes

static const Colour Colours [PredefinedCount]
 The set of colours matching the Predefined enumeration.
 
static const int * ColourCycles [CycleCount]
 A set of colours which can be cycled to highlight components. More...
 
static const int CycleCounts [CycleCount]
 Number of colours in each ColourCycles entry.
 

Detailed Description

A 32-bit integer colour class.

Storage is designed to allow colours to be written as unsigned hexadecimal integers as 0xRRGGBBAA regardless of the target Endian.

Member Enumeration Documentation

◆ Channels

Channel index enumeration.

Enumerator

Alpha channel index.

Blue channel index.

Green channel index.

Red channel index.

◆ ColourCycle

Enumerates the various available colour cycles.

Note: the colours cycles include sets which attempt to cater for various forms of colour blindness. These are not rigorously constructed and may not be as well suited as they are intended. Feel free to offer suggested improvements to these colours sets.

See also
colourCycle()
Enumerator
StandardCycle 

Standard colour set.

DeuteranomalyCycle 

A colour set which attempts to cater for Deuteranomaly colour blindness.

ProtanomalyCycle 

A colour set which attempts to cater for Protanomaly colour blindness.

TritanomalyCycle 

A colour set which attempts to cater for Tritanomaly colour blindness.

GreyCycle 

A small grey scale colour set.

CycleCount 

Defines the number of available colour sets.

Constructor & Destructor Documentation

◆ Colour() [1/8]

tes::Colour::Colour ( uint32_t  c = 0xffffffffu)
inline

Construct a colour with the given numeric value.

Parameters
cThe integer colour representation: 0xRRGGBBAA.

◆ Colour() [2/8]

tes::Colour::Colour ( const Colour other)
inline

Copy constructor.

Parameters
otherThe colour to copy.

◆ Colour() [3/8]

tes::Colour::Colour ( const Colour other,
uint8_t  a 
)
inline

Partial copy constructor with new alpha value.

Parameters
otherThe colour to copy RGB channels from.
aThe new alpha channel value.

References a.

◆ Colour() [4/8]

tes::Colour::Colour ( const Colour other,
int  a 
)
inline

Partial copy constructor with new alpha value.

Parameters
otherThe colour to copy RGB channels from.
aThe new alpha channel value.

References a.

◆ Colour() [5/8]

tes::Colour::Colour ( const Colour other,
float  a 
)
inline

Partial copy constructor with new alpha value.

Parameters
otherThe colour to copy RGB channels from.
aThe new alpha channel value.

References setAf().

◆ Colour() [6/8]

tes::Colour::Colour ( uint8_t  r,
uint8_t  g,
uint8_t  b,
uint8_t  a = 255u 
)
inlineexplicit

Explicit byte based RGBA colour channel initialisation constructor.

Parameters
rRed channel value [0, 255].
gRed channel value [0, 255].
bRed channel value [0, 255].
aRed channel value [0, 255].

◆ Colour() [7/8]

tes::Colour::Colour ( int  r,
int  g,
int  b,
int  a = 255 
)
inline

Integer based RGBA colour channel initialisation constructor.

Parameters
rRed channel value [0, 255].
gRed channel value [0, 255].
bRed channel value [0, 255].
aRed channel value [0, 255].

◆ Colour() [8/8]

tes::Colour::Colour ( float  r,
float  g,
float  b,
float  a = 1.0f 
)
inline

Floating point RGBA colour channel initialisation constructor.

Parameters
rRed channel value [0, 1].
gRed channel value [0, 1].
bRed channel value [0, 1].
aRed channel value [0, 1].

References setAf(), setBf(), setGf(), and setRf().

Member Function Documentation

◆ adjust()

Colour tes::Colour::adjust ( float  factor) const

Lighten or darken a colour by factor.

Works in HSV space, multiplying the V value by factor and clamping the result [0, 1].

Returns
The adjusted colour.

◆ af()

float tes::Colour::af ( ) const
inline

Get alpha channel in floating point form.

Returns
Alpha channel [0, 1].

References getf().

Referenced by tes::toVector().

◆ bf()

float tes::Colour::bf ( ) const
inline

Get blue channel in floating point form.

Returns
Blue channel [0, 1].

References getf().

Referenced by tes::toVector().

◆ cycle()

static const Colour& tes::Colour::cycle ( unsigned  number,
ColourCycle  cycle = StandardCycle 
)
static

A utility function for colour cycling.

This function returns a colour from the ColourCycle, resolved to its actual Colours element. The intended use is to periodically call this function with a monotonic, increasing number value. This is then correctly clamped to the ColourCycle range.

Parameters
numberAny numeric value. It is clamped and wrapped to be in range (using modulus).
cycleThe colour cycle to request a colour from.
Returns
A colour from the cycle.

◆ darken()

Colour tes::Colour::darken ( ) const
inline

Darken the colour by 0.5.

Returns
A darker colour.

◆ getf()

float tes::Colour::getf ( int  index) const
inline

Get a channel in floating point form.

Parameters
indexThe target channel [0, 3]. Best to use Channels.
Returns
The channel value [0, 1].

References rgba.

Referenced by af(), bf(), gf(), and rf().

◆ gf()

float tes::Colour::gf ( ) const
inline

Get green channel in floating point form.

Returns
Green channel [0, 1].

References getf().

Referenced by tes::toVector().

◆ hsvToRgb() [1/2]

static void tes::Colour::hsvToRgb ( float &  r,
float &  g,
float &  b,
const float  h,
const float  s,
const float  v 
)
static

Convert HSV to RGB form.

Parameters
[out]rRed channel [0, 1].
[out]gGreen channel [0, 1].
[out]bBlue channel [0, 1].
hThe hue value [0, 360].
sThe saturation value [0, 1].
vThe colour value [0, 1].

◆ hsvToRgb() [2/2]

static void tes::Colour::hsvToRgb ( uint8_t &  r,
uint8_t &  g,
uint8_t &  b,
const float  h,
const float  s,
const float  v 
)
static

Convert HSV to RGB form.

Parameters
[out]rRed channel [0, 255].
[out]gGreen channel [0, 255].
[out]bBlue channel [0, 255].
hThe hue value [0, 360].
sThe saturation value [0, 1].
vThe colour value [0, 1].

◆ lighten()

Colour tes::Colour::lighten ( ) const
inline

Lighten the colour by 1.5.

Returns
A lighter colour.

◆ operator!=()

bool tes::Colour::operator!= ( const Colour other) const
inline

Precise inequality operator.

Parameters
otherThe colour to compare to.
Returns
True if this colour is not precisely equal to other.

References c.

◆ operator=()

Colour & tes::Colour::operator= ( const Colour other)
inline

Assignment operator.

Parameters
otherThe colour value to assign.
Returns
this.

References c.

◆ operator==()

bool tes::Colour::operator== ( const Colour other) const
inline

Precise equality operator.

Parameters
otherThe colour to compare to.
Returns
True if this colour is precisely equal to other.

References c.

◆ rf()

float tes::Colour::rf ( ) const
inline

Get red channel in floating point form.

Returns
Red channel [0, 1].

References getf().

Referenced by tes::toVector().

◆ rgbToHsv()

static void tes::Colour::rgbToHsv ( float &  h,
float &  s,
float &  v,
const float  r,
const float  g,
const float  b 
)
static

Convert RGB to HSV form.

Parameters
[out]hThe hue value [0, 360].
[out]sThe saturation value [0, 1].
[out]vThe colour value [0, 1].
rRed channel.
gGreen channel.
bBlue channel.

◆ setAf()

void tes::Colour::setAf ( float  f)
inline

Set alpha channel from a floating point value.

Parameters
fChannel value [0, 1].

References setf().

Referenced by Colour(), and tes::toColour().

◆ setBf()

void tes::Colour::setBf ( float  f)
inline

Set blue channel from a floating point value.

Parameters
fChannel value [0, 1].

References setf().

Referenced by Colour(), and tes::toColour().

◆ setf()

void tes::Colour::setf ( float  f,
int  index 
)
inline

Set a channel in floating point form.

Parameters
fChannel value [0, 1].
indexThe target channel [0, 3]. Best to use Channels.

References rgba.

Referenced by setAf(), setBf(), setGf(), and setRf().

◆ setGf()

void tes::Colour::setGf ( float  f)
inline

Set green channel from a floating point value.

Parameters
fChannel value [0, 1].

References setf().

Referenced by Colour(), and tes::toColour().

◆ setRf()

void tes::Colour::setRf ( float  f)
inline

Set red channel from a floating point value.

Parameters
fChannel value [0, 1].

References setf().

Referenced by Colour(), and tes::toColour().

Member Data Documentation

◆ ColourCycles

const int* tes::Colour::ColourCycles[CycleCount]
static

A set of colours which can be cycled to highlight components.

The colour set is initialised to try and provide sufficient contrast between each colour. Each element should be indexed into Colours.


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