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

int get_clr_pair (int fg, int bg)
 Get color pair index for foreground and background colors.
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.
void apply_gamma (RGB *rgb)
 Apply gamma correction to RGB color.
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.
RGB hex_clr_str_to_rgb (char *s)
 Convert six-digit HTML style hex color code to RGB struct.
cchar_t mkccc (int cp)
 Create a cchar_t with the specified color pair index.
int clr_name_to_idx (char *s)
 Get color index from color name.
void list_colors ()
 list colors to stderr

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.

Parameters
rgbPointer to RGB color
Note
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 624 of file dwin.c.

624 {
625 if (rgb->r == rgb->g && rgb->r == rgb->b) {
626 if (GRAY_GAMMA > 0.0f && GRAY_GAMMA != 1.0f) {
627 rgb->r = (int)(pow((rgb->r / 255.0f), 1.0f / GRAY_GAMMA) * 255.0f);
628 rgb->g = rgb->r;
629 rgb->b = rgb->r;
630 }
631 return;
632 }
633 if (rgb->r != 0 && RED_GAMMA > 0.0f && RED_GAMMA != 1.0f)
634 rgb->r = (int)(pow((rgb->r / 255.0f), 1.0f / RED_GAMMA) * 255.0f);
635 if (rgb->g != 0 && GREEN_GAMMA > 0.0f && GREEN_GAMMA != 1.0f)
636 rgb->g = (int)(pow((rgb->g / 255.0f), 1.0f / GREEN_GAMMA) * 255.0f);
637 if (rgb->b != 0 && BLUE_GAMMA > 0.0f && BLUE_GAMMA != 1.0f)
638 rgb->b = (int)(pow((rgb->b / 255.0f), 1.0f / BLUE_GAMMA) * 255.0f);
639}
double BLUE_GAMMA
Definition dwin.c:110
double GRAY_GAMMA
Definition dwin.c:104
double RED_GAMMA
Definition dwin.c:106
double GREEN_GAMMA
Definition dwin.c:108
int r
Definition cm.h:266
int b
Definition cm.h:268
int g
Definition cm.h:267

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.

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

Definition at line 1282 of file dwin.c.

1282 {
1283 int i = 0;
1284 int n = 16;
1285
1286 str_to_lower(s);
1287 while (i < n) {
1288 if (!strcmp(colors_text[i], s))
1289 break;
1290 i++;
1291 }
1292 if (i >= n)
1293 return (-1);
1294 return (i);
1295}
char const colors_text[][10]
Definition dwin.c:91
bool str_to_lower(char *)
Converts a string to lowercase.
Definition futil.c:233

References colors_text, and str_to_lower().

Here is the call graph for this function:

◆ get_clr_pair()

int get_clr_pair ( int fg,
int bg )

Get color pair index for foreground and background colors.

Parameters
fgForeground color index
bgBackground color index
Returns
Color pair index

Definition at line 510 of file dwin.c.

510 {
511 int rc, i, pfg, pbg;
512 for (i = 1; i < clr_pair_cnt; i++) {
513 extended_pair_content(i, &pfg, &pbg);
514 if (pfg == fg && pbg == bg)
515 return i;
516 }
517 if (i >= COLOR_PAIRS) {
518 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__, __LINE__ - 1);
519 ssnprintf(em1, MAXLEN - 1, "NCurses COLOR_PAIRS (%d) exceeded (%d)",
520 COLOR_PAIRS, i);
521 strerror_r(errno, em2, MAXLEN);
522 display_error(em0, em1, em2, nullptr);
523 return (EXIT_FAILURE);
524 }
525 if (i < COLOR_PAIRS) {
526 rc = init_extended_pair(i, fg, bg);
527 if (rc == ERR)
528 return ERR;
529 }
530 if (i < COLOR_PAIRS)
531 clr_pair_cnt++;
532 return i;
533}
#define MAXLEN
Definition curskeys.c:15
int clr_pair_cnt
Definition dwin.c:144
char em1[MAXLEN]
Definition dwin.c:133
char em0[MAXLEN]
Definition dwin.c:132
char em2[MAXLEN]
Definition dwin.c:134
int display_error(char *em0, char *em1, char *em2, char *em3)
Display an error message window or print to stderr.
Definition dwin.c:1054
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:147

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.

Parameters
ssix-digit HTML style hex color code

Definition at line 725 of file dwin.c.

725 {
726 RGB rgb;
727 sscanf(s, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
728 return rgb;
729}
Definition cm.h:265

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.

Parameters
sioPointer to SIO struct with color settings
Returns
true if successful, false if error
Note
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 16 to indicate that the standard colors have been initialized.

Definition at line 651 of file dwin.c.

651 {
652 if (sio->black[0])
653 init_hex_clr(CLR_BLACK, sio->black);
654 if (sio->red[0])
655 init_hex_clr(CLR_RED, sio->red);
656 if (sio->green[0])
657 init_hex_clr(CLR_GREEN, sio->green);
658 if (sio->yellow[0])
659 init_hex_clr(CLR_YELLOW, sio->yellow);
660 if (sio->blue[0])
661 init_hex_clr(CLR_BLUE, sio->blue);
662 if (sio->magenta[0])
663 init_hex_clr(CLR_MAGENTA, sio->magenta);
664 if (sio->cyan[0])
665 init_hex_clr(CLR_CYAN, sio->cyan);
666 if (sio->white[0])
667 init_hex_clr(CLR_WHITE, sio->white);
668 if (sio->bblack[0])
669 init_hex_clr(CLR_BBLACK, sio->bblack);
670 if (sio->bred[0])
671 init_hex_clr(CLR_BRED, sio->bred);
672 if (sio->bgreen[0])
673 init_hex_clr(CLR_BGREEN, sio->bgreen);
674 if (sio->byellow[0])
675 init_hex_clr(CLR_BYELLOW, sio->byellow);
676 if (sio->bblue[0])
677 init_hex_clr(CLR_BBLUE, sio->bblue);
678 if (sio->bmagenta[0])
679 init_hex_clr(CLR_BMAGENTA, sio->bmagenta);
680 if (sio->bcyan[0])
681 init_hex_clr(CLR_BCYAN, sio->bcyan);
682 if (sio->bwhite[0])
683 init_hex_clr(CLR_BWHITE, sio->bwhite);
684 if (sio->borange[0])
685 init_hex_clr(CLR_BORANGE, sio->borange);
686 if (sio->fg_clr_x[0])
687 init_hex_clr(CLR_FG, sio->fg_clr_x);
688 if (sio->bg_clr_x[0])
689 init_hex_clr(CLR_BG, sio->bg_clr_x);
690 if (sio->bo_clr_x[0])
691 init_hex_clr(CLR_BO, sio->bo_clr_x);
692 if (sio->ln_clr_x[0])
693 init_hex_clr(CLR_LN, sio->ln_clr_x);
694 if (sio->ln_bg_clr_x[0])
695 init_hex_clr(CLR_LN_BG, sio->ln_bg_clr_x);
697 return true;
698}
@ CLR_FG
Definition cm.h:140
@ CLR_RED
Definition cm.h:124
@ CLR_YELLOW
Definition cm.h:126
@ CLR_BCYAN
Definition cm.h:137
@ CLR_WHITE
Definition cm.h:130
@ CLR_MAGENTA
Definition cm.h:128
@ CLR_BLACK
Definition cm.h:123
@ CLR_BWHITE
Definition cm.h:138
@ CLR_LN_BG
Definition cm.h:144
@ CLR_BO
Definition cm.h:142
@ CLR_BBLACK
Definition cm.h:131
@ CLR_BBLUE
Definition cm.h:135
@ CLR_LN
Definition cm.h:143
@ CLR_BGREEN
Definition cm.h:133
@ CLR_BYELLOW
Definition cm.h:134
@ CLR_BMAGENTA
Definition cm.h:136
@ CLR_BORANGE
Definition cm.h:139
@ CLR_BG
Definition cm.h:141
@ CLR_BRED
Definition cm.h:132
@ CLR_NCOLORS
Definition cm.h:145
@ CLR_BLUE
Definition cm.h:127
@ CLR_GREEN
Definition cm.h:125
@ CLR_CYAN
Definition cm.h:129
int clr_cnt
Definition dwin.c:142
SIO * sio
Definition dwin.c:77
void init_hex_clr(int, char *)
Initialize extended ncurses color from HTML style hex string.
Definition dwin.c:708

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

Parameters
idxColor index
sHex color string
Note
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 708 of file dwin.c.

708 {
709 RGB rgb;
710 rgb = hex_clr_str_to_rgb(s);
711 apply_gamma(&rgb);
712 if (idx < 16) {
713 StdColors[idx].r = rgb.r;
714 StdColors[idx].g = rgb.g;
715 StdColors[idx].b = rgb.b;
716 }
717 rgb.r = (rgb.r * 1000) / 255;
718 rgb.g = (rgb.g * 1000) / 255;
719 rgb.b = (rgb.b * 1000) / 255;
720 init_extended_color(idx, rgb.r, rgb.g, rgb.b);
721}
RGB StdColors[16]
Definition dwin.c:82
RGB hex_clr_str_to_rgb(char *)
Convert six-digit HTML style hex color code to RGB struct.
Definition dwin.c:725
void apply_gamma(RGB *)
Apply gamma correction to RGB color.
Definition dwin.c:624

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

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

Definition at line 1300 of file dwin.c.

1300 {
1301 int i, col;
1302
1303 for (i = 0, col = 0; i < 16; i++, col++) {
1304 if (i < 8) {
1305 fprintf(stderr, " ");
1306 }
1307 if (i == 8) {
1308 col = 0;
1309 fprintf(stderr, "\n");
1310 } else if (col > 0)
1311 fprintf(stderr, " ");
1312 fprintf(stderr, "%s", colors_text[i]);
1313 }
1314 fprintf(stderr, "\n");
1315}

References colors_text.

◆ mkccc()

cchar_t mkccc ( int cp)

Create a cchar_t with the specified color pair index.

Parameters
cpColor pair index
Returns
cchar_t with the specified color pair index and a space character as the wide character

Definition at line 766 of file dwin.c.

766 {
767 cchar_t cc = {0};
768 wchar_t wc = L' ';
769 setcchar(&cc, &wc, WA_NORMAL, cp, nullptr);
770 return cc;
771}

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.

Parameters
rgbRGB color
Returns
NCurses color index
Note
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 541 of file dwin.c.

541 {
542 int i;
543 int r, g, b;
544 apply_gamma(rgb);
545 rgb->r = (rgb->r * 1000) / 255;
546 rgb->g = (rgb->g * 1000) / 255;
547 rgb->b = (rgb->b * 1000) / 255;
548 for (i = 0; i < clr_cnt; i++) {
549 extended_color_content(i, &r, &g, &b);
550 if (rgb->r == r && rgb->g == g && rgb->b == b) {
551 return i;
552 }
553 }
554 if (i < COLORS) {
555 init_extended_color(i, rgb->r, rgb->g, rgb->b);
556 clr_cnt++;
557 return clr_cnt - 1;
558 }
559 return ERR;
560}

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.

Parameters
rgbRGB color
Returns
XTerm 256 color index note 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 569 of file dwin.c.

569 {
570 if (rgb->r == rgb->g && rgb->g == rgb->b) {
571 if (rgb->r < 8)
572 return 16;
573 if (rgb->r > 248)
574 return 231;
575 return ((rgb->r - 8) / 10) + 231;
576 } else {
577 int r_index = (rgb->r < 45) ? 0 : (rgb->r - 60) / 40 + 1;
578 int g_index = (rgb->g < 45) ? 0 : (rgb->g - 60) / 40 + 1;
579 int b_index = (rgb->b < 45) ? 0 : (rgb->b - 60) / 40 + 1;
580 return 16 + (36 * r_index) + (6 * g_index) + b_index;
581 }
582}

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.

Parameters
idxXTerm 256 color index
Returns
RGB color note 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 590 of file dwin.c.

590 {
594 RGB rgb;
595 if (idx > 255)
596 idx = 255;
597 if (idx < 0)
598 idx = 0;
599 rgb.r = rgb.g = rgb.b = 0;
600 if (idx < 16) {
601 rgb.r = StdColors[idx].r;
602 rgb.g = StdColors[idx].g;
603 rgb.b = StdColors[idx].b;
604 } else if (idx >= 16 && idx <= 231) {
605 idx -= 16;
606 rgb.r = (idx / 36) % 6 * 51;
607 rgb.g = (idx / 6) % 6 * 51;
608 rgb.b = (idx % 6) * 51;
609 } else if (idx >= 232 && idx <= 255) {
610 int gray = (idx - 232) * 11;
611 rgb.r = rgb.g = rgb.b = gray;
612 }
613 return rgb;
614}

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

Referenced by parse_ansi_str().

Here is the caller graph for this function: