GnuCash c935c2f+
Loading...
Searching...
No Matches
gnucash-color.c
1/********************************************************************\
2 * gnucash-color.c -- color handling for table cells *
3 * *
4 * This program is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU General Public License as *
6 * published by the Free Software Foundation; either version 2 of *
7 * the License, or (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License*
15 * along with this program; if not, contact: *
16 * *
17 * Free Software Foundation Voice: +1-617-542-5942 *
18 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19 * Boston, MA 02110-1301, USA gnu@gnu.org *
20 * *
21\********************************************************************/
22
23/*
24 * Shamelessly stolen from Gnumeric and modified
25 *
26 * Heath Martin <martinh@pegasus.cc.ucf.edu>
27 *
28 * color.c: Color allocation on the Gnumeric spreadsheet
29 *
30 * Author:
31 * Miguel de Icaza (miguel@kernel.org)
32 *
33 * We keep our own color context, as the color allocation might take place
34 * before any of our Canvases are realized.
35 */
36
37#ifdef HAVE_CONFIG_H
38#include <config.h>
39#endif
40
41#include <gdk/gdk.h>
42#include "gnucash-color.h"
43
44static int color_inited;
45
46/* Public Colors */
47GdkRGBA gn_white, gn_black, gn_light_gray;
48GdkRGBA gn_dark_gray, gn_blue, gn_red, gn_yellow;
49
50static GHashTable *color_hash_table = NULL;
51
52static guint
53color_hash (gconstpointer v)
54{
55 const guint32 *c = (guint32 *) v;
56
57 return *c;
58}
59
60
61static gint
62color_equal (gconstpointer v, gconstpointer w)
63{
64 const guint32 *c1 = (guint32 *) v;
65 const guint32 *c2 = (guint32 *) w;
66
67 return (*c1 == *c2);
68}
69
70
71/* This function takes an argb spec for a color and returns an
72 * allocated GdkRGBA. We take care of allocating and managing
73 * the colors. Caller must not touch the returned color.
74 */
75GdkRGBA *
77{
78 GdkRGBA *color;
79 const guint32 key = argb;
80 guint32 *newkey;
81
82 color = g_hash_table_lookup (color_hash_table, &key);
83
84 if (color)
85 return color;
86
87 color = g_new0(GdkRGBA, 1);
88 newkey = g_new0(guint32, 1);
89
90 *newkey = key;
91
92 color->red = ((argb & 0xff0000) >> 8)/ 65535.0;
93 color->green = (argb & 0xff00) / 65535.0;
94 color->blue = ((argb & 0xff) << 8) / 65535.0;
95 color->alpha = 1.0;
96
97 g_hash_table_insert (color_hash_table, newkey, color);
98
99 return color;
100}
101
102
103void
104gnucash_color_init (void)
105{
106 /* Allocate the default colors */
107 gdk_rgba_parse (&gn_white, "white");
108 gdk_rgba_parse (&gn_black, "black");
109
110 gdk_rgba_parse (&gn_light_gray, "gray60");
111 gdk_rgba_parse (&gn_dark_gray, "gray40");
112 gdk_rgba_parse (&gn_blue, "blue");
113 gdk_rgba_parse (&gn_red, "red");
114 gdk_rgba_parse (&gn_yellow, "yellow");
115
116 if (!color_hash_table)
117 color_hash_table = g_hash_table_new (color_hash, color_equal);
118
119 color_inited = 1;
120}
121
122
Convenience wrapper around GdkRGBA for use in Register Gnome classes.
GdkRGBA * gnucash_color_argb_to_gdk(guint32 argb)
Return the pixel value for the given red, green and blue.