Update Spanish translation
[gnumeric.git] / src / style-color.c
blobb8b2f30fc3c27b76fc9a467c3c78faf5f8f28e70
1 /*
2 * color.c: Color allocation on the Gnumeric spreadsheet
4 * Author:
5 * Miguel de Icaza (miguel@kernel.org)
7 */
8 #include <gnumeric-config.h>
9 #include <gnumeric.h>
10 #include <style-color.h>
11 #include <style-border.h>
13 static GHashTable *style_color_hash;
14 static GnmColor *sc_black;
15 static GnmColor *sc_white;
16 static GnmColor *sc_grid;
17 static GnmColor *sc_auto_back;
18 static GnmColor *sc_auto_font;
19 static GnmColor *sc_auto_pattern;
21 static GnmColor *
22 gnm_color_make (GOColor c, gboolean is_auto)
24 GnmColor key, *sc;
26 is_auto = !!is_auto;
28 key.go_color = c;
29 key.is_auto = is_auto;
31 sc = g_hash_table_lookup (style_color_hash, &key);
32 if (!sc) {
33 sc = g_new (GnmColor, 1);
34 sc->go_color = c;
35 sc->is_auto = is_auto;
36 sc->ref_count = 1;
38 g_hash_table_insert (style_color_hash, sc, sc);
39 } else
40 sc->ref_count++;
42 return sc;
45 GnmColor *
46 gnm_color_new_rgba16 (guint16 red, guint16 green, guint16 blue, guint16 alpha)
48 return gnm_color_new_rgba8 (red >> 8, green >> 8, blue >> 8, alpha >> 8);
51 GnmColor *
52 gnm_color_new_pango (PangoColor const *c)
54 return gnm_color_new_rgba16 (c->red, c->green, c->blue, 0xffff);
57 GnmColor *
58 gnm_color_new_gdk (GdkRGBA const *c)
61 * The important property here is that a color #rrggbb
62 * (i.e., an 8-bit color) roundtrips correctly when
63 * translated into GdkRGBA using /255 and back. Using
64 * multiplication by 256 here makes rounding unnecessary.
67 guint8 r8 = CLAMP (c->red * 256, 0, 255);
68 guint8 g8 = CLAMP (c->green * 256, 0, 255);
69 guint8 b8 = CLAMP (c->blue * 256, 0, 255);
70 guint8 a8 = CLAMP (c->alpha * 256, 0, 255);
72 return gnm_color_new_rgba8 (r8, g8, b8, a8);
75 GnmColor *
76 gnm_color_new_rgba8 (guint8 red, guint8 green, guint8 blue, guint8 alpha)
78 return gnm_color_new_go (GO_COLOR_FROM_RGBA (red, green, blue, alpha));
81 GnmColor *
82 gnm_color_new_rgb8 (guint8 red, guint8 green, guint8 blue)
84 return gnm_color_new_rgba8 (red, green, blue, 0xff);
87 GnmColor *
88 gnm_color_new_go (GOColor c)
90 return gnm_color_make (c, FALSE);
93 GnmColor *
94 gnm_color_new_auto (GOColor c)
96 return gnm_color_make (c, TRUE);
99 GnmColor *
100 style_color_black (void)
102 if (!sc_black)
103 sc_black = gnm_color_new_rgb8 (0, 0, 0);
104 return style_color_ref (sc_black);
107 GnmColor *
108 style_color_white (void)
110 if (!sc_white)
111 sc_white = gnm_color_new_rgb8 (0xff, 0xff, 0xff);
112 return style_color_ref (sc_white);
115 GnmColor *
116 style_color_grid (void)
118 if (!sc_grid)
119 sc_grid = gnm_color_new_rgb8 (0xc7, 0xc7, 0xc7);
120 return style_color_ref (sc_grid);
124 * Support for Excel auto-colors.
128 * style_color_auto_font:
130 * Always black, as far as we know.
132 GnmColor *
133 style_color_auto_font (void)
135 if (!sc_auto_font)
136 sc_auto_font = gnm_color_new_auto (GO_COLOR_BLACK);
137 return style_color_ref (sc_auto_font);
141 * style_color_auto_back:
143 * Always white, as far as we know.
145 GnmColor *
146 style_color_auto_back (void)
148 if (!sc_auto_back)
149 sc_auto_back = gnm_color_new_auto (GO_COLOR_WHITE);
150 return style_color_ref (sc_auto_back);
154 * style_color_auto_pattern:
156 * Normally black, but follows grid color if so told.
158 GnmColor *
159 style_color_auto_pattern (void)
161 if (!sc_auto_pattern)
162 sc_auto_pattern = gnm_color_new_auto (GO_COLOR_BLACK);
163 return style_color_ref (sc_auto_pattern);
166 GnmColor *
167 style_color_ref (GnmColor *sc)
169 if (sc != NULL)
170 sc->ref_count++;
172 return sc;
175 void
176 style_color_unref (GnmColor *sc)
178 if (sc == NULL)
179 return;
181 g_return_if_fail (sc->ref_count > 0);
183 sc->ref_count--;
184 if (sc->ref_count != 0)
185 return;
187 g_hash_table_remove (style_color_hash, sc);
188 g_free (sc);
191 GType
192 gnm_color_get_type (void)
194 static GType t = 0;
196 if (t == 0)
197 t = g_boxed_type_register_static ("GnmColor",
198 (GBoxedCopyFunc)style_color_ref,
199 (GBoxedFreeFunc)style_color_unref);
200 return t;
203 gint
204 style_color_equal (GnmColor const *k1, GnmColor const *k2)
206 return (k1->go_color == k2->go_color &&
207 k1->is_auto == k2->is_auto);
210 static guint
211 color_hash (gconstpointer v)
213 GnmColor const *k = (GnmColor const *)v;
214 return k->go_color ^ k->is_auto;
218 * gnm_color_init: (skip)
220 void
221 gnm_color_init (void)
223 style_color_hash = g_hash_table_new (color_hash,
224 (GEqualFunc)style_color_equal);
227 static void
228 cb_color_leak (gpointer key, gpointer value, gpointer user_data)
230 GnmColor *color = value;
232 g_printerr ("Leaking style-color at %p [%08x].\n",
233 (void *)color,
234 color->go_color);
238 * gnm_color_shutdown: (skip)
240 void
241 gnm_color_shutdown (void)
243 style_color_unref (sc_black);
244 sc_black = NULL;
246 style_color_unref (sc_white);
247 sc_white = NULL;
249 style_color_unref (sc_grid);
250 sc_grid = NULL;
252 style_color_unref (sc_auto_back);
253 sc_auto_back = NULL;
255 style_color_unref (sc_auto_font);
256 sc_auto_font = NULL;
258 style_color_unref (sc_auto_pattern);
259 sc_auto_pattern = NULL;
261 g_hash_table_foreach (style_color_hash, cb_color_leak, NULL);
262 g_hash_table_destroy (style_color_hash);
263 style_color_hash = NULL;