1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 2002 Michael Natterer <mitch@gimp.org>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpcolor/gimpcolor.h"
29
#include "gui-types.h"
31
#include "config/gimpconfig-utils.h"
32
#include "config/gimpconfigwriter.h"
33
#include "config/gimpscanner.h"
35
#include "color-history.h"
44
static void color_history_init (void);
45
static void color_history_add_from_rc (GimpRGB *color);
48
static GimpRGB color_history[COLOR_HISTORY_SIZE];
49
static gboolean color_history_initialized = FALSE;
53
color_history_save (void)
55
GimpConfigWriter *writer;
59
filename = gimp_personal_rc_file ("colorrc");
60
writer = gimp_config_writer_new_file (filename,
63
"This file holds a list of "
64
"recently used colors.",
71
if (! color_history_initialized)
72
color_history_init ();
74
gimp_config_writer_open (writer, "color-history");
76
for (i = 0; i < COLOR_HISTORY_SIZE; i++)
78
gchar buf[4][G_ASCII_DTOSTR_BUF_SIZE];
80
g_ascii_formatd (buf[0],
81
G_ASCII_DTOSTR_BUF_SIZE, "%f", color_history[i].r);
82
g_ascii_formatd (buf[1],
83
G_ASCII_DTOSTR_BUF_SIZE, "%f", color_history[i].g);
84
g_ascii_formatd (buf[2],
85
G_ASCII_DTOSTR_BUF_SIZE, "%f", color_history[i].b);
86
g_ascii_formatd (buf[3],
87
G_ASCII_DTOSTR_BUF_SIZE, "%f", color_history[i].a);
89
gimp_config_writer_open (writer, "color-rgba");
90
gimp_config_writer_printf (writer, "%s %s %s %s",
91
buf[0], buf[1], buf[2], buf[3]);
92
gimp_config_writer_close (writer);
95
gimp_config_writer_close (writer);
97
gimp_config_writer_finish (writer, "end of colorrc", NULL);
101
color_history_restore (void)
107
filename = gimp_personal_rc_file ("colorrc");
108
scanner = gimp_scanner_new_file (filename, NULL);
114
g_scanner_scope_add_symbol (scanner, 0, "color-history",
115
GINT_TO_POINTER (COLOR_HISTORY));
117
token = G_TOKEN_LEFT_PAREN;
119
while (g_scanner_peek_next_token (scanner) == token)
121
token = g_scanner_get_next_token (scanner);
125
case G_TOKEN_LEFT_PAREN:
126
token = G_TOKEN_SYMBOL;
130
if (scanner->value.v_symbol == GINT_TO_POINTER (COLOR_HISTORY))
132
while (g_scanner_peek_next_token (scanner) == G_TOKEN_LEFT_PAREN)
136
if (! gimp_scanner_parse_color (scanner, &color))
139
color_history_add_from_rc (&color);
142
token = G_TOKEN_RIGHT_PAREN;
145
case G_TOKEN_RIGHT_PAREN:
146
token = G_TOKEN_LEFT_PAREN;
149
default: /* do nothing */
155
gimp_scanner_destroy (scanner);
159
color_history_set (gint index,
162
g_return_if_fail (index >= 0);
163
g_return_if_fail (index < COLOR_HISTORY_SIZE);
164
g_return_if_fail (rgb != NULL);
166
if (! color_history_initialized)
167
color_history_init ();
169
color_history[index] = *rgb;
173
color_history_get (gint index,
176
g_return_if_fail (index >= 0);
177
g_return_if_fail (index < COLOR_HISTORY_SIZE);
178
g_return_if_fail (rgb != NULL);
180
if (! color_history_initialized)
181
color_history_init ();
183
*rgb = color_history[index];
187
color_history_add (const GimpRGB *rgb)
189
gint shift_begin = -1;
192
g_return_val_if_fail (rgb != NULL, 0);
194
if (! color_history_initialized)
195
color_history_init ();
197
/* is the added color already there? */
198
for (i = 0; i < COLOR_HISTORY_SIZE; i++)
200
if (gimp_rgba_distance (&color_history[i], rgb) < 0.0001)
208
/* if not, are there two equal colors? */
209
if (shift_begin == -1)
211
for (i = 0; i < COLOR_HISTORY_SIZE; i++)
213
for (j = i + 1; j < COLOR_HISTORY_SIZE; j++)
215
if (gimp_rgba_distance (&color_history[i],
216
&color_history[j]) < 0.0001)
226
/* if not, shift them all */
227
if (shift_begin == -1)
228
shift_begin = COLOR_HISTORY_SIZE - 1;
232
for (i = shift_begin; i > 0; i--)
233
color_history[i] = color_history[i - 1];
235
color_history[0] = *rgb;
241
/* private functions */
244
color_history_init (void)
248
for (i = 0; i < COLOR_HISTORY_SIZE; i++)
249
gimp_rgba_set (&color_history[i], 1.0, 1.0, 1.0, GIMP_OPACITY_OPAQUE);
251
color_history_initialized = TRUE;
255
color_history_add_from_rc (GimpRGB *color)
257
static gint index = 0;
259
if (! color_history_initialized)
260
color_history_init ();
262
if (color && index < COLOR_HISTORY_SIZE)
264
color_history[index++] = *color;