[gnome-color-manager] Add GcmCellRendererColor to display an XYZ encoded color



commit 991e24b9b1938bcf68caaf129cc8cc58370e30fc
Author: Richard Hughes <richard hughsie com>
Date:   Mon Apr 11 20:27:57 2011 +0100

    Add GcmCellRendererColor to display an XYZ encoded color

 src/Makefile.am               |    2 +
 src/gcm-cell-renderer-color.c |  193 +++++++++++++++++++++++++++++++++++++++++
 src/gcm-cell-renderer-color.h |   60 +++++++++++++
 3 files changed, 255 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index acb426c..6ae6b26 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -162,6 +162,8 @@ gcm_import_CFLAGS =					\
 gcm_viewer_SOURCES =					\
 	gcm-cell-renderer-profile-text.c		\
 	gcm-cell-renderer-profile-text.h		\
+	gcm-cell-renderer-color.c			\
+	gcm-cell-renderer-color.h			\
 	gcm-viewer.c
 
 gcm_viewer_LDADD =					\
diff --git a/src/gcm-cell-renderer-color.c b/src/gcm-cell-renderer-color.c
new file mode 100644
index 0000000..15f44f7
--- /dev/null
+++ b/src/gcm-cell-renderer-color.c
@@ -0,0 +1,193 @@
+/* -*- 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 <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <lcms2.h>
+
+#include "gcm-cell-renderer-color.h"
+
+enum {
+	PROP_0,
+	PROP_COLOR,
+	PROP_PROFILE,
+	PROP_LAST
+};
+
+G_DEFINE_TYPE (GcmCellRendererColor, gcm_cell_renderer_color, GTK_TYPE_CELL_RENDERER_PIXBUF)
+
+static gpointer parent_class = NULL;
+
+static void
+gcm_cell_renderer_color_get_property (GObject *object, guint param_id,
+				      GValue *value, GParamSpec *pspec)
+{
+	GcmCellRendererColor *renderer = GCM_CELL_RENDERER_COLOR (object);
+
+	switch (param_id) {
+	case PROP_COLOR:
+		g_value_set_boxed (value, g_boxed_copy (CD_TYPE_COLOR_XYZ,
+							renderer->color));
+		break;
+	case PROP_PROFILE:
+		g_value_set_object (value, renderer->profile);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+		break;
+	}
+}
+
+static void
+gcm_cell_renderer_set_color (GcmCellRendererColor *renderer)
+{
+	CdColorRGB8 rgb;
+	GdkPixbuf *pixbuf = NULL;
+	gint height = 26; /* TODO: needs to be a property */
+	gint width = 400; /* TODO: needs to be a property */
+	gint x, y;
+	guchar *pixels;
+	guint pos;
+	cmsHPROFILE profile_srgb = NULL;
+	cmsHPROFILE profile_xyz = NULL;
+	cmsHTRANSFORM xform = NULL;
+
+	/* nothing set yet */
+	if (renderer->color == NULL)
+		goto out;
+
+	/* convert the color to sRGB */
+	profile_xyz = cmsCreateXYZProfile ();
+	profile_srgb = cmsCreate_sRGBProfile ();
+	xform = cmsCreateTransform (profile_xyz, TYPE_XYZ_DBL,
+				    profile_srgb, TYPE_RGB_8,
+				    INTENT_ABSOLUTE_COLORIMETRIC, 0);
+	cmsDoTransform (xform, renderer->color, &rgb, 1);
+
+	/* create a pixbuf of the right size */
+	pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
+	width = gdk_pixbuf_get_width (pixbuf);
+	height = gdk_pixbuf_get_height (pixbuf);
+	pixels = gdk_pixbuf_get_pixels (pixbuf);
+	for (y=0; y<height; y++) {
+		for (x=0; x<width; x++) {
+			pos = (y*width+x) * 3;
+			pixels[pos+0] = rgb.R;
+			pixels[pos+1] = rgb.G;
+			pixels[pos+2] = rgb.B;
+		}
+	}
+out:
+	g_object_set (renderer, "pixbuf", pixbuf, NULL);
+	if (profile_srgb != NULL)
+		cmsCloseProfile (profile_srgb);
+	if (profile_xyz != NULL)
+		cmsCloseProfile (profile_xyz);
+	if (xform != NULL)
+		cmsDeleteTransform (xform);
+	if (pixbuf != NULL)
+		g_object_unref (pixbuf);
+}
+
+static void
+gcm_cell_renderer_color_set_property (GObject *object, guint param_id,
+				      const GValue *value, GParamSpec *pspec)
+{
+	GcmCellRendererColor *renderer = GCM_CELL_RENDERER_COLOR (object);
+
+	switch (param_id) {
+	case PROP_COLOR:
+		cd_color_copy_xyz (g_value_get_boxed (value), renderer->color);
+		gcm_cell_renderer_set_color (renderer);
+		break;
+	case PROP_PROFILE:
+		if (renderer->profile != NULL)
+			g_object_unref (renderer->profile);
+		renderer->color = g_value_dup_object (value);
+		gcm_cell_renderer_set_color (renderer);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+		break;
+	}
+}
+
+/**
+ * gcm_cell_renderer_finalize:
+ * @object: The object to finalize
+ **/
+static void
+gcm_cell_renderer_finalize (GObject *object)
+{
+	GcmCellRendererColor *renderer;
+	renderer = GCM_CELL_RENDERER_COLOR (object);
+	g_free (renderer->icon_name);
+	cd_color_xyz_free (renderer->color);
+	if (renderer->profile != NULL)
+		g_object_unref (renderer->profile);
+	G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gcm_cell_renderer_color_class_init (GcmCellRendererColorClass *class)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (class);
+	object_class->finalize = gcm_cell_renderer_finalize;
+
+	parent_class = g_type_class_peek_parent (class);
+
+	object_class->get_property = gcm_cell_renderer_color_get_property;
+	object_class->set_property = gcm_cell_renderer_color_set_property;
+
+	g_object_class_install_property (object_class, PROP_COLOR,
+					 g_param_spec_boxed ("color", NULL,
+					 NULL,
+					 CD_TYPE_COLOR_XYZ,
+					 G_PARAM_READWRITE));
+
+	g_object_class_install_property (object_class, PROP_PROFILE,
+					 g_param_spec_object ("profile", NULL,
+					 NULL,
+					 CD_TYPE_PROFILE,
+					 G_PARAM_READWRITE));
+}
+
+/**
+ * gcm_cell_renderer_color_init:
+ **/
+static void
+gcm_cell_renderer_color_init (GcmCellRendererColor *renderer)
+{
+	renderer->color = cd_color_xyz_new ();
+}
+
+/**
+ * gcm_cell_renderer_color_new:
+ **/
+GtkCellRenderer *
+gcm_cell_renderer_color_new (void)
+{
+	return g_object_new (GCM_TYPE_CELL_RENDERER_COLOR, NULL);
+}
+
diff --git a/src/gcm-cell-renderer-color.h b/src/gcm-cell-renderer-color.h
new file mode 100644
index 0000000..a6585cd
--- /dev/null
+++ b/src/gcm-cell-renderer-color.h
@@ -0,0 +1,60 @@
+/* -*- 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_CELL_RENDERER_COLOR_H
+#define GCM_CELL_RENDERER_COLOR_H
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+#include <colord.h>
+
+#define GCM_TYPE_CELL_RENDERER_COLOR		(gcm_cell_renderer_color_get_type())
+#define GCM_CELL_RENDERER_COLOR(obj)		(G_TYPE_CHECK_INSTANCE_CAST((obj), GCM_TYPE_CELL_RENDERER_COLOR, GcmCellRendererColor))
+#define GCM_CELL_RENDERER_COLOR_CLASS(cls)	(G_TYPE_CHECK_CLASS_CAST((cls), GCM_TYPE_CELL_RENDERER_COLOR, GcmCellRendererColorClass))
+#define GCM_IS_CELL_RENDERER_COLOR(obj)		(G_TYPE_CHECK_INSTANCE_TYPE((obj), GCM_TYPE_CELL_RENDERER_COLOR))
+#define GCM_IS_CELL_RENDERER_COLOR_CLASS(cls)	(G_TYPE_CHECK_CLASS_TYPE((cls), GCM_TYPE_CELL_RENDERER_COLOR))
+#define GCM_CELL_RENDERER_COLOR_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), GCM_TYPE_CELL_RENDERER_COLOR, GcmCellRendererColorClass))
+
+G_BEGIN_DECLS
+
+typedef struct _GcmCellRendererColor		GcmCellRendererColor;
+typedef struct _GcmCellRendererColorClass	GcmCellRendererColorClass;
+
+struct _GcmCellRendererColor
+{
+	GtkCellRendererPixbuf	 parent;
+	CdColorXYZ		*color;
+	CdProfile		*profile;
+	gchar			*icon_name;
+};
+
+struct _GcmCellRendererColorClass
+{
+	GtkCellRendererPixbufClass parent_class;
+};
+
+GType		 gcm_cell_renderer_color_get_type	(void);
+GtkCellRenderer	*gcm_cell_renderer_color_new		(void);
+
+G_END_DECLS
+
+#endif /* GCM_CELL_RENDERER_COLOR_H */
+



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