2 * color.c: Color allocation on the Gnumeric spreadsheet
5 * Miguel de Icaza (miguel@kernel.org)
8 #include <gnumeric-config.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
;
22 gnm_color_make (GOColor c
, gboolean is_auto
)
29 key
.is_auto
= is_auto
;
31 sc
= g_hash_table_lookup (style_color_hash
, &key
);
33 sc
= g_new (GnmColor
, 1);
35 sc
->is_auto
= is_auto
;
38 g_hash_table_insert (style_color_hash
, sc
, sc
);
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);
52 gnm_color_new_pango (PangoColor
const *c
)
54 return gnm_color_new_rgba16 (c
->red
, c
->green
, c
->blue
, 0xffff);
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
);
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
));
82 gnm_color_new_rgb8 (guint8 red
, guint8 green
, guint8 blue
)
84 return gnm_color_new_rgba8 (red
, green
, blue
, 0xff);
88 gnm_color_new_go (GOColor c
)
90 return gnm_color_make (c
, FALSE
);
94 gnm_color_new_auto (GOColor c
)
96 return gnm_color_make (c
, TRUE
);
100 style_color_black (void)
103 sc_black
= gnm_color_new_rgb8 (0, 0, 0);
104 return style_color_ref (sc_black
);
108 style_color_white (void)
111 sc_white
= gnm_color_new_rgb8 (0xff, 0xff, 0xff);
112 return style_color_ref (sc_white
);
116 style_color_grid (void)
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.
133 style_color_auto_font (void)
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.
146 style_color_auto_back (void)
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.
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
);
167 style_color_ref (GnmColor
*sc
)
176 style_color_unref (GnmColor
*sc
)
181 g_return_if_fail (sc
->ref_count
> 0);
184 if (sc
->ref_count
!= 0)
187 g_hash_table_remove (style_color_hash
, sc
);
192 gnm_color_get_type (void)
197 t
= g_boxed_type_register_static ("GnmColor",
198 (GBoxedCopyFunc
)style_color_ref
,
199 (GBoxedFreeFunc
)style_color_unref
);
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
);
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)
221 gnm_color_init (void)
223 style_color_hash
= g_hash_table_new (color_hash
,
224 (GEqualFunc
)style_color_equal
);
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",
238 * gnm_color_shutdown: (skip)
241 gnm_color_shutdown (void)
243 style_color_unref (sc_black
);
246 style_color_unref (sc_white
);
249 style_color_unref (sc_grid
);
252 style_color_unref (sc_auto_back
);
255 style_color_unref (sc_auto_font
);
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
;