1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * color.c: Color allocation on the Gnumeric spreadsheet
6 * Miguel de Icaza (miguel@kernel.org)
9 #include <gnumeric-config.h>
11 #include "style-color.h"
12 #include "style-border.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
;
24 gnm_color_make (GOColor c
, gboolean is_auto
)
31 key
.is_auto
= is_auto
;
33 sc
= g_hash_table_lookup (style_color_hash
, &key
);
35 sc
= g_new (GnmColor
, 1);
37 sc
->is_auto
= is_auto
;
40 g_hash_table_insert (style_color_hash
, sc
, sc
);
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);
54 gnm_color_new_pango (PangoColor
const *c
)
56 return gnm_color_new_rgba16 (c
->red
, c
->green
, c
->blue
, 0xffff);
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
);
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
));
84 gnm_color_new_rgb8 (guint8 red
, guint8 green
, guint8 blue
)
86 return gnm_color_new_rgba8 (red
, green
, blue
, 0xff);
90 gnm_color_new_go (GOColor c
)
92 return gnm_color_make (c
, FALSE
);
96 gnm_color_new_auto (GOColor c
)
98 return gnm_color_make (c
, TRUE
);
102 style_color_black (void)
105 sc_black
= gnm_color_new_rgb8 (0, 0, 0);
106 return style_color_ref (sc_black
);
110 style_color_white (void)
113 sc_white
= gnm_color_new_rgb8 (0xff, 0xff, 0xff);
114 return style_color_ref (sc_white
);
118 style_color_grid (void)
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.
135 style_color_auto_font (void)
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.
148 style_color_auto_back (void)
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.
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
);
169 style_color_ref (GnmColor
*sc
)
178 style_color_unref (GnmColor
*sc
)
183 g_return_if_fail (sc
->ref_count
> 0);
186 if (sc
->ref_count
!= 0)
189 g_hash_table_remove (style_color_hash
, sc
);
194 gnm_color_get_type (void)
199 t
= g_boxed_type_register_static ("GnmColor",
200 (GBoxedCopyFunc
)style_color_ref
,
201 (GBoxedFreeFunc
)style_color_unref
);
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
);
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)
223 gnm_color_init (void)
225 style_color_hash
= g_hash_table_new (color_hash
,
226 (GEqualFunc
)style_color_equal
);
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",
240 * gnm_color_shutdown: (skip)
243 gnm_color_shutdown (void)
246 style_color_unref (sc_black
);
251 style_color_unref (sc_white
);
256 style_color_unref (sc_grid
);
261 style_color_unref (sc_auto_back
);
266 style_color_unref (sc_auto_font
);
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
;