[babl] extensions: Don't perform strict comparisons on floating point values



commit da3b41274052c23b44ad8eceb9d99106d2739f3a
Author: Téo Mazars <teo mazars ensimag fr>
Date:   Sat Aug 3 18:15:15 2013 +0200

    extensions: Don't perform strict comparisons on floating point values

 extensions/HSL.c |    7 ++++---
 extensions/HSV.c |    9 +++++----
 2 files changed, 9 insertions(+), 7 deletions(-)
---
diff --git a/extensions/HSL.c b/extensions/HSL.c
index 5426474..827e028 100644
--- a/extensions/HSL.c
+++ b/extensions/HSL.c
@@ -25,6 +25,7 @@
 
 #define MIN(a,b) ((a > b) ? b : a)
 #define MAX(a,b) ((a < b) ? b : a)
+#define EPSILON  1.0e-10
 
 static long  rgba_to_hsla     (char   *src,
                                char   *dst,
@@ -121,16 +122,16 @@ rgb_to_hsl_step (double* src,
   max = MAX (red, MAX (green, blue));
   min = MIN (red, MIN (green, blue));
 
-  if (max == red)
+  if (max - red < EPSILON)
     cpn_max = 0;
-  else if (max == green)
+  else if (max - green < EPSILON)
     cpn_max = 1;
   else
     cpn_max = 2;
 
   lightness = (max + min) / 2.0;
 
-  if (max == min)
+  if (max - min < EPSILON)
     {
       hue = saturation = 0;
     }
diff --git a/extensions/HSV.c b/extensions/HSV.c
index a4ddd81..c830a0c 100644
--- a/extensions/HSV.c
+++ b/extensions/HSV.c
@@ -30,6 +30,7 @@
 
 #define MIN(a,b) (a > b) ? b : a;
 #define MAX(a,b) (a < b) ? b : a;
+#define EPSILON  1.0e-10
 
 static long rgba_to_hsva     (char *src,
                               char *dst,
@@ -183,25 +184,25 @@ rgba_to_hsv_step (char *src,
 
   chroma = value - min;
 
-  if (value == 0.0)
+  if (value < EPSILON)
     saturation = 0.0;
   else
     saturation = chroma / value;
 
-  if (saturation == 0.0)
+  if (saturation < EPSILON)
     {
       hue = 0.0;
     }
   else
     {
-      if (red == value)
+      if (fabs (red - value) < EPSILON)
         {
           hue = (green - blue) / chroma;
 
           if (hue < 0.0)
             hue += 6.0;
         }
-      else if (green == value)
+      else if (fabs (green - value) < EPSILON)
         hue = 2.0 + (blue - red) / chroma;
       else
         hue = 4.0 + (red - green) / chroma;


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