C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
Color Management

Conversion of Color Data Types and Management of Colors and Color Pairs. More...

Functions

void apply_gamma (RGB *rgb)
 Apply gamma correction to RGB color.
int clr_name_to_idx (char *s)
 Get color index from color name.
void display_cmplx_str (WINDOW *win, cchar_t *cmplx_buf, int line, int col)
 Display a complex character string on a window.
int get_clr_pair (int fg, int bg)
 Get color pair index for foreground and background colors.
RGB hex_clr_str_to_rgb (char *s)
 Convert six-digit HTML style hex color code to RGB struct.
bool init_clr_palette (SIO *sio)
 Initialize color palette based on SIO settings.
void init_hex_clr (int idx, char *s)
 Initialize extended ncurses color from HTML style hex string.
void list_colors ()
 list colors to stderr
size_t mk_cmplx_str (cchar_t *cmplx_buf, char *s, attr_t attr, int cp)
 Convert a multibyte string to an array of cchar_t complex characters.
cchar_t mkccc (int cp, attr_t attr, char *s)
 Create a cchar_t with the specified color pair index.
int rgb_to_curses_clr (RGB *rgb)
 Get color index for RGB color.
int rgb_to_xterm256_idx (RGB *rgb)
 Convert RGB color to XTerm 256 color index.
RGB xterm256_idx_to_rgb (int idx)
 Convert XTerm 256 color index to RGB color.

Detailed Description

Conversion of Color Data Types and Management of Colors and Color Pairs.

Function Documentation

◆ apply_gamma()

void apply_gamma ( RGB * rgb)

Apply gamma correction to RGB color.

apply_gamma

Parameters
rgbPointer to RGB color

This function modifies the RGB color in place. It applies gamma correction to the RGB color based on the gamma values set in the SIO struct. If the color is a shade of gray, it applies the gray gamma correction. Otherwise, it applies the individual red, green, and blue gamma corrections.

Definition at line 401 of file dwin.c.

401 {
402 if (rgb->r == rgb->g && rgb->r == rgb->b) {
403 if (GRAY_GAMMA > 0.0f && GRAY_GAMMA != 1.0f) {
404 rgb->r = (int)(pow((rgb->r / 255.0f), 1.0f / GRAY_GAMMA) * 255.0f);
405 rgb->g = rgb->r;
406 rgb->b = rgb->r;
407 }
408 return;
409 }
410 if (rgb->r != 0 && RED_GAMMA > 0.0f && RED_GAMMA != 1.0f)
411 rgb->r = (int)(pow((rgb->r / 255.0f), 1.0f / RED_GAMMA) * 255.0f);
412 if (rgb->g != 0 && GREEN_GAMMA > 0.0f && GREEN_GAMMA != 1.0f)
413 rgb->g = (int)(pow((rgb->g / 255.0f), 1.0f / GREEN_GAMMA) * 255.0f);
414 if (rgb->b != 0 && BLUE_GAMMA > 0.0f && BLUE_GAMMA != 1.0f)
415 rgb->b = (int)(pow((rgb->b / 255.0f), 1.0f / BLUE_GAMMA) * 255.0f);
416}
double BLUE_GAMMA
Definition dwin.c:118
double GRAY_GAMMA
Definition dwin.c:112
double RED_GAMMA
Definition dwin.c:114
double GREEN_GAMMA
Definition dwin.c:116
int r
Definition cm.h:305
int b
Definition cm.h:307
int g
Definition cm.h:306

References RGB::b, BLUE_GAMMA, RGB::g, GRAY_GAMMA, GREEN_GAMMA, RGB::r, and RED_GAMMA.

Referenced by init_hex_clr(), and rgb_to_curses_clr().

Here is the caller graph for this function:

◆ clr_name_to_idx()

int clr_name_to_idx ( char * s)

Get color index from color name.

clr_name_to_idx

Parameters
sColor name
Returns
Color index or -1 if not found

Definition at line 1536 of file dwin.c.

1536 {
1537 int i = 0;
1538 int n = 16;
1539
1540 str_to_lower(s);
1541 while (i < n) {
1542 if (!strcmp(colors_text[i], s))
1543 break;
1544 i++;
1545 }
1546 if (i >= n)
1547 return (-1);
1548 return (i);
1549}
char const colors_text[][10]
Color names for .minitrc overrides.
Definition dwin.c:91
bool str_to_lower(char *)
Converts a string to lowercase.
Definition futil.c:399

References colors_text, and str_to_lower().

Here is the call graph for this function:

◆ display_cmplx_str()

void display_cmplx_str ( WINDOW * win,
cchar_t * cmplx_buf,
int line,
int col )

Display a complex character string on a window.

display_cmplx_str

Parameters
winNCurses window to display the string on
cmplx_bufArray of cchar_t complex characters to display
lineLine number to display the string on
colColumn number to start displaying the string from

This function clears the line where the string will be displayed, then uses wadd_wchstr to add the complex character string (cmplx_buf) to the window. Finally, it moves the cursor to the specified column position.

Definition at line 678 of file dwin.c.

678 {
679 wmove(win, line, 0);
680 wclrtoeol(win);
681 wmove(win, line, 0);
682 wadd_wchstr(win, cmplx_buf);
683 wmove(win, line, col);
684 return;
685}
WINDOW * win
Definition dwin.c:121

◆ get_clr_pair()

int get_clr_pair ( int fg,
int bg )

Get color pair index for foreground and background colors.

get_clr_pair

Parameters
fgForeground color index
bgBackground color index
Returns
Color pair index

Definition at line 284 of file dwin.c.

284 {
285 int rc, i, pfg, pbg;
286 for (i = 1; i < clr_pair_cnt; i++) {
287 extended_pair_content(i, &pfg, &pbg);
288 if (pfg == fg && pbg == bg)
289 return i;
290 }
291 if (i >= COLOR_PAIRS) {
292 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__, __LINE__ - 1);
293 ssnprintf(em1, MAXLEN - 1, "NCurses COLOR_PAIRS (%d) exceeded (%d)",
294 COLOR_PAIRS, i);
295 strerror_r(errno, em2, MAXLEN);
296 display_error(em0, em1, em2, nullptr);
297 return (EXIT_FAILURE);
298 }
299 if (i < COLOR_PAIRS) {
300 rc = init_extended_pair(i, fg, bg);
301 if (rc == ERR)
302 return ERR;
303 }
304 if (i < COLOR_PAIRS)
305 clr_pair_cnt++;
306 return i;
307}
#define MAXLEN
Definition curskeys.c:15
int clr_pair_cnt
Definition dwin.c:155
char em1[MAXLEN]
Definition dwin.c:142
char em0[MAXLEN]
Definition dwin.c:141
char em2[MAXLEN]
Definition dwin.c:143
int display_error(char *em0, char *em1, char *em2, char *em3)
Display an error message window or print to stderr.
Definition dwin.c:1102
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:311

References clr_pair_cnt, display_error(), em0, em1, em2, and ssnprintf().

Referenced by open_curses(), and parse_ansi_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hex_clr_str_to_rgb()

RGB hex_clr_str_to_rgb ( char * s)

Convert six-digit HTML style hex color code to RGB struct.

hex_clr_str_to_rgb

Parameters
ssix-digit HTML style hex color code

Definition at line 526 of file dwin.c.

526 {
527 RGB rgb;
528 sscanf(s, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
529 return rgb;
530}
Definition cm.h:304

References RGB::b, RGB::g, and RGB::r.

Referenced by init_hex_clr().

Here is the caller graph for this function:

◆ init_clr_palette()

bool init_clr_palette ( SIO * sio)

Initialize color palette based on SIO settings.

init_clr_palette

Parameters
sioPointer to SIO struct with color settings
Returns
true if successful, false if error

This function initializes the xterm256 color cube and applies any color overrides specified in the SIO struct. The color strings in the SIO struct are expected to be six-digit HTML style hex color codes (e.g., "#RRGGBB"). If a color override is specified for any of the standard colors, it is applied using the init_hex_clr function. After processing all colors, the clr_cnt variable is set to CLR_NCOLORS to indicate that the standard colors have been initialized.

Definition at line 429 of file dwin.c.

429 {
430 if (sio->black[0])
431 init_hex_clr(CLR_BLACK, sio->black);
432 if (sio->red[0])
433 init_hex_clr(CLR_RED, sio->red);
434 if (sio->green[0])
435 init_hex_clr(CLR_GREEN, sio->green);
436 if (sio->yellow[0])
437 init_hex_clr(CLR_YELLOW, sio->yellow);
438 if (sio->blue[0])
439 init_hex_clr(CLR_BLUE, sio->blue);
440 if (sio->magenta[0])
441 init_hex_clr(CLR_MAGENTA, sio->magenta);
442 if (sio->cyan[0])
443 init_hex_clr(CLR_CYAN, sio->cyan);
444 if (sio->white[0])
445 init_hex_clr(CLR_WHITE, sio->white);
446 if (sio->bblack[0])
447 init_hex_clr(CLR_BBLACK, sio->bblack);
448 if (sio->bred[0])
449 init_hex_clr(CLR_BRED, sio->bred);
450 if (sio->bgreen[0])
451 init_hex_clr(CLR_BGREEN, sio->bgreen);
452 if (sio->byellow[0])
453 init_hex_clr(CLR_BYELLOW, sio->byellow);
454 if (sio->bblue[0])
455 init_hex_clr(CLR_BBLUE, sio->bblue);
456 if (sio->bmagenta[0])
457 init_hex_clr(CLR_BMAGENTA, sio->bmagenta);
458 if (sio->bcyan[0])
459 init_hex_clr(CLR_BCYAN, sio->bcyan);
460 if (sio->bwhite[0])
461 init_hex_clr(CLR_BWHITE, sio->bwhite);
462 if (sio->borange[0])
463 init_hex_clr(CLR_BORANGE, sio->borange);
464 if (sio->fg_clr_x[0])
465 init_hex_clr(CLR_FG, sio->fg_clr_x);
466 if (sio->bg_clr_x[0])
467 init_hex_clr(CLR_BG, sio->bg_clr_x);
468 if (sio->bo_clr_x[0])
469 init_hex_clr(CLR_BO, sio->bo_clr_x);
470 if (sio->ln_clr_x[0])
471 init_hex_clr(CLR_LN, sio->ln_clr_x);
472 if (sio->ln_bg_clr_x[0])
473 init_hex_clr(CLR_LN_BG, sio->ln_bg_clr_x);
474
475 if (sio->nt_fg[0])
476 init_hex_clr(CLR_NT_FG, sio->nt_fg);
477 if (sio->nt_bg[0])
478 init_hex_clr(CLR_NT_BG, sio->nt_bg);
479
480 if (sio->nt_rev_fg[0])
481 init_hex_clr(CLR_NT_REV_FG, sio->nt_rev_fg);
482 if (sio->nt_rev_bg[0])
483 init_hex_clr(CLR_NT_REV_BG, sio->nt_rev_bg);
484
485 if (sio->nt_hl_fg[0])
486 init_hex_clr(CLR_NT_HL_FG, sio->nt_hl_fg);
487 if (sio->nt_hl_bg[0])
488 init_hex_clr(CLR_NT_HL_BG, sio->nt_hl_bg);
489
490 if (sio->nt_hl_rev_fg[0])
491 init_hex_clr(CLR_NT_HL_REV_FG, sio->nt_hl_rev_fg);
492 if (sio->nt_hl_rev_bg[0])
493 init_hex_clr(CLR_NT_HL_REV_BG, sio->nt_hl_rev_bg);
494
496 return true;
497}
@ CLR_FG
Definition cm.h:141
@ CLR_RED
Definition cm.h:125
@ CLR_NT_FG
Definition cm.h:146
@ CLR_YELLOW
Definition cm.h:127
@ CLR_BCYAN
Definition cm.h:138
@ CLR_WHITE
Definition cm.h:131
@ CLR_MAGENTA
Definition cm.h:129
@ CLR_NT_REV_BG
Definition cm.h:149
@ CLR_BLACK
Definition cm.h:124
@ CLR_BWHITE
Definition cm.h:139
@ CLR_LN_BG
Definition cm.h:145
@ CLR_NT_HL_REV_BG
Definition cm.h:153
@ CLR_BO
Definition cm.h:143
@ CLR_BBLACK
Definition cm.h:132
@ CLR_NT_HL_FG
Definition cm.h:150
@ CLR_BBLUE
Definition cm.h:136
@ CLR_LN
Definition cm.h:144
@ CLR_BGREEN
Definition cm.h:134
@ CLR_BYELLOW
Definition cm.h:135
@ CLR_BMAGENTA
Definition cm.h:137
@ CLR_BORANGE
Definition cm.h:140
@ CLR_NT_HL_BG
Definition cm.h:151
@ CLR_NT_HL_REV_FG
Definition cm.h:152
@ CLR_BG
Definition cm.h:142
@ CLR_NT_BG
Definition cm.h:147
@ CLR_BRED
Definition cm.h:133
@ CLR_NCOLORS
Definition cm.h:154
@ CLR_BLUE
Definition cm.h:128
@ CLR_GREEN
Definition cm.h:126
@ CLR_NT_REV_FG
Definition cm.h:148
@ CLR_CYAN
Definition cm.h:130
int clr_cnt
Definition dwin.c:153
SIO * sio
Definition dwin.c:80
void init_hex_clr(int, char *)
Initialize extended ncurses color from HTML style hex string.
Definition dwin.c:508

References SIO::bblack, SIO::bblue, SIO::bcyan, SIO::bg_clr_x, SIO::bgreen, SIO::black, SIO::blue, SIO::bmagenta, SIO::bo_clr_x, SIO::borange, SIO::bred, SIO::bwhite, SIO::byellow, CLR_BBLACK, CLR_BBLUE, CLR_BCYAN, CLR_BG, CLR_BGREEN, CLR_BLACK, CLR_BLUE, CLR_BMAGENTA, CLR_BO, CLR_BORANGE, CLR_BRED, CLR_BWHITE, CLR_BYELLOW, clr_cnt, CLR_CYAN, CLR_FG, CLR_GREEN, CLR_LN, CLR_LN_BG, CLR_MAGENTA, CLR_NCOLORS, CLR_NT_BG, CLR_NT_FG, CLR_NT_HL_BG, CLR_NT_HL_FG, CLR_NT_HL_REV_BG, CLR_NT_HL_REV_FG, CLR_NT_REV_BG, CLR_NT_REV_FG, CLR_RED, CLR_WHITE, CLR_YELLOW, SIO::cyan, SIO::fg_clr_x, SIO::green, init_hex_clr(), SIO::ln_bg_clr_x, SIO::ln_clr_x, SIO::magenta, SIO::nt_bg, SIO::nt_fg, SIO::nt_hl_bg, SIO::nt_hl_fg, SIO::nt_hl_rev_bg, SIO::nt_hl_rev_fg, SIO::nt_rev_bg, SIO::nt_rev_fg, SIO::red, SIO::white, and SIO::yellow.

Referenced by open_curses().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_hex_clr()

void init_hex_clr ( int idx,
char * s )

Initialize extended ncurses color from HTML style hex string.

init_hex_clr

Parameters
idxColor index
sHex color string

NCurses uses 0-1000 for RGB values, so the RGB values from the hex string are converted to this range before initializing the color. If the color index is less than 16, the RGB values are also stored in the StdColors array for reference.

Definition at line 508 of file dwin.c.

508 {
509 RGB rgb;
510 rgb = hex_clr_str_to_rgb(s);
511 apply_gamma(&rgb);
512 if (idx < 16) {
513 StdColors[idx].r = rgb.r;
514 StdColors[idx].g = rgb.g;
515 StdColors[idx].b = rgb.b;
516 }
517 rgb.r = (rgb.r * 1000) / 255;
518 rgb.g = (rgb.g * 1000) / 255;
519 rgb.b = (rgb.b * 1000) / 255;
520 init_extended_color(idx, rgb.r, rgb.g, rgb.b);
521}
RGB StdColors[16]
Definition dwin.c:85
RGB hex_clr_str_to_rgb(char *)
Convert six-digit HTML style hex color code to RGB struct.
Definition dwin.c:526
void apply_gamma(RGB *)
Apply gamma correction to RGB color.
Definition dwin.c:401

References apply_gamma(), RGB::b, RGB::g, hex_clr_str_to_rgb(), RGB::r, and StdColors.

Referenced by init_clr_palette().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_colors()

void list_colors ( )

list colors to stderr

list_colors

only lists the first 16, since that's how many we let the user redefine

Definition at line 1555 of file dwin.c.

1555 {
1556 int i, col;
1557
1558 for (i = 0, col = 0; i < 16; i++, col++) {
1559 if (i < 8) {
1560 fprintf(stderr, " ");
1561 }
1562 if (i == 8) {
1563 col = 0;
1564 fprintf(stderr, "\n");
1565 } else if (col > 0)
1566 fprintf(stderr, " ");
1567 fprintf(stderr, "%s", colors_text[i]);
1568 }
1569 fprintf(stderr, "\n");
1570}

References colors_text.

◆ mk_cmplx_str()

size_t mk_cmplx_str ( cchar_t * cmplx_buf,
char * s,
attr_t attr,
int cp )

Convert a multibyte string to an array of cchar_t complex characters.

mk_cmplx_str

Parameters
cmplx_bufOutput buffer for complex characters (allocated within the function)
sInput multibyte string
attrAttributes to apply to the complex characters
cpColor pair index for the complex characters
Returns
Number of bytes processed from the input string

This function converts a multibyte string to an array of complex characters (cchar_t) that can be used with NCurses functions. It handles multibyte characters and applies the specified color pair to each character.

Note
cmplx_buf is allocated within the function and should be freed by the caller when no longer needed.

Definition at line 648 of file dwin.c.

648 {
649 cchar_t cc = {0};
650 wchar_t wstr[2] = {L'\0', L'\0'};
651 mbstate_t mbstate;
652 memset(&mbstate, 0, sizeof(mbstate));
653 size_t len = strlen(s);
654 cmplx_buf = calloc(len + 1, sizeof(cchar_t));
655
656 while (*s != '\0') {
657 mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
658 setcchar(&cc, wstr, attr, cp, nullptr);
659 *cmplx_buf++ = cc;
660 s += mbrlen(s, MB_CUR_MAX, &mbstate);
661 }
662 wstr[0] = L'\0';
663 wstr[1] = L'\0';
664 setcchar(&cc, wstr, attr, cp, nullptr);
665 *cmplx_buf++ = cc;
666 return len;
667}

◆ mkccc()

cchar_t mkccc ( int cp,
attr_t attr,
char * s )

Create a cchar_t with the specified color pair index.

mkccc

Parameters
cpColor pair index
attrAttributes to apply to the cchar_t
sMultibyte string to convert to wide character (only the first character is used)
Returns
cchar_t with the specified color pair index and a space character as the wide character

Definition at line 625 of file dwin.c.

625 {
626 cchar_t cc = {0};
627 wchar_t wstr[2] = {L'\0', L'\0'};
628 mbstate_t mbstate;
629 memset(&mbstate, 0, sizeof(mbstate));
630 mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
631 setcchar(&cc, wstr, attr, cp, nullptr);
632 return cc;
633}

Referenced by open_curses().

Here is the caller graph for this function:

◆ rgb_to_curses_clr()

int rgb_to_curses_clr ( RGB * rgb)

Get color index for RGB color.

rgb_to_curses_clr

Parameters
rgbRGB color
Returns
NCurses color index

Curses uses 0-1000 for RGB values. If the color does not exist, it is created along with a new color index

Definition at line 315 of file dwin.c.

315 {
316 int i;
317 int r, g, b;
318 apply_gamma(rgb);
319 rgb->r = (rgb->r * 1000) / 255;
320 rgb->g = (rgb->g * 1000) / 255;
321 rgb->b = (rgb->b * 1000) / 255;
322 for (i = 0; i < clr_cnt; i++) {
323 extended_color_content(i, &r, &g, &b);
324 if (rgb->r == r && rgb->g == g && rgb->b == b) {
325 return i;
326 }
327 }
328 if (i < COLORS) {
329 init_extended_color(i, rgb->r, rgb->g, rgb->b);
330 clr_cnt++;
331 return clr_cnt - 1;
332 }
333 return ERR;
334}

References apply_gamma(), RGB::b, clr_cnt, RGB::g, and RGB::r.

Referenced by parse_ansi_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rgb_to_xterm256_idx()

int rgb_to_xterm256_idx ( RGB * rgb)

Convert RGB color to XTerm 256 color index.

rgb_to_xterm256_idx

Parameters
rgbRGB color
Returns
XTerm 256 color index

This function converts an RGB color to the nearest XTerm 256 color index. It first checks if the color is a shade of gray, and if so, it uses the gray ramp. Otherwise, it calculates the nearest color in the 6x6x6 color cube.

Definition at line 344 of file dwin.c.

344 {
345 if (rgb->r == rgb->g && rgb->g == rgb->b) {
346 if (rgb->r < 8)
347 return 16;
348 if (rgb->r > 248)
349 return 231;
350 return ((rgb->r - 8) / 10) + 231;
351 } else {
352 int r_index = (rgb->r < 45) ? 0 : (rgb->r - 60) / 40 + 1;
353 int g_index = (rgb->g < 45) ? 0 : (rgb->g - 60) / 40 + 1;
354 int b_index = (rgb->b < 45) ? 0 : (rgb->b - 60) / 40 + 1;
355 return 16 + (36 * r_index) + (6 * g_index) + b_index;
356 }
357}

References RGB::b, RGB::g, and RGB::r.

◆ xterm256_idx_to_rgb()

RGB xterm256_idx_to_rgb ( int idx)

Convert XTerm 256 color index to RGB color.

xterm256_idx_to_rgb

Parameters
idxXTerm 256 color index
Returns
RGB color

This function converts an XTerm 256 color index to an RGB color. It first checks if the index is in the standard 16 colors, then checks if it's in the 6x6x6 color cube, and finally checks if it's in the gray ramp.

Convert XTerm 256 color index to RGB

Parameters
idx- XTerm 256 color index
Returns
RGB struct

Definition at line 366 of file dwin.c.

366 {
370 RGB rgb;
371 if (idx > 255)
372 idx = 255;
373 if (idx < 0)
374 idx = 0;
375 rgb.r = rgb.g = rgb.b = 0;
376 if (idx < 16) {
377 rgb.r = StdColors[idx].r;
378 rgb.g = StdColors[idx].g;
379 rgb.b = StdColors[idx].b;
380 } else if (idx >= 16 && idx <= 231) {
381 idx -= 16;
382 rgb.r = (idx / 36) % 6 * 51;
383 rgb.g = (idx / 6) % 6 * 51;
384 rgb.b = (idx % 6) * 51;
385 } else if (idx >= 232 && idx <= 255) {
386 int gray = (idx - 232) * 11;
387 rgb.r = rgb.g = rgb.b = gray;
388 }
389 return rgb;
390}

References RGB::b, RGB::g, RGB::r, and StdColors.

Referenced by parse_ansi_str().

Here is the caller graph for this function: