]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_color.c
ncurses 6.2 - patch 20210508
[ncurses.git] / ncurses / base / lib_color.c
index 376ad4f6047d099158438cc7feb87031a7ca9bf7..e4276784f8fed6d5ffd25510c19ae4fd9cfd7f1c 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright 2018-2019,2020 Thomas E. Dickey                                *
+ * Copyright 2018-2020,2021 Thomas E. Dickey                                *
  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
@@ -49,7 +49,7 @@
 #define CUR SP_TERMTYPE
 #endif
 
 #define CUR SP_TERMTYPE
 #endif
 
-MODULE_ID("$Id: lib_color.c,v 1.143 2020/02/02 23:34:34 tom Exp $")
+MODULE_ID("$Id: lib_color.c,v 1.147 2021/05/08 15:11:48 tom Exp $")
 
 #ifdef USE_TERM_DRIVER
 #define CanChange      InfoOf(SP_PARM).canchange
 
 #ifdef USE_TERM_DRIVER
 #define CanChange      InfoOf(SP_PARM).canchange
@@ -140,7 +140,6 @@ NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette = hls_palette;
 #endif
 
 /* *INDENT-ON* */
 #endif
 
 /* *INDENT-ON* */
-
 #if NCURSES_EXT_FUNCS
 /*
  * These are called from _nc_do_color(), which in turn is called from
 #if NCURSES_EXT_FUNCS
 /*
  * These are called from _nc_do_color(), which in turn is called from
@@ -190,12 +189,12 @@ set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
     if (set_a_background) {
        TPUTS_TRACE("set_a_background");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
     if (set_a_background) {
        TPUTS_TRACE("set_a_background");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
-                               TPARM_1(set_a_background, bg),
+                               TIPARM_1(set_a_background, bg),
                                1, outc);
     } else {
        TPUTS_TRACE("set_background");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
                                1, outc);
     } else {
        TPUTS_TRACE("set_background");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
-                               TPARM_1(set_background, toggled_colors(bg)),
+                               TIPARM_1(set_background, toggled_colors(bg)),
                                1, outc);
     }
 #endif
                                1, outc);
     }
 #endif
@@ -210,12 +209,12 @@ set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
     if (set_a_foreground) {
        TPUTS_TRACE("set_a_foreground");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
     if (set_a_foreground) {
        TPUTS_TRACE("set_a_foreground");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
-                               TPARM_1(set_a_foreground, fg),
+                               TIPARM_1(set_a_foreground, fg),
                                1, outc);
     } else {
        TPUTS_TRACE("set_foreground");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
                                1, outc);
     } else {
        TPUTS_TRACE("set_foreground");
        NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
-                               TPARM_1(set_foreground, toggled_colors(fg)),
+                               TIPARM_1(set_foreground, toggled_colors(fg)),
                                1, outc);
     }
 #endif
                                1, outc);
     }
 #endif
@@ -253,20 +252,21 @@ init_direct_colors(NCURSES_SP_DCL0)
 {
     static NCURSES_CONST char name[] = "RGB";
 
 {
     static NCURSES_CONST char name[] = "RGB";
 
-    int n;
-    const char *s;
-    int width;
     rgb_bits_t *result = &(SP_PARM->_direct_color);
 
     result->value = 0;
 
     if (COLORS >= 8) {
     rgb_bits_t *result = &(SP_PARM->_direct_color);
 
     result->value = 0;
 
     if (COLORS >= 8) {
+       int n;
+       const char *s;
+       int width;
+
        /* find the number of bits needed for the maximum color value */
        for (width = 0; (1 << width) - 1 < (COLORS - 1); ++width) {
            ;
        }
 
        /* find the number of bits needed for the maximum color value */
        for (width = 0; (1 << width) - 1 < (COLORS - 1); ++width) {
            ;
        }
 
-       if ((n = tigetflag(name)) > 0) {
+       if (tigetflag(name) > 0) {
            n = (width + 2) / 3;
            result->bits.red = UChar(n);
            result->bits.green = UChar(n);
            n = (width + 2) / 3;
            result->bits.red = UChar(n);
            result->bits.green = UChar(n);
@@ -361,7 +361,6 @@ NCURSES_EXPORT(int)
 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
 {
     int result = ERR;
 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
 {
     int result = ERR;
-    int maxpairs = 0, maxcolors = 0;
 
     T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
 
 
     T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
 
@@ -370,8 +369,8 @@ NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
     } else if (SP_PARM->_coloron) {
        result = OK;
     } else {
     } else if (SP_PARM->_coloron) {
        result = OK;
     } else {
-       maxpairs = MaxPairs;
-       maxcolors = MaxColors;
+       int maxpairs = MaxPairs;
+       int maxcolors = MaxColors;
        if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
            set_foreground_color(NCURSES_SP_ARGx
                                 default_fg(NCURSES_SP_ARG),
        if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
            set_foreground_color(NCURSES_SP_ARGx
                                 default_fg(NCURSES_SP_ARG),
@@ -672,14 +671,14 @@ _nc_init_pair(SCREEN *sp, int pair, int f, int b)
            (int) tp[b].red, (int) tp[b].green, (int) tp[b].blue));
 
        NCURSES_PUTP2("initialize_pair",
            (int) tp[b].red, (int) tp[b].green, (int) tp[b].blue));
 
        NCURSES_PUTP2("initialize_pair",
-                     TPARM_7(initialize_pair,
-                             pair,
-                             (int) tp[f].red,
-                             (int) tp[f].green,
-                             (int) tp[f].blue,
-                             (int) tp[b].red,
-                             (int) tp[b].green,
-                             (int) tp[b].blue));
+                     TIPARM_7(initialize_pair,
+                              pair,
+                              (int) tp[f].red,
+                              (int) tp[f].green,
+                              (int) tp[f].blue,
+                              (int) tp[b].red,
+                              (int) tp[b].green,
+                              (int) tp[b].blue));
     }
 #endif
 
     }
 #endif
 
@@ -746,7 +745,7 @@ _nc_init_color(SCREEN *sp, int color, int r, int g, int b)
        CallDriver_4(sp, td_initcolor, color, r, g, b);
 #else
        NCURSES_PUTP2("initialize_color",
        CallDriver_4(sp, td_initcolor, color, r, g, b);
 #else
        NCURSES_PUTP2("initialize_color",
-                     TPARM_4(initialize_color, color, r, g, b));
+                     TIPARM_4(initialize_color, color, r, g, b));
 #endif
        sp->_color_defs = max(color + 1, sp->_color_defs);
 
 #endif
        sp->_color_defs = max(color + 1, sp->_color_defs);
 
@@ -841,52 +840,57 @@ _nc_color_content(SCREEN *sp, int color, int *r, int *g, int *b)
        (void *) g,
        (void *) b));
 
        (void *) g,
        (void *) b));
 
-    if (sp == 0)
-       returnCode(result);
-
-    maxcolors = MaxColors;
+    if (sp != 0) {
+       maxcolors = MaxColors;
 
 
-    if (color < 0 || !OkColorHi(color) || !sp->_coloron) {
-       result = ERR;
-    } else {
-       int c_r, c_g, c_b;
+       if (color >= 0 && OkColorHi(color) && sp->_coloron) {
+           int c_r, c_g, c_b;
 
 
-       if (sp->_direct_color.value) {
-           rgb_bits_t *work = &(sp->_direct_color);
+           if (sp->_direct_color.value) {
+               rgb_bits_t *work = &(sp->_direct_color);
 
 #define max_direct_color(name) ((1 << work->bits.name) - 1)
 #define value_direct_color(max) (1000 * ((color >> bitoff) & max)) / max
 
 
 #define max_direct_color(name) ((1 << work->bits.name) - 1)
 #define value_direct_color(max) (1000 * ((color >> bitoff) & max)) / max
 
-           int max_r = max_direct_color(red);
-           int max_g = max_direct_color(green);
-           int max_b = max_direct_color(blue);
+               int max_r = max_direct_color(red);
+               int max_g = max_direct_color(green);
+               int max_b = max_direct_color(blue);
 
 
-           int bitoff = 0;
+               int bitoff = 0;
 
 
-           c_b = value_direct_color(max_b);
-           bitoff += work->bits.blue;
+               c_b = value_direct_color(max_b);
+               bitoff += work->bits.blue;
 
 
-           c_g = value_direct_color(max_g);
-           bitoff += work->bits.green;
+               c_g = value_direct_color(max_g);
+               bitoff += work->bits.green;
 
 
-           c_r = value_direct_color(max_r);
+               c_r = value_direct_color(max_r);
 
 
-       } else {
-           c_r = sp->_color_table[color].red;
-           c_g = sp->_color_table[color].green;
-           c_b = sp->_color_table[color].blue;
-       }
+           } else {
+               c_r = sp->_color_table[color].red;
+               c_g = sp->_color_table[color].green;
+               c_b = sp->_color_table[color].blue;
+           }
+
+           if (r)
+               *r = c_r;
+           if (g)
+               *g = c_g;
+           if (b)
+               *b = c_b;
 
 
+           TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
+                            color, c_r, c_g, c_b));
+           result = OK;
+       }
+    }
+    if (result != OK) {
        if (r)
        if (r)
-           *r = c_r;
+           *r = 0;
        if (g)
        if (g)
-           *g = c_g;
+           *g = 0;
        if (b)
        if (b)
-           *b = c_b;
-
-       TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
-                        color, c_r, c_g, c_b));
-       result = OK;
+           *b = 0;
     }
     returnCode(result);
 }
     }
     returnCode(result);
 }
@@ -1004,7 +1008,7 @@ NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
        if (set_color_pair) {
            TPUTS_TRACE("set_color_pair");
            NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
        if (set_color_pair) {
            TPUTS_TRACE("set_color_pair");
            NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
-                                   TPARM_1(set_color_pair, pair),
+                                   TIPARM_1(set_color_pair, pair),
                                    1, outc);
            return;
        } else if (SP_PARM != 0) {
                                    1, outc);
            return;
        } else if (SP_PARM != 0) {