Introspection fixes
[gnumeric.git] / src / style-color.c
blobf1aedbe537a0c529842ab57d6398380c59d93dcc
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * color.c: Color allocation on the Gnumeric spreadsheet
5 * Author:
6 * Miguel de Icaza (miguel@kernel.org)
8 */
9 #include <gnumeric-config.h>
10 #include "gnumeric.h"
11 #include "style-color.h"
12 #include "style-border.h"
13 #include <gtk/gtk.h>
15 static GHashTable *style_color_hash;
16 static GnmColor *sc_black;
17 static GnmColor *sc_white;
18 static GnmColor *sc_grid;
19 static GnmColor *sc_auto_back;
20 static GnmColor *sc_auto_font;
21 static GnmColor *sc_auto_pattern;
23 static GnmColor *
24 gnm_color_make (GOColor c, gboolean is_auto)
26 GnmColor key, *sc;
28 is_auto = !!is_auto;
30 key.go_color = c;
31 key.is_auto = is_auto;
33 sc = g_hash_table_lookup (style_color_hash, &key);
34 if (!sc) {
35 sc = g_new (GnmColor, 1);
36 sc->go_color = c;
37 sc->is_auto = is_auto;
38 sc->ref_count = 1;
40 g_hash_table_insert (style_color_hash, sc, sc);
41 } else
42 sc->ref_count++;
44 return sc;
47 GnmColor *
48 gnm_color_new_rgba16 (guint16 red, guint16 green, guint16 blue, guint16 alpha)
50 return gnm_color_new_rgba8 (red >> 8, green >> 8, blue >> 8, alpha >> 8);
53 GnmColor *
54 gnm_color_new_pango (PangoColor const *c)
56 return gnm_color_new_rgba16 (c->red, c->green, c->blue, 0xffff);
59 GnmColor *
60 gnm_color_new_gdk (GdkRGBA const *c)
63 * The important property here is that a color #rrggbb
64 * (i.e., an 8-bit color) roundtrips correctly when
65 * translated into GdkRGBA using /255 and back. Using
66 * multiplication by 256 here makes rounding unnecessary.
69 guint8 r8 = CLAMP (c->red * 256, 0, 255);
70 guint8 g8 = CLAMP (c->green * 256, 0, 255);
71 guint8 b8 = CLAMP (c->blue * 256, 0, 255);
72 guint8 a8 = CLAMP (c->alpha * 256, 0, 255);
74 return gnm_color_new_rgba8 (r8, g8, b8, a8);
77 GnmColor *
78 gnm_color_new_rgba8 (guint8 red, guint8 green, guint8 blue, guint8 alpha)
80 return gnm_color_new_go (GO_COLOR_FROM_RGBA (red, green, blue, alpha));
83 GnmColor *
84 gnm_color_new_rgb8 (guint8 red, guint8 green, guint8 blue)
86 return gnm_color_new_rgba8 (red, green, blue, 0xff);
89 GnmColor *
90 gnm_color_new_go (GOColor c)
92 return gnm_color_make (c, FALSE);
95 GnmColor *
96 gnm_color_new_auto (GOColor c)
98 return gnm_color_make (c, TRUE);
101 GnmColor *
102 style_color_black (void)
104 if (!sc_black)
105 sc_black = gnm_color_new_rgb8 (0, 0, 0);
106 return style_color_ref (sc_black);
109 GnmColor *
110 style_color_white (void)
112 if (!sc_white)
113 sc_white = gnm_color_new_rgb8 (0xff, 0xff, 0xff);
114 return style_color_ref (sc_white);
117 GnmColor *
118 style_color_grid (void)
120 if (!sc_grid)
121 sc_grid = gnm_color_new_rgb8 (0xc7, 0xc7, 0xc7);
122 return style_color_ref (sc_grid);
126 * Support for Excel auto-colors.
130 * style_color_auto_font:
132 * Always black, as far as we know.
134 GnmColor *
135 style_color_auto_font (void)
137 if (!sc_auto_font)
138 sc_auto_font = gnm_color_new_auto (GO_COLOR_BLACK);
139 return style_color_ref (sc_auto_font);
143 * style_color_auto_back:
145 * Always white, as far as we know.
147 GnmColor *
148 style_color_auto_back (void)
150 if (!sc_auto_back)
151 sc_auto_back = gnm_color_new_auto (GO_COLOR_WHITE);
152 return style_color_ref (sc_auto_back);
156 * style_color_auto_pattern:
158 * Normally black, but follows grid color if so told.
160 GnmColor *
161 style_color_auto_pattern (void)
163 if (!sc_auto_pattern)
164 sc_auto_pattern = gnm_color_new_auto (GO_COLOR_BLACK);
165 return style_color_ref (sc_auto_pattern);
168 GnmColor *
169 style_color_ref (GnmColor *sc)
171 if (sc != NULL)
172 sc->ref_count++;
174 return sc;
177 void
178 style_color_unref (GnmColor *sc)
180 if (sc == NULL)
181 return;
183 g_return_if_fail (sc->ref_count > 0);
185 sc->ref_count--;
186 if (sc->ref_count != 0)
187 return;
189 g_hash_table_remove (style_color_hash, sc);
190 g_free (sc);
193 GType
194 gnm_color_get_type (void)
196 static GType t = 0;
198 if (t == 0)
199 t = g_boxed_type_register_static ("GnmColor",
200 (GBoxedCopyFunc)style_color_ref,
201 (GBoxedFreeFunc)style_color_unref);
202 return t;
205 gint
206 style_color_equal (GnmColor const *k1, GnmColor const *k2)
208 return (k1->go_color == k2->go_color &&
209 k1->is_auto == k2->is_auto);
212 static guint
213 color_hash (gconstpointer v)
215 GnmColor const *k = (GnmColor const *)v;
216 return k->go_color ^ k->is_auto;
220 * gnm_color_init: (skip)
222 void
223 gnm_color_init (void)
225 style_color_hash = g_hash_table_new (color_hash,
226 (GEqualFunc)style_color_equal);
229 static void
230 cb_color_leak (gpointer key, gpointer value, gpointer user_data)
232 GnmColor *color = value;
234 g_printerr ("Leaking style-color at %p [%08x].\n",
235 (void *)color,
236 color->go_color);
240 * gnm_color_shutdown: (skip)
242 void
243 gnm_color_shutdown (void)
245 if (sc_black) {
246 style_color_unref (sc_black);
247 sc_black = NULL;
250 if (sc_white) {
251 style_color_unref (sc_white);
252 sc_white = NULL;
255 if (sc_grid) {
256 style_color_unref (sc_grid);
257 sc_grid = NULL;
260 if (sc_auto_back) {
261 style_color_unref (sc_auto_back);
262 sc_auto_back = NULL;
265 if (sc_auto_font) {
266 style_color_unref (sc_auto_font);
267 sc_auto_font = NULL;
270 if (sc_auto_pattern) {
271 style_color_unref (sc_auto_pattern);
272 sc_auto_pattern = NULL;
275 g_hash_table_foreach (style_color_hash, cb_color_leak, NULL);
276 g_hash_table_destroy (style_color_hash);
277 style_color_hash = NULL;