1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimppalettemru.c
* Copyright (C) 2014 Michael Natterer <mitch@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"
#include "core-types.h"
#include "gimppalettemru.h"
#include "gimp-intl.h"
#define MAX_N_COLORS 256
#define RGBA_EPSILON 1e-4
enum
{
COLOR_HISTORY = 1,
COLOR = 2
};
G_DEFINE_TYPE (GimpPaletteMru, gimp_palette_mru, GIMP_TYPE_PALETTE)
#define parent_class gimp_palette_mru_parent_class
static void
gimp_palette_mru_class_init (GimpPaletteMruClass *klass)
{
}
static void
gimp_palette_mru_init (GimpPaletteMru *palette)
{
}
/* public functions */
GimpData *
gimp_palette_mru_new (const gchar *name)
{
GimpPaletteMru *palette;
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (*name != '\0', NULL);
palette = g_object_new (GIMP_TYPE_PALETTE_MRU,
"name", name,
"mime-type", "application/x-gimp-palette",
NULL);
return GIMP_DATA (palette);
}
void
gimp_palette_mru_load (GimpPaletteMru *mru,
GFile *file)
{
GimpPalette *palette;
GScanner *scanner;
GTokenType token;
g_return_if_fail (GIMP_IS_PALETTE_MRU (mru));
g_return_if_fail (G_IS_FILE (file));
palette = GIMP_PALETTE (mru);
scanner = gimp_scanner_new_file (file, NULL);
if (! scanner)
return;
g_scanner_scope_add_symbol (scanner, 0, "color-history",
GINT_TO_POINTER (COLOR_HISTORY));
g_scanner_scope_add_symbol (scanner, 0, "color",
GINT_TO_POINTER (COLOR_HISTORY));
token = G_TOKEN_LEFT_PAREN;
while (g_scanner_peek_next_token (scanner) == token)
{
token = g_scanner_get_next_token (scanner);
switch (token)
{
case G_TOKEN_LEFT_PAREN:
token = G_TOKEN_SYMBOL;
break;
case G_TOKEN_SYMBOL:
if (scanner->value.v_symbol == GINT_TO_POINTER (COLOR_HISTORY))
{
while (g_scanner_peek_next_token (scanner) == G_TOKEN_LEFT_PAREN)
{
GeglColor *color = NULL;
if (! gimp_scanner_parse_color (scanner, &color))
goto end;
gimp_palette_add_entry (palette, -1, _("History Color"), color);
g_object_unref (color);
if (gimp_palette_get_n_colors (palette) == MAX_N_COLORS)
goto end;
}
}
token = G_TOKEN_RIGHT_PAREN;
break;
case G_TOKEN_RIGHT_PAREN:
token = G_TOKEN_LEFT_PAREN;
break;
default: /* do nothing */
break;
}
}
end:
gimp_scanner_unref (scanner);
}
void
gimp_palette_mru_save (GimpPaletteMru *mru,
GFile *file)
{
GimpPalette *palette;
GimpConfigWriter *writer;
GList *list;
g_return_if_fail (GIMP_IS_PALETTE_MRU (mru));
g_return_if_fail (G_IS_FILE (file));
writer = gimp_config_writer_new_from_file (file,
TRUE,
"GIMP colorrc\n\n"
"This file holds a list of "
"recently used colors.",
NULL);
if (! writer)
return;
palette = GIMP_PALETTE (mru);
gimp_config_writer_open (writer, "color-history");
for (list = palette->colors; list; list = g_list_next (list))
{
GimpPaletteEntry *entry = list->data;
GeglColor *color = gegl_color_duplicate (entry->color);
const gchar *encoding;
const Babl *format = gegl_color_get_format (color);
const Babl *space;
GBytes *bytes;
gconstpointer data;
gsize data_length;
guint8 *profile_data;
int profile_length = 0;
gimp_config_writer_open (writer, "color");
if (babl_format_is_palette (format))
{
guint8 pixel[40];
/* As a special case, we don't want to serialize
* palette colors, because they are just too much
* dependent on external data and cannot be
* deserialized back safely. So we convert them first.
*/
format = babl_format_with_space ("R'G'B'A u8", format);
gegl_color_get_pixel (color, format, pixel);
gegl_color_set_pixel (color, format, pixel);
}
encoding = babl_format_get_encoding (format);
gimp_config_writer_string (writer, encoding);
bytes = gegl_color_get_bytes (color, format);
data = g_bytes_get_data (bytes, &data_length);
gimp_config_writer_printf (writer, "%" G_GSIZE_FORMAT, data_length);
gimp_config_writer_data (writer, data_length, data);
space = babl_format_get_space (format);
if (space != babl_space ("sRGB"))
{
profile_data = (guint8 *) babl_space_get_icc (space, &profile_length);
gimp_config_writer_printf (writer, "%u", profile_length);
if (profile_data)
gimp_config_writer_data (writer, profile_length, profile_data);
}
else
{
gimp_config_writer_printf (writer, "%u", profile_length);
}
gimp_config_writer_close (writer);
g_bytes_unref (bytes);
g_object_unref (color);
}
gimp_config_writer_close (writer);
gimp_config_writer_finish (writer, "end of colorrc", NULL);
}
void
gimp_palette_mru_add (GimpPaletteMru *mru,
GeglColor *color)
{
GimpPalette *palette;
GList *list;
g_return_if_fail (GIMP_IS_PALETTE_MRU (mru));
g_return_if_fail (GEGL_IS_COLOR (color));
palette = GIMP_PALETTE (mru);
/* is the added color already there? */
for (list = gimp_palette_get_colors (palette);
list;
list = g_list_next (list))
{
GimpPaletteEntry *entry = list->data;
if (gimp_color_is_perceptually_identical (entry->color, color))
{
gimp_palette_move_entry (palette, entry, 0);
/* Even though they are nearly the same color, let's make them
* exactly equal.
*/
gimp_palette_set_entry_color (palette, 0, color, FALSE);
return;
}
}
if (gimp_palette_get_n_colors (palette) == MAX_N_COLORS)
{
gimp_palette_delete_entry (palette,
gimp_palette_get_entry (palette,
MAX_N_COLORS - 1));
}
gimp_palette_add_entry (palette, 0, _("History Color"), color);
}
|