[cairo-commit] src/cairo-color.c

Carl Worth cworth at kemper.freedesktop.org
Mon Oct 16 09:24:09 PDT 2006


 src/cairo-color.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)

New commits:
diff-tree b62710d4f8602203d848daf2d444865b611fff09 (from 71037f3612da9d11431567c05c17807499ab1746)
Author: Carl Worth <cworth at cworth.org>
Date:   Wed Aug 30 14:43:21 2006 -0700

    Bug #7497: Change _cairo_color_compute_shorts to not rely on any particular floating-point epsilon value.

diff --git a/src/cairo-color.c b/src/cairo-color.c
index e202af2..a348839 100644
--- a/src/cairo-color.c
+++ b/src/cairo-color.c
@@ -89,19 +89,27 @@ _cairo_color_init_rgb (cairo_color_t *co
     _cairo_color_init_rgba (color, red, green, blue, 1.0);
 }
 
-/* We multiply colors by (0x10000 - epsilon), such that we get a uniform
- * range even for 0xffff.  In other words, (1.0 - epsilon) would convert
- * to 0xffff, not 0xfffe.
+/* Convert a double in [0.0, 1.0] to an integer in [0, 65535]
+ * The conversion is designed to divide the input range into 65536
+ * equally-sized regions. This is achieved by multiplying by 65536 and
+ * then special-casing the result of an input value of 1.0 so that it
+ * maps to 65535 instead of 65536.
  */
-#define CAIRO_COLOR_ONE_MINUS_EPSILON (65536.0 - 1e-5)
+static inline uint16_t _color_to_short (double d)
+{
+    uint32_t i;
+    i = (uint32_t) (d * 65536);
+    i -= (i >> 16);
+    return i;
+}
 
 static void
 _cairo_color_compute_shorts (cairo_color_t *color)
 {
-    color->red_short   = color->red   * color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
-    color->green_short = color->green * color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
-    color->blue_short  = color->blue  * color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
-    color->alpha_short = color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
+    color->red_short   = _color_to_short (color->red   * color->alpha);
+    color->green_short = _color_to_short (color->green * color->alpha);
+    color->blue_short  = _color_to_short (color->blue  * color->alpha);
+    color->alpha_short = _color_to_short (color->alpha);
 }
 
 void


More information about the cairo-commit mailing list