42G_DEFINE_TYPE(ColorPalette, color_palette, G_TYPE_OBJECT)
58 int non_empty_lines = 0;
59 char last_char =
'\n';
65 for (idx = 0; idx < length && data[idx]; idx++) {
66 if (data[idx] ==
'\n' && last_char !=
'\n')
68 last_char = data[idx];
72 if (data[idx-1] !=
'\n')
75 return non_empty_lines;
89 unsigned int color_idx;
91 const char *char_array;
98 if (!palette || !resource_name)
101 data = g_resources_lookup_data(resource_name, 0, NULL);
106 char_array = (
const char *)g_bytes_get_data(data, &byte_count);
108 if (!char_array || !byte_count)
117 palette->color_array = (GdkRGBA *)malloc(
sizeof(GdkRGBA) * (
unsigned int)lines);
120 regex = g_regex_new(
"^(?<red>[0-9A-Fa-f][0-9A-Fa-f])(?<green>[0-9A-Fa-f][0-9A-Fa-f])(?<blue>[0-9A-Fa-f][0-9A-Fa-f])$",
131 for (idx = 0 ; (
unsigned int)idx < byte_count; idx++) {
133 line[line_idx] = char_array[idx];
136 if (line[line_idx] ==
'\n' || line[line_idx] ==
'\0') {
137 line[line_idx] =
'\0';
140 g_regex_match(regex, line, 0, &mi);
141 if (g_match_info_matches(mi) && color_idx < (
unsigned int)lines) {
142 match = g_match_info_fetch_named(mi,
"red");
143 palette->color_array[color_idx].red =
144 (double)g_ascii_strtoll(match, NULL, 16) / 255.0;
146 match = g_match_info_fetch_named(mi,
"green");
147 palette->color_array[color_idx].green =
148 (double)g_ascii_strtoll(match, NULL, 16) / 255.0;
150 match = g_match_info_fetch_named(mi,
"blue");
151 palette->color_array[color_idx].blue =
152 (double)g_ascii_strtoll(match, NULL, 16) / 255.0;
156 palette->color_array[color_idx].alpha = 1.0;
161 g_match_info_free(mi);
164 if (char_array[idx] ==
'\0')
174 line_idx += ((
unsigned int)line_idx <
sizeof(line)-1 ? 1 : 0);
178 palette->color_array = realloc(palette->color_array, (
size_t)color_idx *
sizeof(GdkRGBA));
179 palette->color_array_length = color_idx;
181 g_regex_unref(regex);
190 ColorPalette *palette;
206 if (index >= palette->color_array_length)
212 c = (GdkRGBA *)malloc(
sizeof(GdkRGBA));
215 c->red = palette->color_array[index].red;
216 c->green = palette->color_array[index].green;
217 c->blue = palette->color_array[index].blue;
218 c->alpha = palette->color_array[index].alpha;
225 unsigned int return_val = 0;
228 return_val = palette->color_array_length;
235 ColorPalette *palette;
237 palette = GDS_RENDER_COLOR_PALETTE(gobj);
238 if (palette->color_array) {
239 palette->color_array_length = 0;
240 free(palette->color_array);
241 palette->color_array = NULL;
245 G_OBJECT_CLASS(color_palette_parent_class)->dispose(gobj);
250 GObjectClass *gclass;
252 gclass = G_OBJECT_CLASS(klass);
258 self->color_array = NULL;
259 self->color_array_length = 0;
unsigned int color_palette_get_color_count(ColorPalette *palette)
Return amount of stored colors in palette.
ColorPalette * color_palette_new_from_resource(char *resource_name)
Create a new object with from a resource containing the html hex color scheme.
static int count_non_empty_lines_in_array(const char *data, size_t length)
Return the number of non empty lines in array.
static void color_palette_dispose(GObject *gobj)
static int color_palette_fill_with_resource(ColorPalette *palette, char *resource_name)
color_palette_fill_with_resource
GdkRGBA * color_palette_get_color(ColorPalette *palette, GdkRGBA *color, unsigned int index)
Get the n-th color in the palette identified by the index.
static void color_palette_init(ColorPalette *self)
static void color_palette_class_init(ColorPaletteClass *klass)
Class representing a color palette.
#define TYPE_GDS_RENDER_COLOR_PALETTE
GdkRGBA * color_array
The internal array to store the colors.
unsigned int color_array_length
The length of the _ColorPalette::color_array array.