]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Better choice of colors for 8-color models.
authorBruno Haible <bruno@clisp.org>
Mon, 4 Dec 2006 13:26:39 +0000 (13:26 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:14:26 +0000 (12:14 +0200)
gnulib-local/ChangeLog
gnulib-local/lib/term-ostream.oo.c

index 8762b93f6754e6d5226c0a5d000ac3ecf85ac8a2..fcce7852ba5d97a5b8e9bc2494444e2a32619461 100644 (file)
@@ -1,3 +1,11 @@
+2006-12-01  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        * lib/term-ostream.oo.c (out_attr_change): Fix typo.
index 1d66a09d6e8ce1dc95e7c096ddbee8fe81d9c734..1f1efbeeb3d556326555e35b7fbef68d9ef30794 100644 (file)
@@ -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 ------------------------ */