< index
< 0. Compiling with libtcod

=====================================
1. Colors
=====================================

> 2. Console emulator
1.1 Predefined colors
1.2 User defined colors
1.3 Comparing colors
1.4 Colors operators
1.5 Interpolating colors
1.6 HSV conversions
1.7 Color map generation


Note that the Doryen library uses 32bits colors. Thus, your OS desktop must use 32bits colors.
A color is defined by its red, green and blue component between 0 and 255. You can use predefined colors :
C++Ccolor
TCODColor TCODColor::black(0,0,0);
TCOD_color_t TCOD_black={0,0,0};
TCODColor TCODColor::darkGrey(96,96,96);
TCOD_color_t TCOD_dark_grey={96,96,96};
TCODColor TCODColor::grey(196,196,196);
TCOD_color_t TCOD_grey={196,196,196};
TCODColor TCODColor::white(255,255,255);
TCOD_color_t TCOD_white={255,255,255};
TCODColor TCODColor::darkBlue(40,40,128);
TCOD_color_t TCOD_dark_blue={40,40,128};
TCODColor TCODColor::lightBlue(120,120,255);
TCOD_color_t TCOD_light_blue={120,120,255};
TCODColor TCODColor::darkRed(128,0,0);
TCOD_color_t TCOD_dark_red={128,0,0};
TCODColor TCODColor::lightRed(255,100,50);
TCOD_color_t TCOD_light_red={255,100,50};
TCODColor TCODColor::darkBrown(32,16,0);
TCOD_color_t TCOD_dark_brown={32,16,0};
TCODColor TCODColor::lightYellow(255,255,150);
TCOD_color_t TCOD_light_yellow={255,255,150};
TCODColor TCODColor::yellow(255,255,0);
TCOD_color_t TCOD_yellow={255,255,0};
TCODColor TCODColor::darkYellow(164,164,0);
TCOD_color_t TCOD_dark_yellow={164,164,0};
TCODColor TCODColor::green(0,220,0);
TCOD_color_t TCOD_green={0,220,0};
TCODColor TCODColor::orange(255,150,0);
TCOD_color_t TCOD_orange={255,150,0};
TCODColor TCODColor::red(255,0,0);
TCOD_color_t TCOD_red={255,0,0};
TCODColor TCODColor::silver(203,203,203);
TCOD_color_t TCOD_silver={203,203,203};
TCODColor TCODColor::gold(255,255,102);
TCOD_color_t TCOD_gold={255,255,102};
TCODColor TCODColor::purple(204,51,153);
TCOD_color_t TCOD_purple={204,51,153};
TCODColor TCODColor::darkPurple(51,0,51);
TCOD_color_t TCOD_dark_purple={51,0,51};
or define your own :

C++ : TCODColor myColor(24,64,255);

C   : TCOD_color_t my_color={24,64,255};


You can compare two colors :

C++ :
  if (myColor == TCODColor::yellow ) { ... }
if (myColor != TCODColor::white ) { ... } C : if ( TCOD_color_equals(my_color, TCOD_yellow ) ) { ... }
if ( ! TCOD_color_equals(my_color, TCOD_white ) ) { ... }



You can multiply two colors :
C++ : TCODColor myDarkRed = 
TCODColor::darkGrey *
TCODColor::lightRed;
C   : TCOD_color_t my_dark_red = TCOD_color_multiply(
TCOD_dark_grey,
TCOD_light_red);

  c1 = c2 * c3 => c1.r = c2.r * c3.r /255
c1.g = c2.g * c3.g /255
c1.b = c2.b * c3.b /255

or multiply a color by a float :
C++ : TCODColor myDarkRed = 
TCODColor::lightRed *
0.5f;
C   : TCOD_color_t my_dark_red = TCOD_color_multiply_scalar(
TCOD_light_red,
0.5f);

  c1 = c2 * v => c1.r = CLAMP(0, 255, c2.r * v)
c1.g = CLAMP(0, 255, c2.g * v)
c1.b = CLAMP(0, 255, c2.b * v)

or add two colors :
C++ : TCODColor myRed = 
TCODColor::lightRed +
TCODColor::darkGrey
C   : TCOD_color_t my_red = TCOD_color_add(
TCOD_light_red,
TCOD_dark_grey);

  c1 = c1 + c2 => c1.r = CLAMP(0, 255, c1.r + c2.r)
c1.g = CLAMP(0, 255, c1.g + c2.g)
c1.b = CLAMP(0, 255, c1.b + c2.b)


You can interpolate between two colors with

C++ : static TCODColor TCODColor::lerp(TCODColor a, TCODColor b, float coef)

C   : TCOD_color_t TCOD_color_lerp(TCOD_color_t a, TCOD_color_t b, float coef)

C++ : TCODColor myColor = TCODColor::lerp ( 
TCODColor::darkGrey,
TCODColor::lightRed,coef);
C   : TCOD_color_t my_color = TCOD_color_lerp ( 
TCOD_dark_grey,
TCOD_light_red,coef);
coef == 0.0f
coef == 0.25f
coef == 0.5f
coef == 0.75f
coef == 1.0f

  c1 = lerp (c2, c3, coef) => c1.r = c2.r  + (c3.r - c2.r ) * coef
c1.g = c2.g + (c3.g - c2.g ) * coef
c1.b = c2.b + (c3.b - c2.b ) * coef

coef should be between 0.0 and 1.0 but you can as well use other values


You can define a color from its hue, saturation, value components :

C++ : void TCODColor::setHSV(float h, float s, float v)
C   : void TCOD_color_set_HSV(TCOD_color_t *c,float h, float s, float v)

ParameterDescription
cIn the C version, the TCOD_color_t to modify.
h,s,vColor components in the HSV space
0.0 <= h < 360.0
0.0 <= s <= 1.0
0.0 <= v <= 1.0
After this function is called, the r,g,b fields of the color contains the red, green, blue values corresponding to the h,s,v parameters.

You can get the HSV components of a color :

C++ : void TCODColor::getHSV(float *h, float *s, float *v) const
C   : void TCOD_color_get_HSV(TCOD_color_t c,float * h, float * s, float * v)

ParameterDescription
cIn the C version, the TCOD_color_t from which to read.
h,s,vColor components in the HSV space
0.0 <= h < 360.0
0.0 <= s <= 1.0
0.0 <= v <= 1.0


You can define a color map from an array of color keys. Colors will be interpolated between the keys.

C++ : static void genMap(TCODColor *map, int nbKey, TCODColor const *keyColor, int const *keyIndex)
C   : void TCOD_gen_map(TCOD_color_t *map, int nb_key, TCOD_color_t const *key_color, int const *key_index)

ParameterDescription
mapAn array of colors to be filled by the function.
nbKeyNumber of color keys
keyColorArray of nbKey colors containing the color of each key
keyIndexArray of nbKey integers containing the index of each key.
If you want to fill the map array, keyIndex[0] must be 0 and keyIndex[nbKey-1] is the number of elements in map minus 1 but you can also use the function to fill only a part of the map array.

Example :

int idx[] = { 0, 4, 8 }; /* indexes of the keys */
TCOD_color_t col[] = { { 0,0,0 }, {255,0,0}, {255,255,255} }; /* colors : black, red, white */
TCOD_color_t map[9];
TCOD_color_gen_map(map,3,col,idx);

Result :
map[0]
 black
map[1]
 
map[2]
 
map[3]
 
map[4]
 red
map[5]
 
map[6]
 
map[7]
 
map[8]
 white