< 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 :

color | red | orange | yellow | chartreuse | green | sea | cyan | sky | blue | violet | magenta | pink
level |
desaturated |  |  |  |  |  |  |  |  |  |  |  |
light |  |  |  |  |  |  |  |  |  |  |  |
normal |  |  |  |  |  |  |  |  |  |  |  |
dark |  |  |  |  |  |  |  |  |  |  |  |
darker |  |  |  |  |  |  |  |  |  |  |  |

Or grey levels :

| black | darker grey | dark grey | grey | light grey | white

    C++ : TCODColor::desaturatedRed
          TCODColor::lightRed
          TCODColor::red
          TCODColor::darkRed
          TCODColor::darkerRed
    C   : TCOD_desaturated_red
          TCOD_light_red
          TCOD_red
          TCOD_dark_red
          TCOD_darker_red
    Py  : libtcod.desaturated_red
          libtcod.light_red
          libtcod.red
          libtcod.dark_red
          libtcod.darker_red

or define your own :

    C++ : TCODColor myColor(24,64,255);
    C   : TCOD_color_t my_color={24,64,255};
    Py  : my_color=libtcod.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 ) ) { ... }
    Py :
      if my_color ==  libtcod.yellow : ...

      if my_color != litbcod.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);
    Py  : my_dark_red =
    libtcod.dark_grey *
    libtcod.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);
    Py  : myDarkRed =
    litbcod.light_red *
    0.5

      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);
    Py  : myRed =
    libtcod.light_red +
    libtcod.dark_grey

      c1 = c1 + c2 => c1.r = MIN(255, c1.r + c2.r)

                      c1.g = MIN(255, c1.g + c2.g)

                      c1.b = MIN(255, c1.b + c2.b)

or subtract two colors :

    C++ : TCODColor myRed =
    TCODColor::lightRed -
    TCODColor::darkGrey
    C   : TCOD_color_t my_red = TCOD_color_subtract(
    TCOD_light_red,
    TCOD_dark_grey);
    Py  : myRed =
    libtcod.light_red -
    libtcod.dark_grey

      c1 = c1 - c2 => c1.r = MAX(0, c1.r - c2.r)

                      c1.g = MAX(0, c1.g - c2.g)

                      c1.b = MAX(0, 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)

    Py  : color_lerp(a, b, 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);
    Py  : my_color = libtcod.color_lerp (
    libtcod.dark_grey,
    litbcod.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)
    Py  : color_set_HSV(c,h,s,v)

Parameter | Description
--- | ---
c | In the C version, the TCOD_color_t to modify.
h,s,v | Color 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)
    Py  : color_get_HSV(c) # returns [h,s,v]

Parameter | Description
--- | ---
c | In the C version, the TCOD_color_t from which to read.
h,s,v | Color 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)
    Py  : color_gen_map(keyColor,keyIndex) # returns an array of colors

Parameter | Description
--- | ---
map | An array of colors to be filled by the function.
nbKey | Number of color keys
keyColor | Array of nbKey colors containing the color of each key
keyIndex | Array 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 :

    C++ :
    int idx[] = { 0, 4, 8 }; /* indexes of the keys */
    TCODColor col[] = { TCODColor( 0,0,0 ), TCODColor(255,0,0), TCODColor(255,255,255) }; // colors : black, red, white
    TCODColor map[9];
    TCODColor::genMap(map,3,col,idx);
    C   :
    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);
    Py  :
    idx = [ 0, 4, 8 ] # indexes of the keys
    col = [ libtcod.Color( 0,0,0 ), libtcod.Color( 255,0,0 ), libtcod.Color(255,255,255) ] # colors : black, red, white
    map=libtcod.color_gen_map(col,idx)

Result :

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