[gtk/wip/baedert/for-master: 83/99] colorutils: Refactor hsv_to_rgb and fold into only caller




commit 319fa09cf3a9ae63edb3ba003c8333b7ea98a96c
Author: Timm Bäder <mail baedert org>
Date:   Sat Sep 26 09:59:27 2020 +0200

    colorutils: Refactor hsv_to_rgb and fold into only caller

 gtk/gtkcolorutils.c | 138 ++++++++++++++++++++++------------------------------
 1 file changed, 57 insertions(+), 81 deletions(-)
---
diff --git a/gtk/gtkcolorutils.c b/gtk/gtkcolorutils.c
index b027f37ab0..9b15eea997 100644
--- a/gtk/gtkcolorutils.c
+++ b/gtk/gtkcolorutils.c
@@ -34,80 +34,6 @@
 
 #include "gtkcolorutils.h"
 
-
-/* Converts from HSV to RGB */
-static void
-hsv_to_rgb (float *h,
-            float *s,
-            float *v)
-{
-  float hue, saturation, value;
-  float f, p, q, t;
-
-  if (*s == 0.0)
-    {
-      *h = *v;
-      *s = *v;
-      *v = *v; /* heh */
-    }
-  else
-    {
-      hue = *h * 6.0;
-      saturation = *s;
-      value = *v;
-
-      if (hue == 6.0)
-        hue = 0.0;
-
-      f = hue - (int) hue;
-      p = value * (1.0 - saturation);
-      q = value * (1.0 - saturation * f);
-      t = value * (1.0 - saturation * (1.0 - f));
-
-      switch ((int) hue)
-        {
-        case 0:
-          *h = value;
-          *s = t;
-          *v = p;
-          break;
-
-        case 1:
-          *h = q;
-          *s = value;
-          *v = p;
-          break;
-
-        case 2:
-          *h = p;
-          *s = value;
-          *v = t;
-          break;
-
-        case 3:
-          *h = p;
-          *s = q;
-          *v = value;
-          break;
-
-        case 4:
-          *h = t;
-          *s = p;
-          *v = value;
-          break;
-
-        case 5:
-          *h = value;
-          *s = p;
-          *v = q;
-          break;
-
-        default:
-          g_assert_not_reached ();
-        }
-    }
-}
-
 /* Converts from RGB to HSV */
 static void
 rgb_to_hsv (float *r,
@@ -201,20 +127,70 @@ void
 gtk_hsv_to_rgb (float  h, float  s, float  v,
                 float *r, float *g, float *b)
 {
+  float hue;
+  float f, p;
+  int ihue;
+
   g_return_if_fail (h >= 0.0 && h <= 1.0);
   g_return_if_fail (s >= 0.0 && s <= 1.0);
   g_return_if_fail (v >= 0.0 && v <= 1.0);
+  g_return_if_fail (r);
+  g_return_if_fail (g);
+  g_return_if_fail (b);
+
+  if (s == 0.0)
+    {
+      *r = v;
+      *g = v;
+      *b = v;
+      return;
+    }
 
-  hsv_to_rgb (&h, &s, &v);
+  hue = h * 6.0;
 
-  if (r)
-    *r = h;
+  if (hue == 6.0)
+    hue = 0.0;
 
-  if (g)
-    *g = s;
+  ihue = (int)hue;
+  f = hue - ihue;
+  p = v * (1.0 - s);
 
-  if (b)
-    *b = v;
+  if (ihue == 0)
+    {
+      *r = v;
+      *g = v * (1.0 - s * (1.0 - f));
+      *b = p;
+    }
+  else if (ihue == 1)
+    {
+      *r = v * (1.0 - s * f);
+      *g = v;
+      *b = p;
+    }
+  else if (ihue == 2)
+    {
+      *r = p;
+      *g = v;
+      *b = v * (1.0 - s * (1.0 - f));
+    }
+  else if (ihue == 3)
+    {
+      *r = p;
+      *g = v * (1.0 - s * f);
+      *b = v;
+    }
+  else if (ihue == 4)
+    {
+      *r = v * (1.0 - s * (1.0 - f));
+      *g = p;
+      *b = v;
+    }
+  else if (ihue == 5)
+    {
+      *r = v;
+      *g = p;
+      *b = v * (1.0 - s * f);
+    }
 }
 
 /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]