diff --git a/ChangeLog b/ChangeLog index c3b7f1b9d5..4c8e117b1b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2007-12-18 Sven Neumann + + * app/core/gimppalette-import.c + * app/core/gimppalette-load.[ch]: applied slightly modified patch + from Nicola Archibald that adds import of colors from CSS files + (bug #464480). + + * app/core/gimppalette.c (gimp_palette_find_entry): allow to call + this function on an empty palette. + 2007-12-18 Sven Neumann * app/base/tile-pyramid.c: use the coordinate parameters in diff --git a/app/core/gimppalette-import.c b/app/core/gimppalette-import.c index 4d88539188..6d03bcedce 100644 --- a/app/core/gimppalette-import.c +++ b/app/core/gimppalette-import.c @@ -515,6 +515,10 @@ gimp_palette_import_from_file (const gchar *filename, palette_list = gimp_palette_load_aco (filename, error); break; + case GIMP_PALETTE_FILE_FORMAT_CSS: + palette_list = gimp_palette_load_css (filename, error); + break; + default: g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, diff --git a/app/core/gimppalette-load.c b/app/core/gimppalette-load.c index 0af169f7ac..b6e7b8cca7 100644 --- a/app/core/gimppalette-load.c +++ b/app/core/gimppalette-load.c @@ -574,6 +574,69 @@ gimp_palette_load_aco (const gchar *filename, return g_list_prepend (NULL, palette); } + +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.*);", 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); +} + GimpPaletteFileFormat gimp_palette_load_detect_format (const gchar *filename) { @@ -606,7 +669,13 @@ gimp_palette_load_detect_format (const gchar *filename) gchar *lower_filename = g_ascii_strdown (filename, -1); if (g_str_has_suffix (lower_filename, ".aco")) - format = GIMP_PALETTE_FILE_FORMAT_ACO; + { + format = GIMP_PALETTE_FILE_FORMAT_ACO; + } + else if (g_str_has_suffix (lower_filename, ".css")) + { + format = GIMP_PALETTE_FILE_FORMAT_CSS; + } g_free (lower_filename); } diff --git a/app/core/gimppalette-load.h b/app/core/gimppalette-load.h index 9d79f76364..ccb1cefd24 100644 --- a/app/core/gimppalette-load.h +++ b/app/core/gimppalette-load.h @@ -30,7 +30,8 @@ typedef enum GIMP_PALETTE_FILE_FORMAT_RIFF_PAL, /* RIFF palette */ GIMP_PALETTE_FILE_FORMAT_ACT, /* Photoshop binary color palette */ GIMP_PALETTE_FILE_FORMAT_PSP_PAL, /* JASC's Paint Shop Pro color palette */ - GIMP_PALETTE_FILE_FORMAT_ACO /* Photoshop ACO color file */ + GIMP_PALETTE_FILE_FORMAT_ACO, /* Photoshop ACO color file */ + GIMP_PALETTE_FILE_FORMAT_CSS /* Cascaded Stylesheet file (CSS) */ } GimpPaletteFileFormat; @@ -44,6 +45,8 @@ GList * gimp_palette_load_psp (const gchar *filename, GError **error); GList * gimp_palette_load_aco (const gchar *filename, GError **error); +GList * gimp_palette_load_css (const gchar *filename, + GError **error); GimpPaletteFileFormat gimp_palette_load_detect_format (const gchar *filename); diff --git a/app/core/gimppalette.c b/app/core/gimppalette.c index f2b364599c..23beb3ff87 100644 --- a/app/core/gimppalette.c +++ b/app/core/gimppalette.c @@ -427,7 +427,6 @@ gimp_palette_find_entry (GimpPalette *palette, g_return_val_if_fail (GIMP_IS_PALETTE (palette), NULL); g_return_val_if_fail (color != NULL, NULL); - g_return_val_if_fail (palette->n_colors > 0, NULL); if (! start_from) {