ELinks 0.18.0
color.c File Reference

Terminal color composing. More...

#include "elinks.h"
#include "terminal/color.h"
#include "terminal/draw.h"
#include "util/color.h"
#include "util/error.h"
#include "terminal/palette.inc"
Include dependency graph for color.c:

Data Structures

struct  rgb
 
struct  rgb_cache_entry
 
struct  color_mode_info
 

Macros

#define RED_COLOR_MASK   0x00FF0000
 
#define GREEN_COLOR_MASK   0x0000FF00
 
#define BLUE_COLOR_MASK   0x000000FF
 
#define RED_COLOR(color)   (unsigned char)(((color) & RED_COLOR_MASK) >> 16)
 
#define GREEN_COLOR(color)   (unsigned char)(((color) & GREEN_COLOR_MASK) >> 8)
 
#define BLUE_COLOR(color)   (unsigned char)(((color) & BLUE_COLOR_MASK) >> 0)
 
#define RED(color)   (RED_COLOR(color) << 3)
 
#define GREEN(color)   (GREEN_COLOR(color) << 2)
 
#define BLUE(color)   (BLUE_COLOR(color) << 0)
 
#define RGBCOLOR(color)   (RED(color) + GREEN(color) + BLUE(color))
 
#define RGB_HASH_SIZE   4096
 
#define HASH_RGB(color, l)   ((RGBCOLOR(color) + (l)) & (RGB_HASH_SIZE - 1))
 
#define CMPCODE(c)   (((c) << 1 | (c) >> 2) & TERM_COLOR_MASK)
 
#define use_inverse(bg, fg)   CMPCODE(fg & TERM_COLOR_MASK) < CMPCODE(bg)
 

Typedefs

typedef int assert_enough_color_modes[(sizeof(color_modes)/sizeof(color_modes[0])==COLOR_MODES) ? 1 :-1]
 Get a compile-time error if the color_modes array has the wrong size.
 

Enumerations

enum  palette_range { PALETTE_FULL = 0 , PALETTE_HALF , PALETTE_RANGES }
 Controls what color ranges to use when setting the terminal color. More...
 

Functions

static int color_distance (const struct rgb *c1, const struct rgb *c2)
 
static unsigned char get_color (color_T color, const struct rgb *palette, int level)
 Locates the nearest terminal color.
 
NONSTATIC_INLINE void set_term_color16 (struct screen_char *schar, color_flags_T flags, unsigned char fg, unsigned char bg)
 
color_T get_term_color16 (unsigned int index)
 Mixes the color pair and attributes to a terminal text color.
 
void get_screen_char_color (struct screen_char *schar, struct color_pair *pair, color_flags_T flags, color_mode_T color_mode)
 
void set_term_color (struct screen_char *schar, struct color_pair *pair, color_flags_T flags, color_mode_T color_mode)
 

Variables

static const struct color_mode_info color_mode_16
 
static const struct color_mode_info *const color_modes []
 
static const char fg_color [16][8]
 Map foreground colors to more visible ones on various backgrounds.
 

Detailed Description

Terminal color composing.

Macro Definition Documentation

◆ BLUE

#define BLUE ( color)    (BLUE_COLOR(color) << 0)

◆ BLUE_COLOR

#define BLUE_COLOR ( color)    (unsigned char)(((color) & BLUE_COLOR_MASK) >> 0)

◆ BLUE_COLOR_MASK

#define BLUE_COLOR_MASK   0x000000FF

◆ CMPCODE

#define CMPCODE ( c)    (((c) << 1 | (c) >> 2) & TERM_COLOR_MASK)

◆ GREEN

#define GREEN ( color)    (GREEN_COLOR(color) << 2)

◆ GREEN_COLOR

#define GREEN_COLOR ( color)    (unsigned char)(((color) & GREEN_COLOR_MASK) >> 8)

◆ GREEN_COLOR_MASK

#define GREEN_COLOR_MASK   0x0000FF00

◆ HASH_RGB

#define HASH_RGB ( color,
l )   ((RGBCOLOR(color) + (l)) & (RGB_HASH_SIZE - 1))

◆ RED

#define RED ( color)    (RED_COLOR(color) << 3)

◆ RED_COLOR

#define RED_COLOR ( color)    (unsigned char)(((color) & RED_COLOR_MASK) >> 16)

◆ RED_COLOR_MASK

#define RED_COLOR_MASK   0x00FF0000

◆ RGB_HASH_SIZE

#define RGB_HASH_SIZE   4096

◆ RGBCOLOR

#define RGBCOLOR ( color)    (RED(color) + GREEN(color) + BLUE(color))

◆ use_inverse

#define use_inverse ( bg,
fg )   CMPCODE(fg & TERM_COLOR_MASK) < CMPCODE(bg)

Typedef Documentation

◆ assert_enough_color_modes

typedef int assert_enough_color_modes[(sizeof(color_modes)/sizeof(color_modes[0])==COLOR_MODES) ? 1 :-1]

Get a compile-time error if the color_modes array has the wrong size.


Enumeration Type Documentation

◆ palette_range

Controls what color ranges to use when setting the terminal color.

Todo
TODO: Part of the 256 color palette is gray scale, maybe we could experiment with a grayscale mode. ;) –jonas
Enumerator
PALETTE_FULL 
PALETTE_HALF 
PALETTE_RANGES 

Function Documentation

◆ color_distance()

static int color_distance ( const struct rgb * c1,
const struct rgb * c2 )
inlinestatic

◆ get_color()

static unsigned char get_color ( color_T color,
const struct rgb * palette,
int level )
inlinestatic

Locates the nearest terminal color.

◆ get_screen_char_color()

void get_screen_char_color ( struct screen_char * schar,
struct color_pair * pair,
color_flags_T flags,
color_mode_T color_mode )

◆ get_term_color16()

color_T get_term_color16 ( unsigned int index)

Mixes the color pair and attributes to a terminal text color.

If flags has masked in the COLOR_INCREASE_CONTRAST the foreground color will be adjusted.

XXX: schar may not be NULL and is modified adding stuff like boldness.

◆ set_term_color()

void set_term_color ( struct screen_char * schar,
struct color_pair * pair,
color_flags_T flags,
color_mode_T color_mode )

◆ set_term_color16()

NONSTATIC_INLINE void set_term_color16 ( struct screen_char * schar,
color_flags_T flags,
unsigned char fg,
unsigned char bg )

Variable Documentation

◆ color_mode_16

const struct color_mode_info color_mode_16
static
Initial value:
= {
palette16,
{
{ 8, 16 },
{ 8, 8 },
}
}

◆ color_modes

const struct color_mode_info* const color_modes[]
static
Initial value:
= {
}
static const struct color_mode_info color_mode_16
Definition color.c:133

◆ fg_color

const char fg_color[16][8]
static

Map foreground colors to more visible ones on various backgrounds.

Use like: fg = fg_color[fg][bg];

This table is based mostly on wild guesses of mine. Feel free to correct it. –pasky