[cogl] color: Store components as floats and make public



commit 17b52690b8e37941fafbba67e1983714a8b4ecd5
Author: Robert Bragg <robert linux intel com>
Date:   Mon Aug 6 21:16:08 2012 +0100

    color: Store components as floats and make public
    
    Instead of storing the CoglColor components as bytes we now store them
    as single precision floats and also make the members public. The
    intention is to evolve the cogl_color_ api into a more useful utility
    api for color space manipulations.
    
    Reviewed-by: Neil Roberts <neil linux intel com>

 cogl/cogl-color-private.h          |    4 +-
 cogl/cogl-color.c                  |  164 ++++++++++++++++--------------------
 cogl/cogl-color.h                  |   99 ++++++++++------------
 cogl/cogl-pipeline-private.h       |    4 -
 cogl/cogl-pipeline-state-private.h |   17 ++++
 cogl/cogl-pipeline-state.c         |   11 ---
 cogl/cogl-types.h                  |   20 +----
 7 files changed, 140 insertions(+), 179 deletions(-)
---
diff --git a/cogl/cogl-color-private.h b/cogl/cogl-color-private.h
index 591ff73..e0f0a57 100644
--- a/cogl/cogl-color-private.h
+++ b/cogl/cogl-color-private.h
@@ -38,8 +38,8 @@
 #define _COGL_COLOR_DATA_SIZE 4
 
 void
-_cogl_color_get_rgba_4ubv (const CoglColor *color,
-                           uint8_t *dest);
+_cogl_color_get_rgba_4fv (const CoglColor *color,
+                          float *dest);
 
 #endif /* __COGL_COLOR_PRIVATE_PRIVATE_H */
 
diff --git a/cogl/cogl-color.c b/cogl/cogl-color.c
index ed4dd46..0919a75 100644
--- a/cogl/cogl-color.c
+++ b/cogl/cogl-color.c
@@ -3,7 +3,7 @@
  *
  * An object oriented GL/GLES Abstraction/Utility Layer
  *
- * Copyright (C) 2008,2009 Intel Corporation.
+ * Copyright (C) 2008,2009,2012 Intel Corporation.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -16,7 +16,8 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
  *
  *
  */
@@ -31,28 +32,6 @@
 #include "cogl-color.h"
 #include "cogl-color-private.h"
 
-CoglColor *
-cogl_color_new (void)
-{
-  return g_slice_new (CoglColor);
-}
-
-CoglColor *
-cogl_color_copy (const CoglColor *color)
-{
-  if (G_LIKELY (color))
-    return g_slice_dup (CoglColor, color);
-
-  return NULL;
-}
-
-void
-cogl_color_free (CoglColor *color)
-{
-  if (G_LIKELY (color))
-    g_slice_free (CoglColor, color);
-}
-
 void
 cogl_color_init_from_4ub (CoglColor *color,
                           uint8_t red,
@@ -60,12 +39,10 @@ cogl_color_init_from_4ub (CoglColor *color,
                           uint8_t blue,
                           uint8_t alpha)
 {
-  _COGL_RETURN_IF_FAIL (color != NULL);
-
-  color->red   = red;
-  color->green = green;
-  color->blue  = blue;
-  color->alpha = alpha;
+  color->red = red / 255.0f;
+  color->green = green / 255.0f;
+  color->blue = blue / 255.0f;
+  color->alpha = alpha / 255.0f;
 }
 
 void
@@ -75,188 +52,181 @@ cogl_color_init_from_4f (CoglColor *color,
                          float blue,
                          float alpha)
 {
-  _COGL_RETURN_IF_FAIL (color != NULL);
-
-  color->red   =  (red * 255);
-  color->green =  (green * 255);
-  color->blue  =  (blue * 255);
-  color->alpha =  (alpha * 255);
+  color->red = red;
+  color->green = green;
+  color->blue = blue;
+  color->alpha = alpha;
 }
 
 void
 cogl_color_init_from_4fv (CoglColor *color,
                           float *color_array)
 {
-  _COGL_RETURN_IF_FAIL (color != NULL);
-
-  color->red   =  (color_array[0] * 255);
-  color->green =  (color_array[1] * 255);
-  color->blue  =  (color_array[2] * 255);
-  color->alpha =  (color_array[3] * 255);
+  memcpy (color, color_array, sizeof (CoglColor));
 }
 
-unsigned char
+uint8_t
 cogl_color_get_red_byte (const CoglColor *color)
 {
-  return color->red;
+  return color->red * 255;
 }
 
 float
 cogl_color_get_red_float (const CoglColor *color)
 {
-  return (float) color->red / 255.0;
+  return color->red;
 }
 
 float
 cogl_color_get_red (const CoglColor *color)
 {
-  return  ((float) color->red / 255.0);
+  return color->red;
 }
 
-unsigned char
+uint8_t
 cogl_color_get_green_byte (const CoglColor *color)
 {
-  return color->green;
+  return color->green * 255;
 }
 
 float
 cogl_color_get_green_float (const CoglColor *color)
 {
-  return (float) color->green / 255.0;
+  return color->green;
 }
 
 float
 cogl_color_get_green (const CoglColor *color)
 {
-  return  ((float) color->green / 255.0);
+  return color->green;
 }
 
-unsigned char
+uint8_t
 cogl_color_get_blue_byte (const CoglColor *color)
 {
-  return color->blue;
+  return color->blue * 255;
 }
 
 float
 cogl_color_get_blue_float (const CoglColor *color)
 {
-  return (float) color->blue / 255.0;
+  return color->blue;
 }
 
 float
 cogl_color_get_blue (const CoglColor *color)
 {
-  return  ((float) color->blue / 255.0);
+  return color->blue;
 }
 
-unsigned char
+uint8_t
 cogl_color_get_alpha_byte (const CoglColor *color)
 {
-  return color->alpha;
+  return color->alpha * 255;
 }
 
 float
 cogl_color_get_alpha_float (const CoglColor *color)
 {
-  return (float) color->alpha / 255.0;
+  return color->alpha;
 }
 
 float
 cogl_color_get_alpha (const CoglColor *color)
 {
-  return  ((float) color->alpha / 255.0);
+  return color->alpha;
 }
 
 void
-cogl_color_set_red_byte (CoglColor     *color,
-                         unsigned char  red)
+cogl_color_set_red_byte (CoglColor *color,
+                         uint8_t red)
 {
-  color->red = red;
+  color->red = red / 255.0f;
 }
 
 void
 cogl_color_set_red_float (CoglColor *color,
-                          float      red)
+                          float red)
 {
-  color->red = red * 255.0;
+  color->red = red;
 }
 
 void
 cogl_color_set_red (CoglColor *color,
-                    float      red)
+                    float red)
 {
-  color->red = red * 255.0;
+  color->red = red;
 }
 
 void
-cogl_color_set_green_byte (CoglColor     *color,
-                           unsigned char  green)
+cogl_color_set_green_byte (CoglColor *color,
+                           uint8_t green)
 {
-  color->green = green;
+  color->green = green / 255.0f;
 }
 
 void
 cogl_color_set_green_float (CoglColor *color,
                             float green)
 {
-  color->green = green * 255.0;
+  color->green = green;
 }
 
 void
 cogl_color_set_green (CoglColor *color,
                       float green)
 {
-  color->green = green * 255.0;
+  color->green = green;
 }
 
 void
 cogl_color_set_blue_byte (CoglColor *color,
-                          unsigned char blue)
+                          uint8_t blue)
 {
-  color->blue = blue;
+  color->blue = blue / 255.0f;
 }
 
 void
 cogl_color_set_blue_float (CoglColor *color,
                            float blue)
 {
-  color->blue = blue * 255.0;
+  color->blue = blue;
 }
 
 void
 cogl_color_set_blue (CoglColor *color,
                      float blue)
 {
-  color->blue = blue * 255.0;
+  color->blue = blue;
 }
 
 void
 cogl_color_set_alpha_byte (CoglColor *color,
-                           unsigned char  alpha)
+                           uint8_t alpha)
 {
-  color->alpha = alpha;
+  color->alpha = alpha / 255.0f;
 }
 
 void
 cogl_color_set_alpha_float (CoglColor *color,
                             float alpha)
 {
-  color->alpha = alpha * 255.0;
+  color->alpha = alpha;
 }
 
 void
 cogl_color_set_alpha (CoglColor *color,
                       float alpha)
 {
-  color->alpha = alpha * 255.0;
+  color->alpha = alpha;
 }
 
 void
 cogl_color_premultiply (CoglColor *color)
 {
-  color->red = (color->red * color->alpha + 128) / 255;
-  color->green = (color->green * color->alpha + 128) / 255;
-  color->blue = (color->blue * color->alpha + 128) / 255;
+  color->red *= color->alpha;
+  color->green *= color->alpha;
+  color->blue *= color->alpha;
 }
 
 void
@@ -264,28 +234,40 @@ cogl_color_unpremultiply (CoglColor *color)
 {
   if (color->alpha != 0)
     {
-      color->red = (color->red * 255) / color->alpha;
-      color->green = (color->green * 255) / color->alpha;
-      color->blue = (color->blue * 255) / color->alpha;
+      color->red /= color->alpha;
+      color->green /= color->alpha;
+      color->blue /= color->alpha;
     }
 }
 
 CoglBool
 cogl_color_equal (const void *v1, const void *v2)
 {
-  const uint32_t *c1 = v1, *c2 = v2;
-
   _COGL_RETURN_VAL_IF_FAIL (v1 != NULL, FALSE);
   _COGL_RETURN_VAL_IF_FAIL (v2 != NULL, FALSE);
 
-  /* XXX: We don't compare the padding */
-  return *c1 == *c2 ? TRUE : FALSE;
+  return memcmp (v1, v2, sizeof (CoglColor)) == 0;
+}
+
+CoglColor *
+cogl_color_copy (const CoglColor *color)
+{
+  if (G_LIKELY (color))
+    return g_slice_dup (CoglColor, color);
+
+  return NULL;
 }
 
 void
-_cogl_color_get_rgba_4ubv (const CoglColor *color,
-                           uint8_t *dest)
+cogl_color_free (CoglColor *color)
 {
-  memcpy (dest, color, 4);
+  if (G_LIKELY (color))
+    g_slice_free (CoglColor, color);
 }
 
+void
+_cogl_color_get_rgba_4fv (const CoglColor *color,
+                          float *dest)
+{
+  memcpy (dest, color, sizeof (CoglColor));
+}
diff --git a/cogl/cogl-color.h b/cogl/cogl-color.h
index 3bff8fe..0a2e3ff 100644
--- a/cogl/cogl-color.h
+++ b/cogl/cogl-color.h
@@ -3,7 +3,7 @@
  *
  * An object oriented GL/GLES Abstraction/Utility Layer
  *
- * Copyright (C) 2008,2009 Intel Corporation.
+ * Copyright (C) 2008,2009,2012 Intel Corporation.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -43,44 +43,6 @@
 G_BEGIN_DECLS
 
 /**
- * cogl_color_new:
- *
- * Creates a new (empty) color
- *
- * Return value: a newly-allocated #CoglColor. Use cogl_color_free()
- *   to free the allocated resources
- *
- * Since: 1.0
- */
-CoglColor *
-cogl_color_new (void);
-
-/**
- * cogl_color_copy:
- * @color: the color to copy
- *
- * Creates a copy of @color
- *
- * Return value: a newly-allocated #CoglColor. Use cogl_color_free()
- *   to free the allocate resources
- *
- * Since: 1.0
- */
-CoglColor *
-cogl_color_copy (const CoglColor *color);
-
-/**
- * cogl_color_free:
- * @color: the color to free
- *
- * Frees the resources allocated by cogl_color_new() and cogl_color_copy()
- *
- * Since: 1.0
- */
-void
-cogl_color_free (CoglColor *color);
-
-/**
  * cogl_color_init_from_4ub:
  * @color: A pointer to a #CoglColor to initialize
  * @red: value of the red channel, between 0 and 255
@@ -142,7 +104,7 @@ cogl_color_init_from_4fv (CoglColor *color,
  *
  * Since: 1.0
  */
-unsigned char
+uint8_t
 cogl_color_get_red_byte (const CoglColor *color);
 
 /**
@@ -156,7 +118,7 @@ cogl_color_get_red_byte (const CoglColor *color);
  *
  * Since: 1.0
  */
-unsigned char
+uint8_t
 cogl_color_get_green_byte (const CoglColor *color);
 
 /**
@@ -170,7 +132,7 @@ cogl_color_get_green_byte (const CoglColor *color);
  *
  * Since: 1.0
  */
-unsigned char
+uint8_t
 cogl_color_get_blue_byte (const CoglColor *color);
 
 /**
@@ -184,7 +146,7 @@ cogl_color_get_blue_byte (const CoglColor *color);
  *
  * Since: 1.0
  */
-unsigned char
+uint8_t
 cogl_color_get_alpha_byte (const CoglColor *color);
 
 /**
@@ -309,8 +271,8 @@ cogl_color_get_alpha (const CoglColor *color);
  * Since: 1.4
  */
 void
-cogl_color_set_red_byte (CoglColor     *color,
-                         unsigned char  red);
+cogl_color_set_red_byte (CoglColor *color,
+                         uint8_t red);
 
 /**
  * cogl_color_set_green_byte:
@@ -322,8 +284,8 @@ cogl_color_set_red_byte (CoglColor     *color,
  * Since: 1.4
  */
 void
-cogl_color_set_green_byte (CoglColor     *color,
-                           unsigned char  green);
+cogl_color_set_green_byte (CoglColor *color,
+                           uint8_t green);
 
 /**
  * cogl_color_set_blue_byte:
@@ -335,8 +297,8 @@ cogl_color_set_green_byte (CoglColor     *color,
  * Since: 1.4
  */
 void
-cogl_color_set_blue_byte (CoglColor     *color,
-                          unsigned char  blue);
+cogl_color_set_blue_byte (CoglColor *color,
+                          uint8_t blue);
 
 /**
  * cogl_color_set_alpha_byte:
@@ -348,8 +310,8 @@ cogl_color_set_blue_byte (CoglColor     *color,
  * Since: 1.4
  */
 void
-cogl_color_set_alpha_byte (CoglColor     *color,
-                           unsigned char  alpha);
+cogl_color_set_alpha_byte (CoglColor *color,
+                           uint8_t alpha);
 
 /**
  * cogl_color_set_red_float:
@@ -362,7 +324,7 @@ cogl_color_set_alpha_byte (CoglColor     *color,
  */
 void
 cogl_color_set_red_float (CoglColor *color,
-                          float      red);
+                          float red);
 
 /**
  * cogl_color_set_green_float:
@@ -375,7 +337,7 @@ cogl_color_set_red_float (CoglColor *color,
  */
 void
 cogl_color_set_green_float (CoglColor *color,
-                            float      green);
+                            float green);
 
 /**
  * cogl_color_set_blue_float:
@@ -388,7 +350,7 @@ cogl_color_set_green_float (CoglColor *color,
  */
 void
 cogl_color_set_blue_float (CoglColor *color,
-                           float      blue);
+                           float blue);
 
 /**
  * cogl_color_set_alpha_float:
@@ -401,7 +363,7 @@ cogl_color_set_blue_float (CoglColor *color,
  */
 void
 cogl_color_set_alpha_float (CoglColor *color,
-                            float      alpha);
+                            float alpha);
 
 /**
  * cogl_color_set_red:
@@ -414,7 +376,7 @@ cogl_color_set_alpha_float (CoglColor *color,
  */
 void
 cogl_color_set_red (CoglColor *color,
-                    float      red);
+                    float red);
 
 /**
  * cogl_color_set_green:
@@ -498,6 +460,31 @@ cogl_color_unpremultiply (CoglColor *color);
 CoglBool
 cogl_color_equal (const void *v1, const void *v2);
 
+/**
+ * cogl_color_copy:
+ * @color: the color to copy
+ *
+ * Creates a copy of @color
+ *
+ * Return value: a newly-allocated #CoglColor. Use cogl_color_free()
+ *   to free the allocate resources
+ *
+ * Since: 1.0
+ */
+CoglColor *
+cogl_color_copy (const CoglColor *color);
+
+/**
+ * cogl_color_free:
+ * @color: the color to free
+ *
+ * Frees the resources allocated by cogl_color_new() and cogl_color_copy()
+ *
+ * Since: 1.0
+ */
+void
+cogl_color_free (CoglColor *color);
+
 G_END_DECLS
 
 #endif /* __COGL_COLOR_H__ */
diff --git a/cogl/cogl-pipeline-private.h b/cogl/cogl-pipeline-private.h
index 26b7cf1..3c2f0a5 100644
--- a/cogl/cogl-pipeline-private.h
+++ b/cogl/cogl-pipeline-private.h
@@ -832,10 +832,6 @@ _cogl_pipeline_set_vertend (CoglPipeline *pipeline, int vertend);
 CoglPipeline *
 _cogl_pipeline_get_parent (CoglPipeline *pipeline);
 
-void
-_cogl_pipeline_get_colorubv (CoglPipeline *pipeline,
-                             uint8_t       *color);
-
 /* XXX: At some point it could be good for this to accept a mask of
  * the state groups we are interested in comparing since we can
  * probably use that information in a number situations to reduce
diff --git a/cogl/cogl-pipeline-state-private.h b/cogl/cogl-pipeline-state-private.h
index 3954f23..8c8b2b8 100644
--- a/cogl/cogl-pipeline-state-private.h
+++ b/cogl/cogl-pipeline-state-private.h
@@ -28,6 +28,23 @@
 #ifndef __COGL_PIPELINE_STATE_PRIVATE_H
 #define __COGL_PIPELINE_STATE_PRIVATE_H
 
+#include "cogl-pipeline-private.h"
+
+/* This is used heavily by the cogl journal when logging quads */
+static inline void
+_cogl_pipeline_get_colorubv (CoglPipeline *pipeline,
+                             uint8_t *color)
+{
+  CoglPipeline *authority =
+    _cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_COLOR);
+  const CoglColor *cogl_color = &authority->color;
+
+  color[0] = cogl_color->red * 255;
+  color[1] = cogl_color->green * 255;
+  color[2] = cogl_color->blue * 255;
+  color[3] = cogl_color->alpha * 255;
+}
+
 CoglBool
 _cogl_pipeline_maybe_has_custom_texture_transform (CoglPipeline *pipeline);
 
diff --git a/cogl/cogl-pipeline-state.c b/cogl/cogl-pipeline-state.c
index b0a83de..b8e53dc 100644
--- a/cogl/cogl-pipeline-state.c
+++ b/cogl/cogl-pipeline-state.c
@@ -315,17 +315,6 @@ cogl_pipeline_get_color (CoglPipeline *pipeline,
   *color = authority->color;
 }
 
-/* This is used heavily by the cogl journal when logging quads */
-void
-_cogl_pipeline_get_colorubv (CoglPipeline *pipeline,
-                             uint8_t *color)
-{
-  CoglPipeline *authority =
-    _cogl_pipeline_get_authority (pipeline, COGL_PIPELINE_STATE_COLOR);
-
-  _cogl_color_get_rgba_4ubv (&authority->color, color);
-}
-
 void
 cogl_pipeline_set_color (CoglPipeline    *pipeline,
 			 const CoglColor *color)
diff --git a/cogl/cogl-types.h b/cogl/cogl-types.h
index 7684412..9e61ca4 100644
--- a/cogl/cogl-types.h
+++ b/cogl/cogl-types.h
@@ -398,26 +398,16 @@ typedef enum
 /**
  * CoglColor:
  *
- * A structure for holding a color definition. The contents of
- * the CoglColor structure are private and should never by accessed
- * directly.
+ * A structure for holding a single color definition.
  *
  * Since: 1.0
  */
 struct _CoglColor
 {
-  /*< private >*/
-  uint8_t COGL_PRIVATE (red);
-  uint8_t COGL_PRIVATE (green);
-  uint8_t COGL_PRIVATE (blue);
-
-  uint8_t COGL_PRIVATE (alpha);
-
-  /* padding in case we want to change to floats at
-   * some point */
-  uint32_t COGL_PRIVATE (padding0);
-  uint32_t COGL_PRIVATE (padding1);
-  uint32_t COGL_PRIVATE (padding2);
+  float red;
+  float green;
+  float blue;
+  float alpha;
 };
 COGL_STRUCT_SIZE_ASSERT (CoglColor, 16);
 



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