[gnome-color-manager] Add GcmNamedColor as an object to hold a named color value



commit bca7150b0a38cfc9739774a6472624838ad5f638
Author: Richard Hughes <richard hughsie com>
Date:   Mon Apr 11 09:49:44 2011 +0100

    Add GcmNamedColor as an object to hold a named color value
    
    Named color values consist of a title and a color XYZ value.

 src/Makefile.am       |    4 +
 src/gcm-named-color.c |  248 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcm-named-color.h |   62 ++++++++++++
 src/gcm-self-test.c   |   51 ++++++++++
 4 files changed, 365 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 097cb78..acb426c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -48,6 +48,8 @@ libgcmshared_a_SOURCES =				\
 	gcm-print.h					\
 	gcm-profile.c					\
 	gcm-profile.h					\
+	gcm-named-color.c				\
+	gcm-named-color.h				\
 	gcm-tables.c					\
 	gcm-tables.h					\
 	gcm-trc-widget.c				\
@@ -249,6 +251,8 @@ gcm_self_test_SOURCES =					\
 	gcm-calibrate-native.h				\
 	gcm-gamma-widget.c				\
 	gcm-gamma-widget.h				\
+	gcm-named-color.c				\
+	gcm-named-color.h				\
 	gcm-sample-window.c				\
 	gcm-sample-window.h				\
 	gcm-profile-store.c				\
diff --git a/src/gcm-named-color.c b/src/gcm-named-color.c
new file mode 100644
index 0000000..5458288
--- /dev/null
+++ b/src/gcm-named-color.c
@@ -0,0 +1,248 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2011 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <colord.h>
+
+#include "gcm-named-color.h"
+
+static void	gcm_named_color_class_init	(GcmNamedColorClass	*klass);
+static void	gcm_named_color_init		(GcmNamedColor	*named_color);
+static void	gcm_named_color_finalize	(GObject	*object);
+
+#define GCM_NAMED_COLOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GCM_TYPE_NAMED_COLOR, GcmNamedColorPrivate))
+
+/**
+ * GcmNamedColorPrivate:
+ *
+ * Private #GcmNamedColor data
+ **/
+struct _GcmNamedColorPrivate
+{
+	gchar			*title;
+	CdColorXYZ		*value;
+};
+
+enum {
+	PROP_0,
+	PROP_TITLE,
+	PROP_VALUE,
+	PROP_LAST
+};
+
+G_DEFINE_TYPE (GcmNamedColor, gcm_named_color, G_TYPE_OBJECT)
+
+/**
+ * gcm_named_color_get_title:
+ * @named_color: a #GcmNamedColor instance.
+ *
+ * Gets the named color title.
+ *
+ * Return value: A string, or %NULL for unset or invalid
+ **/
+const gchar *
+gcm_named_color_get_title (GcmNamedColor *named_color)
+{
+	g_return_val_if_fail (GCM_IS_NAMED_COLOR (named_color), NULL);
+	return named_color->priv->title;
+}
+
+/**
+ * gcm_named_color_set_title:
+ * @named_color: a #GcmNamedColor instance.
+ * @title: the new title for this color
+ *
+ * Sets the named color title.
+ **/
+void
+gcm_named_color_set_title (GcmNamedColor *named_color,
+			   const gchar *title)
+{
+	g_return_if_fail (GCM_IS_NAMED_COLOR (named_color));
+	g_free (named_color->priv->title);
+	named_color->priv->title = g_strdup (title);
+}
+
+/**
+ * gcm_named_color_get_value:
+ * @named_color: a #GcmNamedColor instance.
+ *
+ * Gets the named color value.
+ *
+ * Return value: An XYZ value, or %NULL for unset or invalid
+ **/
+const CdColorXYZ *
+gcm_named_color_get_value (GcmNamedColor *named_color)
+{
+	g_return_val_if_fail (GCM_IS_NAMED_COLOR (named_color), NULL);
+	return named_color->priv->value;
+}
+
+/**
+ * gcm_named_color_set_value:
+ * @named_color: a #GcmNamedColor instance.
+ * @value: the new value for this color
+ *
+ * Sets the named color color value.
+ **/
+void
+gcm_named_color_set_value (GcmNamedColor *named_color,
+			   const CdColorXYZ *value)
+{
+	g_return_if_fail (GCM_IS_NAMED_COLOR (named_color));
+	cd_color_xyz_free (named_color->priv->value);
+	named_color->priv->value = cd_color_xyz_dup (value);
+}
+
+/*
+ * gcm_named_color_set_property:
+ */
+static void
+gcm_named_color_set_property (GObject *object,
+			      guint prop_id,
+			      const GValue *value,
+			      GParamSpec *pspec)
+{
+	GcmNamedColor *named_color = GCM_NAMED_COLOR (object);
+
+	switch (prop_id) {
+	case PROP_TITLE:
+		gcm_named_color_set_title (named_color,
+					   g_value_get_string (value));
+		break;
+	case PROP_VALUE:
+		gcm_named_color_set_value (named_color,
+					   g_value_get_boxed (value));
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+/*
+ * gcm_named_color_get_property:
+ */
+static void
+gcm_named_color_get_property (GObject *object,
+			      guint prop_id,
+			      GValue *value,
+			      GParamSpec *pspec)
+{
+	GcmNamedColor *named_color = GCM_NAMED_COLOR (object);
+
+	switch (prop_id) {
+	case PROP_TITLE:
+		g_value_set_string (value, named_color->priv->title);
+		break;
+	case PROP_VALUE:
+		g_value_set_boxed (value, named_color->priv->value);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+    }
+}
+
+/*
+ * gcm_named_color_class_init:
+ */
+static void
+gcm_named_color_class_init (GcmNamedColorClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = gcm_named_color_finalize;
+	object_class->set_property = gcm_named_color_set_property;
+	object_class->get_property = gcm_named_color_get_property;
+
+	/**
+	 * GcmNamedColor:title:
+	 *
+	 * The color title.
+	 *
+	 * Since: 0.1.6
+	 **/
+	g_object_class_install_property (object_class,
+					 PROP_TITLE,
+					 g_param_spec_string ("title",
+							      NULL, NULL,
+							      NULL,
+							      G_PARAM_READWRITE));
+	/**
+	 * GcmNamedColor:value:
+	 *
+	 * The color value.
+	 *
+	 * Since: 0.1.6
+	 **/
+	g_object_class_install_property (object_class,
+					 PROP_VALUE,
+					 g_param_spec_boxed ("value",
+							     NULL, NULL,
+							     CD_TYPE_COLOR_XYZ,
+							     G_PARAM_READWRITE));
+
+	g_type_class_add_private (klass, sizeof (GcmNamedColorPrivate));
+}
+
+/*
+ * gcm_named_color_init:
+ */
+static void
+gcm_named_color_init (GcmNamedColor *named_color)
+{
+	named_color->priv = GCM_NAMED_COLOR_GET_PRIVATE (named_color);
+}
+
+/*
+ * gcm_named_color_finalize:
+ */
+static void
+gcm_named_color_finalize (GObject *object)
+{
+	GcmNamedColor *named_color;
+
+	g_return_if_fail (GCM_IS_NAMED_COLOR (object));
+
+	named_color = GCM_NAMED_COLOR (object);
+
+	g_free (named_color->priv->title);
+	cd_color_xyz_free (named_color->priv->value);
+
+	G_OBJECT_CLASS (gcm_named_color_parent_class)->finalize (object);
+}
+
+/**
+ * gcm_named_color_new:
+ *
+ * Creates a new #GcmNamedColor object.
+ *
+ * Return value: a new GcmNamedColor object.
+ **/
+GcmNamedColor *
+gcm_named_color_new (void)
+{
+	GcmNamedColor *named_color;
+	named_color = g_object_new (GCM_TYPE_NAMED_COLOR, NULL);
+	return GCM_NAMED_COLOR (named_color);
+}
diff --git a/src/gcm-named-color.h b/src/gcm-named-color.h
new file mode 100644
index 0000000..34d2140
--- /dev/null
+++ b/src/gcm-named-color.h
@@ -0,0 +1,62 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2011 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __GCM_NAMED_COLOR_H
+#define __GCM_NAMED_COLOR_H
+
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <colord.h>
+
+G_BEGIN_DECLS
+
+#define GCM_TYPE_NAMED_COLOR		(gcm_named_color_get_type ())
+#define GCM_NAMED_COLOR(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), GCM_TYPE_NAMED_COLOR, GcmNamedColor))
+#define GCM_NAMED_COLOR_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), GCM_TYPE_NAMED_COLOR, GcmNamedColorClass))
+#define GCM_IS_NAMED_COLOR(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), GCM_TYPE_NAMED_COLOR))
+
+typedef struct _GcmNamedColorPrivate GcmNamedColorPrivate;
+
+typedef struct
+{
+	 GObject		 parent;
+	 GcmNamedColorPrivate	*priv;
+} GcmNamedColor;
+
+typedef struct
+{
+	GObjectClass		 parent_class;
+} GcmNamedColorClass;
+
+GType		 gcm_named_color_get_type	(void);
+GcmNamedColor	*gcm_named_color_new		(void);
+
+const gchar	*gcm_named_color_get_title	(GcmNamedColor	*named_color);
+void		 gcm_named_color_set_title	(GcmNamedColor	*named_color,
+						 const gchar	*title);
+const CdColorXYZ *gcm_named_color_get_value	(GcmNamedColor	*named_color);
+void		 gcm_named_color_set_value	(GcmNamedColor	*named_color,
+						 const CdColorXYZ *value);
+
+G_END_DECLS
+
+#endif /* __GCM_NAMED_COLOR_H */
+
diff --git a/src/gcm-self-test.c b/src/gcm-self-test.c
index 0a7ebd5..97cf29f 100644
--- a/src/gcm-self-test.c
+++ b/src/gcm-self-test.c
@@ -24,6 +24,7 @@
 #include <glib-object.h>
 #include <math.h>
 #include <glib/gstdio.h>
+#include <stdlib.h>
 
 #include "gcm-brightness.h"
 #include "gcm-calibrate-dialog.h"
@@ -39,6 +40,7 @@
 #include "gcm-gamma-widget.h"
 #include "gcm-hull.h"
 #include "gcm-image.h"
+#include "gcm-named-color.h"
 #include "gcm-print.h"
 #include "gcm-profile.h"
 #include "gcm-profile-store.h"
@@ -1070,6 +1072,54 @@ gcm_test_utils_func (void)
 	g_assert (gcm_utils_device_kind_to_profile_kind (CD_DEVICE_KIND_UNKNOWN) == CD_PROFILE_KIND_UNKNOWN);
 }
 
+static void
+gcm_test_named_color_func (void)
+{
+	GcmNamedColor *nc;
+	CdColorXYZ *xyz;
+	CdColorXYZ *xyz2;
+	const CdColorXYZ *xyz_new;
+	gchar *tmp = NULL;
+
+	nc = gcm_named_color_new ();
+
+	gcm_named_color_set_title (nc, "Hello world");
+
+	xyz = cd_color_xyz_new ();
+
+	/* use setters */
+	cd_color_set_xyz (xyz, 0.1, 0.2, 0.3);
+	gcm_named_color_set_value (nc, xyz);
+
+	/* test getters */
+	g_assert_cmpstr (gcm_named_color_get_title (nc), ==, "Hello world");
+	xyz_new = gcm_named_color_get_value (nc);
+	g_assert_cmpfloat (abs (xyz_new->X - 0.1), <, 0.01);
+	g_assert_cmpfloat (abs (xyz_new->Y - 0.2), <, 0.01);
+	g_assert_cmpfloat (abs (xyz_new->Z - 0.3), <, 0.01);
+
+	/* overwrite using properties */
+	cd_color_set_xyz (xyz, 0.4, 0.5, 0.6);
+	g_object_set (nc,
+		      "title", "dave",
+		      "value", xyz,
+		      NULL);
+
+	/* test property getters */
+	g_object_get (nc,
+		      "title", &tmp,
+		      "value", &xyz2,
+		      NULL);
+	g_assert_cmpstr (gcm_named_color_get_title (nc), ==, "dave");
+	g_assert_cmpfloat (abs (xyz2->X - 0.4), <, 0.01);
+	g_assert_cmpfloat (abs (xyz2->Y - 0.5), <, 0.01);
+	g_assert_cmpfloat (abs (xyz2->Z - 0.6), <, 0.01);
+
+	g_free (tmp);
+	cd_color_xyz_free (xyz);
+	cd_color_xyz_free (xyz2);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -1081,6 +1131,7 @@ main (int argc, char **argv)
 	/* setup manually as we have no GMainContext */
 	gcm_debug_setup (g_getenv ("VERBOSE") != NULL);
 
+	g_test_add_func ("/color/named-color", gcm_test_named_color_func);
 	g_test_add_func ("/color/calibrate", gcm_test_calibrate_func);
 	g_test_add_func ("/color/exif", gcm_test_exif_func);
 	g_test_add_func ("/color/utils", gcm_test_utils_func);



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