summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-03-11 20:20:07 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-03-11 20:20:39 -0400
commitfd5651a159da880b0c378ae9d2a2b4ac1c0614b3 (patch)
treebe73e0434fa425161fa4d95b3fad304453147247
parentd7391436ff96ff51e2417dc7edcf13ccb8300c30 (diff)
downloadkd-forest-fd5651a159da880b0c378ae9d2a2b4ac1c0614b3.tar.xz
Refactor hue comparator into color.c.
-rw-r--r--color.c22
-rw-r--r--color.h4
-rw-r--r--main.c29
3 files changed, 27 insertions, 28 deletions
diff --git a/color.c b/color.c
index 34eab01..d75d58f 100644
--- a/color.c
+++ b/color.c
@@ -11,6 +11,7 @@
#include "color.h"
#include <math.h>
+#define PI 3.1415926535897932
void
color_unpack(uint8_t pixel[3], uint32_t color)
@@ -117,3 +118,24 @@ color_set_Luv(double coords[3], uint32_t color)
coords[1] = 13.0*coords[0]*(uprime - unprime);
coords[2] = 13.0*coords[0]*(vprime - vnprime);
}
+
+static double
+hue(uint32_t color)
+{
+ double RGB[3];
+ color_set_RGB(RGB, color);
+
+ double hue = atan2(sqrt(3.0)*(RGB[1] - RGB[2]), 2*RGB[0] - RGB[1] - RGB[2]);
+ if (hue < 0.0) {
+ hue += 2.0*PI;
+ }
+ return hue;
+}
+
+int
+color_comparator(const void *a, const void *b)
+{
+ double ahue = hue(*(uint32_t *)a);
+ double bhue = hue(*(uint32_t *)b);
+ return (ahue > bhue) - (ahue < bhue);
+}
diff --git a/color.h b/color.h
index 6ce5168..6fec82d 100644
--- a/color.h
+++ b/color.h
@@ -15,6 +15,7 @@
#include "kd-forest.h"
#include <stdint.h>
+// Unpack a color into 8-bit RGB values
void color_unpack(uint8_t pixel[3], uint32_t color);
// Use RGB coordinates
@@ -24,4 +25,7 @@ void color_set_Lab(double coords[3], uint32_t color);
// Use CIE L*u*v* coordinates
void color_set_Luv(double coords[3], uint32_t color);
+// For sorting by hue
+int color_comparator(const void *a, const void *b);
+
#endif // COLOR_H
diff --git a/main.c b/main.c
index d2f3871..e186bc8 100644
--- a/main.c
+++ b/main.c
@@ -109,33 +109,6 @@ remove_non_boundary(kd_forest_t *kdf, kd_node_t *node, unsigned int width, unsig
}
}
-#if HUE_SORT
-#define PI 3.1415926535897932
-
-static double
-hue(uint32_t color)
-{
- int R = (color >> 16) & 0xFF;
- int G = (color >> 8) & 0xFF;
- int B = color & 0xFF;
-
- double hue = atan2(sqrt(3.0)*(G - B), 2*R - G - B);
- if (hue < 0.0) {
- hue += 2.0*PI;
- }
- return hue;
-}
-
-static int
-hue_comparator(const void *a, const void *b)
-{
- double ahue = hue(*(uint32_t *)a);
- double bhue = hue(*(uint32_t *)b);
- return (ahue > bhue) - (ahue < bhue);
-}
-
-#endif
-
int
main(void)
{
@@ -166,7 +139,7 @@ main(void)
}
#endif
#if HUE_SORT
- qsort(colors, size, sizeof(uint32_t), hue_comparator);
+ qsort(colors, size, sizeof(uint32_t), color_comparator);
#endif
// Make the actual bitmap image