~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/gui/color-history.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * color-history.c
 
5
 * Copyright (C) 2002 Michael Natterer <mitch@gimp.org>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <gtk/gtk.h>
 
25
 
 
26
#include "libgimpbase/gimpbase.h"
 
27
#include "libgimpcolor/gimpcolor.h"
 
28
 
 
29
#include "gui-types.h"
 
30
 
 
31
#include "config/gimpconfig-utils.h"
 
32
#include "config/gimpconfigwriter.h"
 
33
#include "config/gimpscanner.h"
 
34
 
 
35
#include "color-history.h"
 
36
 
 
37
 
 
38
enum
 
39
{
 
40
  COLOR_HISTORY = 1
 
41
};
 
42
 
 
43
 
 
44
static void   color_history_init        (void);
 
45
static void   color_history_add_from_rc (GimpRGB *color);
 
46
 
 
47
 
 
48
static GimpRGB   color_history[COLOR_HISTORY_SIZE];
 
49
static gboolean  color_history_initialized = FALSE;
 
50
 
 
51
 
 
52
void
 
53
color_history_save (void)
 
54
{
 
55
  GimpConfigWriter *writer;
 
56
  gchar            *filename;
 
57
  gint              i;
 
58
 
 
59
  filename = gimp_personal_rc_file ("colorrc");
 
60
  writer = gimp_config_writer_new_file (filename,
 
61
                                        TRUE,
 
62
                                        "GIMP colorrc\n\n"
 
63
                                        "This file holds a list of "
 
64
                                        "recently used colors.",
 
65
                                        NULL);
 
66
  g_free (filename);
 
67
 
 
68
  if (!writer)
 
69
    return;
 
70
 
 
71
  if (! color_history_initialized)
 
72
    color_history_init ();
 
73
 
 
74
  gimp_config_writer_open (writer, "color-history");
 
75
 
 
76
  for (i = 0; i < COLOR_HISTORY_SIZE; i++)
 
77
    {
 
78
      gchar buf[4][G_ASCII_DTOSTR_BUF_SIZE];
 
79
 
 
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);
 
88
 
 
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);
 
93
    }
 
94
 
 
95
  gimp_config_writer_close (writer);
 
96
 
 
97
  gimp_config_writer_finish (writer, "end of colorrc", NULL);
 
98
}
 
99
 
 
100
void
 
101
color_history_restore (void)
 
102
{
 
103
  gchar      *filename;
 
104
  GScanner   *scanner;
 
105
  GTokenType  token;
 
106
 
 
107
  filename = gimp_personal_rc_file ("colorrc");
 
108
  scanner = gimp_scanner_new_file (filename, NULL);
 
109
  g_free (filename);
 
110
 
 
111
  if (! scanner)
 
112
    return;
 
113
 
 
114
  g_scanner_scope_add_symbol (scanner, 0, "color-history",
 
115
                              GINT_TO_POINTER (COLOR_HISTORY));
 
116
 
 
117
  token = G_TOKEN_LEFT_PAREN;
 
118
 
 
119
  while (g_scanner_peek_next_token (scanner) == token)
 
120
    {
 
121
      token = g_scanner_get_next_token (scanner);
 
122
 
 
123
      switch (token)
 
124
        {
 
125
        case G_TOKEN_LEFT_PAREN:
 
126
          token = G_TOKEN_SYMBOL;
 
127
          break;
 
128
 
 
129
        case G_TOKEN_SYMBOL:
 
130
          if (scanner->value.v_symbol == GINT_TO_POINTER (COLOR_HISTORY))
 
131
            {
 
132
              while (g_scanner_peek_next_token (scanner) == G_TOKEN_LEFT_PAREN)
 
133
                {
 
134
                  GimpRGB color;
 
135
 
 
136
                  if (! gimp_scanner_parse_color (scanner, &color))
 
137
                    goto error;
 
138
 
 
139
                  color_history_add_from_rc (&color);
 
140
                }
 
141
            }
 
142
          token = G_TOKEN_RIGHT_PAREN;
 
143
          break;
 
144
 
 
145
        case G_TOKEN_RIGHT_PAREN:
 
146
          token = G_TOKEN_LEFT_PAREN;
 
147
          break;
 
148
 
 
149
        default: /* do nothing */
 
150
          break;
 
151
        }
 
152
    }
 
153
 
 
154
 error:
 
155
  gimp_scanner_destroy (scanner);
 
156
}
 
157
 
 
158
void
 
159
color_history_set (gint           index,
 
160
                   const GimpRGB *rgb)
 
161
{
 
162
  g_return_if_fail (index >= 0);
 
163
  g_return_if_fail (index < COLOR_HISTORY_SIZE);
 
164
  g_return_if_fail (rgb != NULL);
 
165
 
 
166
  if (! color_history_initialized)
 
167
    color_history_init ();
 
168
 
 
169
  color_history[index] = *rgb;
 
170
}
 
171
 
 
172
void
 
173
color_history_get (gint     index,
 
174
                   GimpRGB *rgb)
 
175
{
 
176
  g_return_if_fail (index >= 0);
 
177
  g_return_if_fail (index < COLOR_HISTORY_SIZE);
 
178
  g_return_if_fail (rgb != NULL);
 
179
 
 
180
  if (! color_history_initialized)
 
181
    color_history_init ();
 
182
 
 
183
  *rgb = color_history[index];
 
184
}
 
185
 
 
186
gint
 
187
color_history_add (const GimpRGB *rgb)
 
188
{
 
189
  gint shift_begin = -1;
 
190
  gint i, j;
 
191
 
 
192
  g_return_val_if_fail (rgb != NULL, 0);
 
193
 
 
194
  if (! color_history_initialized)
 
195
    color_history_init ();
 
196
 
 
197
  /*  is the added color already there?  */
 
198
  for (i = 0; i < COLOR_HISTORY_SIZE; i++)
 
199
    {
 
200
      if (gimp_rgba_distance (&color_history[i], rgb) < 0.0001)
 
201
        {
 
202
          shift_begin = i;
 
203
 
 
204
          goto doit;
 
205
        }
 
206
    }
 
207
 
 
208
  /*  if not, are there two equal colors?  */
 
209
  if (shift_begin == -1)
 
210
    {
 
211
      for (i = 0; i < COLOR_HISTORY_SIZE; i++)
 
212
        {
 
213
          for (j = i + 1; j < COLOR_HISTORY_SIZE; j++)
 
214
            {
 
215
              if (gimp_rgba_distance (&color_history[i],
 
216
                                      &color_history[j]) < 0.0001)
 
217
                {
 
218
                  shift_begin = i;
 
219
 
 
220
                  goto doit;
 
221
                }
 
222
            }
 
223
        }
 
224
    }
 
225
 
 
226
  /*  if not, shift them all  */
 
227
  if (shift_begin == -1)
 
228
    shift_begin = COLOR_HISTORY_SIZE - 1;
 
229
 
 
230
 doit:
 
231
 
 
232
  for (i = shift_begin; i > 0; i--)
 
233
    color_history[i] = color_history[i - 1];
 
234
 
 
235
  color_history[0] = *rgb;
 
236
 
 
237
  return shift_begin;
 
238
}
 
239
 
 
240
 
 
241
/*  private functions  */
 
242
 
 
243
static void
 
244
color_history_init (void)
 
245
{
 
246
  gint i;
 
247
 
 
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);
 
250
 
 
251
  color_history_initialized = TRUE;
 
252
}
 
253
 
 
254
static void
 
255
color_history_add_from_rc (GimpRGB *color)
 
256
{
 
257
  static gint index = 0;
 
258
 
 
259
  if (! color_history_initialized)
 
260
    color_history_init ();
 
261
 
 
262
  if (color && index < COLOR_HISTORY_SIZE)
 
263
    {
 
264
      color_history[index++] = *color;
 
265
    }
 
266
}