static PyObject * pygimp_rgb_parse_css(PyObject *self, PyObject *args, PyObject *kwargs) { char *css; int len; GimpRGB rgb; gboolean success, with_alpha = FALSE; static char *kwlist[] = { "css", "with_alpha", NULL }; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i:rgb_parse_css", kwlist, &css, &len, &with_alpha)) return NULL; if (with_alpha) success = gimp_rgba_parse_css(&rgb, css, len); else { rgb.a = 1.0; success = gimp_rgb_parse_css(&rgb, css, len); } if (!success) { PyErr_SetString(PyExc_ValueError, "unable to parse CSS color"); return NULL; } return pygimp_rgb_new(&rgb); }
int main (void) { gint failures = 0; gint i; g_print ("\nTesting the GIMP color parser ...\n"); for (i = 0; i < G_N_ELEMENTS (samples); i++) { GimpRGB rgb = { 0.0, 0.0, 0.0, 0.0 }; gboolean success; if (samples[i].alpha) success = gimp_rgba_parse_css (&rgb, samples[i].str, -1); else success = gimp_rgb_parse_css (&rgb, samples[i].str, -1); failures += check_failure (samples + i, success, &rgb); } if (failures) { g_print ("%d out of %d samples failed!\n\n", failures, (int)G_N_ELEMENTS (samples)); return EXIT_FAILURE; } else { g_print ("All %d samples passed.\n\n", (int)G_N_ELEMENTS (samples)); return EXIT_SUCCESS; } }
static VALUE rb_gimp_rgb_parse_css (VALUE self, VALUE css) { GimpRGB color; gboolean success; success = gimp_rgb_parse_css(&color, (gchar *)StringValuePtr(css), (gint)-1); if(!success) return Qnil; return GimpRGB2rb(&color); }
static void svg_parse_gradient_stop_style_prop (SvgStop *stop, const gchar *name, const gchar *value) { if (strcmp (name, "stop-color") == 0) { gimp_rgb_parse_css (&stop->color, value, -1); } else if (strcmp (name, "stop-opacity") == 0) { gdouble opacity = g_ascii_strtod (value, NULL); if (errno != ERANGE) gimp_rgb_set_alpha (&stop->color, CLAMP (opacity, 0.0, 1.0)); } }
GList * gimp_palette_load_css (const gchar *filename, GError **error) { GimpPalette *palette; gchar *name; FILE *file; GRegex *regex; GimpRGB color; g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (g_path_is_absolute (filename), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); regex = g_regex_new (".*color.*:(?P<param>.*);", G_REGEX_CASELESS, 0, error); if (! regex) return NULL; file = g_fopen (filename, "rb"); if (! file) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN, _("Could not open '%s' for reading: %s"), gimp_filename_to_utf8 (filename), g_strerror (errno)); return NULL; } name = g_filename_display_basename (filename); palette = GIMP_PALETTE (gimp_palette_new (name)); g_free (name); do { GMatchInfo *matches; gchar buf[1024]; if (fgets (buf, sizeof (buf), file) != NULL) { if (g_regex_match (regex, buf, 0, &matches)) { gchar *word = g_match_info_fetch_named (matches, "param"); if (gimp_rgb_parse_css (&color, word, -1)) { if (! gimp_palette_find_entry (palette, &color, NULL)) { gimp_palette_add_entry (palette, -1, NULL, &color); } } g_free (word); } } } while (! feof (file)); fclose (file); g_regex_unref (regex); return g_list_prepend (NULL, palette); }
GList * gimp_palette_load_css (GimpContext *context, GFile *file, GInputStream *input, GError **error) { GimpPalette *palette; GDataInputStream *data_input; gchar *name; GRegex *regex; gchar *buf; g_return_val_if_fail (G_IS_FILE (file), NULL); g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); regex = g_regex_new (".*color.*:(?P<param>.*);", G_REGEX_CASELESS, 0, error); if (! regex) return NULL; name = g_path_get_basename (gimp_file_get_utf8_name (file)); palette = GIMP_PALETTE (gimp_palette_new (context, name)); g_free (name); data_input = g_data_input_stream_new (input); do { gsize buf_len = 1024; buf = g_data_input_stream_read_line (data_input, &buf_len, NULL, NULL); if (buf) { GMatchInfo *matches; if (g_regex_match (regex, buf, 0, &matches)) { GimpRGB color; gchar *word = g_match_info_fetch_named (matches, "param"); if (gimp_rgb_parse_css (&color, word, -1)) { if (! gimp_palette_find_entry (palette, &color, NULL)) { gimp_palette_add_entry (palette, -1, NULL, &color); } } g_free (word); } g_match_info_free (matches); g_free (buf); } } while (buf); g_regex_unref (regex); g_object_unref (data_input); return g_list_prepend (NULL, palette); }