]> git.cworth.org Git - acre/commitdiff
Use CIE L*a*b colors instead of HSV
authorCarl Worth <cworth@cworth.org>
Tue, 3 Feb 2009 16:45:01 +0000 (08:45 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 16 Apr 2009 19:06:52 +0000 (12:06 -0700)
This gives perceptually uniform colors, (rather than totally
different lightness as one gets with HSV---which is based on
RGB primaries).

Thanks to LittleCMS for the colorspace conversion.

Makefile
acre.c

index 1d5beef2a8dabef652cc03c4e54fe8cc6cfa6a1f..d17e7d93ca81ef9e4b0ca1ce17b92e414756f441 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,9 +7,9 @@ acre_test_SOURCES = \
        xmalloc.c \
        xmalloc.h
 
-acre_test_LDFLAGS=$$(pkg-config --libs pangocairo)
+acre_test_LDFLAGS=$$(pkg-config --libs pangocairo lcms)
 
-acre_test_CFLAGS=$$(pkg-config --cflags pangocairo) \
+acre_test_CFLAGS=$$(pkg-config --cflags pangocairo lcms) \
        -Wall -Wextra -Wpointer-arith -Wstrict-prototypes \
        -Wmissing-prototypes -Wmissing-declarations \
        -Wnested-externs -Wno-unused-parameter
diff --git a/acre.c b/acre.c
index bda8e81996c3e72cdfad5e634a7ddf132ee0902e..b844d755153911710ebad19570b710a06d37a2db 100644 (file)
--- a/acre.c
+++ b/acre.c
@@ -28,6 +28,8 @@
 #include <stdarg.h>
 #include <math.h>
 
+#include <lcms.h>
+
 typedef struct _acre_data_point_2d {
     double x;
     double y;
@@ -569,8 +571,18 @@ _acre_color_from_hsv (acre_color_t *color,
 static void
 _choose_colors (acre_t *acre)
 {
-    double hue, saturation, value;
+    cmsHPROFILE lab_profile, srgb_profile;
+    cmsHTRANSFORM lab_to_srgb;
     int i;
+    double theta, radius, srgb[3];
+    cmsCIELab lab;
+
+    lab_profile = cmsCreateLabProfile (NULL); /* D50 */
+    srgb_profile = cmsCreate_sRGBProfile ();
+
+    lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
+                                     srgb_profile, TYPE_RGB_DBL,
+                                     INTENT_PERCEPTUAL, 0);
 
     acre->num_colors = acre->num_data;
 
@@ -580,13 +592,23 @@ _choose_colors (acre_t *acre)
                                 acre->colors_size * sizeof (acre_color_t));
     }
 
-    saturation = 1.0;
-    value = 0.7;
+    lab.L = 36;
+    radius = 130;
     for (i = 0; i < acre->num_colors; i++) {
-       hue = 360 * (double) i / acre->num_colors;
-       _acre_color_from_hsv (&acre->colors[i],
-                             hue, saturation, value);
+       theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
+       lab.a = radius * cos (theta);
+       lab.b = radius * sin (theta);
+
+       cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
+
+       acre->colors[i].red = srgb[0];
+       acre->colors[i].green = srgb[1];
+       acre->colors[i].blue = srgb[2];
     }
+
+    cmsDeleteTransform (lab_to_srgb);
+    cmsCloseProfile (lab_profile);
+    cmsCloseProfile (srgb_profile);
 }
 
 static void