Colors

Note

These docs are incomplete. Several functions and features are better documented by the older 1.6.4 docs which you can find here.

libtcod uses 32-bit color, therefore your OS desktop must also use 32-bit color. A color is defined by its red, green and blue component between 0 and 255.

You can use the following predefined colors (hover over a color to see its full name and R,G,B values):

INSERT COLOUR TABLE IN A PAINLESS MANNER

Create your own colors

You can create your own colours using a set of constructors, both for RGB and HSV values.

/* RGB */
TCOD_color_t my_color= { 24, 64, 255 };
TCOD_color_t my_other_color = TCOD_color_RGB(24, 64, 255);
/* HSV */
TCOD_color_t my_yet_another_color = TCOD_color_HSV(321.0f, 0.7f, 1.0f);
// RGB
TCODColor myColor(24, 64, 255);
// HSV
TCODColor myOtherColor(321.0f, 0.7f, 1.0f);
struct ColorRGB : public TCOD_ColorRGB

A C++ RGB color, used to handle conversions between color types.

New in version 1.19.

Public Functions

inline constexpr ColorRGB() noexcept

Default construct a black ColorRGB object.

RGB values are zero.

inline constexpr ColorRGB(uint8_t red, uint8_t green, uint8_t blue) noexcept

Construct a ColorRGB object with the provided color.

inline explicit constexpr ColorRGB(const TCOD_ColorRGB &rhs) noexcept

Construct a ColorRGB object from an TCOD_ColorRGB struct.

inline explicit constexpr ColorRGB(const TCOD_ColorRGBA &rhs) noexcept

Construct a ColorRGB object from an RGBA color, truncating the alpha.

inline constexpr operator const TCOD_ColorRGBA() const noexcept

Allow implicit casts to RGBA colors, where alpha=255 is implied.

inline explicit constexpr operator TCOD_ColorRGB*() noexcept

Allow explicit casts to a TCOD_ColorRGB pointer.

inline explicit constexpr operator const TCOD_ColorRGB*() const noexcept

Allow explicit casts to a const TCOD_ColorRGB pointer.

struct ColorRGBA : public TCOD_ColorRGBA

A C++ RGBA color, used to handle conversions between color types.

New in version 1.19.

Public Functions

inline constexpr ColorRGBA() noexcept

Default construct a black ColorRGBA object.

RGB values are zero, alpha is 255.

inline constexpr ColorRGBA(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255) noexcept

Construct a ColorRGBA object with the provided color and alpha.

inline explicit constexpr ColorRGBA(const TCOD_ColorRGB &rhs, uint8_t alpha = 255) noexcept

Construct a ColorRGBA object by adding an alpha channel to an RGB object.

inline explicit constexpr ColorRGBA(const TCOD_ColorRGBA &rhs) noexcept

Construct a ColorRGBA object from an TCOD_ColorRGBA struct.

inline explicit constexpr operator TCOD_ColorRGB() const noexcept

Allow explicit conversions to a TCOD_ColorRGB struct.

inline explicit constexpr operator TCOD_ColorRGBA*() noexcept

Allow explicit conversions to a TCOD_ColorRGBA pointer.

inline explicit constexpr operator const TCOD_ColorRGBA*() const noexcept

Allow explicit conversions to a const TCOD_ColorRGBA pointer.

Compare two colors

bool TCOD_color_equals(TCOD_color_t c1, TCOD_color_t c2)

Add and subtract Colors

TCOD_color_t TCOD_color_add(TCOD_color_t c1, TCOD_color_t c2)
TCOD_color_t TCOD_color_subtract(TCOD_color_t c1, TCOD_color_t c2)

Multiply Colors together

TCOD_color_t TCOD_color_multiply(TCOD_color_t c1, TCOD_color_t c2)
TCOD_color_t TCOD_color_multiply_scalar(TCOD_color_t c1, float value)

Interpolate between two colors

TCOD_color_t TCOD_color_lerp(TCOD_color_t c1, TCOD_color_t c2, float coef)

Define a color by its hue, saturation and value

After this function is called, the r,g,b fields of the color are calculated according to the h,s,v parameters.

void TCOD_color_set_HSV(TCOD_color_t *color, float hue, float saturation, float value)

These functions set only a single component in the HSV color space.

void TCOD_color_set_hue(TCOD_color_t *color, float hue)
void TCOD_color_set_saturation(TCOD_color_t *color, float saturation)
void TCOD_color_set_value(TCOD_color_t *color, float value)

Get a color hue, saturation and value components

void TCOD_color_get_HSV(TCOD_color_t color, float *hue, float *saturation, float *value)

Should you need to extract only one of the HSV components, these functions are what you should call. Note that if you need all three values, it’s way less burdensome for the CPU to call TCODColor::getHSV().

float TCOD_color_get_hue(TCOD_color_t color)
float TCOD_color_get_saturation(TCOD_color_t color)
float TCOD_color_get_value(TCOD_color_t color)

Shift a color’s hue up or down

The hue shift value is the number of grades the color’s hue will be shifted. The value can be negative for shift left, or positive for shift right. Resulting values H < 0 and H >= 360 are handled automatically.

void TCOD_color_shift_hue(TCOD_color_t *color, float shift)

Scale a color’s saturation and value

void TCOD_color_scale_HSV(TCOD_color_t *color, float saturation_coef, float value_coef)

Generate a smooth color map

You can define a color map from an array of color keys. Colors will be interpolated between the keys. 0 -> black 4 -> red 8 -> white Result:

INSERT TABLE.

void TCOD_color_gen_map(TCOD_color_t *map, int nb_key, const TCOD_color_t *key_color, const int *key_index)