From: Bruno Haible Date: Mon, 4 Dec 2006 13:26:39 +0000 (+0000) Subject: Better choice of colors for 8-color models. X-Git-Tag: v0.17~613 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f94cd6cd3a52973a0691b20006d820c168541427;p=thirdparty%2Fgettext.git Better choice of colors for 8-color models. --- diff --git a/gnulib-local/ChangeLog b/gnulib-local/ChangeLog index 8762b93f6..fcce7852b 100644 --- a/gnulib-local/ChangeLog +++ b/gnulib-local/ChangeLog @@ -1,3 +1,11 @@ +2006-12-01 Bruno Haible + + Preserve the hue of bright colors. + * lib/term-ostream.oo.c (colors_of_common8): New variable. + (rgb_to_color_common8): Rewritten. + (colors_of_xterm8): New variable. + (rgb_to_color_xterm8): Rewritten. + 2006-12-01 Bruno Haible * lib/term-ostream.oo.c (out_attr_change): Fix typo. diff --git a/gnulib-local/lib/term-ostream.oo.c b/gnulib-local/lib/term-ostream.oo.c index 1d66a09d6..1f1efbeeb 100644 --- a/gnulib-local/lib/term-ostream.oo.c +++ b/gnulib-local/lib/term-ostream.oo.c @@ -310,10 +310,40 @@ rgb_to_color_monochrome () COLOR_MAGENTA 101 COLOR_YELLOW 110 COLOR_WHITE 111 */ +static const rgb_t colors_of_common8[8] = +{ + /* R G B grey index */ + { 0, 0, 0 }, /* 0.000 0 */ + { 0, 0, 255 }, + { 0, 255, 0 }, + { 0, 255, 255 }, + { 255, 0, 0 }, + { 255, 0, 255 }, + { 255, 255, 0 }, + { 255, 255, 255 } /* 1.000 7 */ +}; + static inline term_color_t rgb_to_color_common8 (int r, int g, int b) { - return (r >= 128 ? 4 : 0) + (g >= 128 ? 2 : 0) + (b >= 128 ? 1 : 0); + rgb_t color; + hsv_t hsv; + + color.red = r; color.green = g; color.blue = b; + rgb_to_hsv (color, &hsv); + + if (hsv.saturation < 0.065f) + { + /* Greyscale approximation. */ + float luminance = color_luminance (r, g, b); + if (luminance < 0.500f) + return 0; + else + return 7; + } + else + /* Color approximation. */ + return nearest_color (color, colors_of_common8, 8); } /* Convert a cm_common8 color in RGB encoding to BGR encoding. @@ -337,10 +367,41 @@ color_bgr (term_color_t color) COLOR_MAGENTA 101 COLOR_CYAN 110 COLOR_WHITE 111 */ +static const rgb_t colors_of_xterm8[8] = +{ + /* The real xterm's colors are dimmed; assume full-brightness instead. */ + /* R G B grey index */ + { 0, 0, 0 }, /* 0.000 0 */ + { 255, 0, 0 }, + { 0, 255, 0 }, + { 255, 255, 0 }, + { 0, 0, 255 }, + { 255, 0, 255 }, + { 0, 255, 255 }, + { 255, 255, 255 } /* 1.000 7 */ +}; + static inline term_color_t rgb_to_color_xterm8 (int r, int g, int b) { - return (r >= 128 ? 1 : 0) + (g >= 128 ? 2 : 0) + (b >= 128 ? 4 : 0); + rgb_t color; + hsv_t hsv; + + color.red = r; color.green = g; color.blue = b; + rgb_to_hsv (color, &hsv); + + if (hsv.saturation < 0.065f) + { + /* Greyscale approximation. */ + float luminance = color_luminance (r, g, b); + if (luminance < 0.500f) + return 0; + else + return 7; + } + else + /* Color approximation. */ + return nearest_color (color, colors_of_xterm8, 8); } /* ------------------------ cm_xterm16 color model ------------------------ */