Applied slightly modified patch from David Gowers which abstracts away and

2006-12-14  Michael Natterer  <mitch@gimp.org>

	Applied slightly modified patch from David Gowers which abstracts
	away and unifies seraching a color in a palette (bug #132146):

	* app/core/gimppalette.[ch]: added gimp_palette_find_entry().

	* app/widgets/gimpcolorselectorpalette.c
	* app/widgets/gimppaletteeditor.c: use it for selecting matching
	colors from the active palette.
This commit is contained in:
Michael Natterer 2006-12-14 12:02:05 +00:00 committed by Michael Natterer
parent 14ddf98d14
commit 98ae7326d7
5 changed files with 96 additions and 77 deletions

View File

@ -1,3 +1,14 @@
2006-12-14 Michael Natterer <mitch@gimp.org>
Applied slightly modified patch from David Gowers which abstracts
away and unifies seraching a color in a palette (bug #132146):
* app/core/gimppalette.[ch]: added gimp_palette_find_entry().
* app/widgets/gimpcolorselectorpalette.c
* app/widgets/gimppaletteeditor.c: use it for selecting matching
colors from the active palette.
2006-12-13 Kevin Cozens <kcozens@cvs.gnome.org>
* plug-ins/script-fu/tinyscheme/scheme-private.h

View File

@ -35,6 +35,7 @@
#include "gimp-intl.h"
#define EPSILON 1e-10
/* local function prototypes */
@ -420,6 +421,72 @@ gimp_palette_get_columns (GimpPalette *palette)
return palette->n_columns;
}
GimpPaletteEntry *
gimp_palette_find_entry (GimpPalette *palette,
const GimpRGB *color,
GimpPaletteEntry *start_from)
{
GimpPaletteEntry *entry;
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)
{
GList *list;
/* search from the start */
for (list = palette->colors; list; list = g_list_next (list))
{
entry = (GimpPaletteEntry *) list->data;
if (gimp_rgb_distance (&entry->color, color) < EPSILON)
return entry;
}
}
else if (gimp_rgb_distance (&start_from->color, color) < EPSILON)
{
return start_from;
}
else
{
GList *old = g_list_find (palette->colors, start_from);
GList *next;
GList *prev;
g_return_val_if_fail (old != NULL, NULL);
next = old->next;
prev = old->prev;
/* proximity-based search */
while (next || prev)
{
if (next)
{
entry = (GimpPaletteEntry *) next->data;
if (gimp_rgb_distance (&entry->color, color) < EPSILON)
return entry;
next = next->next;
}
if (prev)
{
entry = (GimpPaletteEntry *) prev->data;
if (gimp_rgb_distance (&entry->color, color) < EPSILON)
return entry;
prev = prev->prev;
}
}
}
return NULL;
}
/* private functions */

View File

@ -75,5 +75,9 @@ void gimp_palette_set_columns (GimpPalette *palette,
gint columns);
gint gimp_palette_get_columns (GimpPalette *palette);
GimpPaletteEntry * gimp_palette_find_entry (GimpPalette *palette,
const GimpRGB *color,
GimpPaletteEntry *start_from);
#endif /* __GIMP_PALETTE_H__ */

View File

@ -77,23 +77,16 @@ gimp_color_selector_palette_set_color (GimpColorSelector *selector,
{
GimpPalette *palette = gimp_context_get_palette (select->context);
if (palette)
if (palette && palette->n_colors > 0)
{
GList *list;
GimpPaletteEntry *entry;
for (list = palette->colors; list; list = g_list_next (list))
{
GimpPaletteEntry *entry = list->data;
entry = gimp_palette_find_entry (palette, rgb,
GIMP_PALETTE_VIEW (select->view)->selected);
#define EPSILON 1e-10
if (gimp_rgb_distance (&entry->color, rgb) < EPSILON)
{
if (entry)
gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (select->view),
entry);
break;
}
}
}
}
}

View File

@ -56,8 +56,6 @@
#define PREVIEW_WIDTH ((ENTRY_WIDTH + SPACING) * COLUMNS + 1)
#define PREVIEW_HEIGHT ((ENTRY_HEIGHT + SPACING) * ROWS + 1)
#define EPSILON 1e-10
/* local function prototypes */
static void gimp_palette_editor_docked_iface_init (GimpDockedInterface *face);
@ -568,77 +566,23 @@ gimp_palette_editor_get_index (GimpPaletteEditor *editor,
const GimpRGB *search)
{
GimpPalette *palette;
gint index = 0;
g_return_val_if_fail (GIMP_IS_PALETTE_EDITOR (editor), -1);
g_return_val_if_fail (search != NULL, -1);
palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
if (! palette || palette->n_colors == 0)
if (palette && palette->n_colors > 0)
{
GimpPaletteEntry *entry;
entry = gimp_palette_find_entry (palette, search, editor->color);
if (entry)
return entry->position;
}
return -1;
if (editor->color)
index = editor->color->position;
if (search)
{
if (! editor->color)
{
GList *list;
/* search from the start */
for (list = palette->colors; list; list = g_list_next (list))
{
GimpPaletteEntry *entry = list->data;
if (gimp_rgb_distance (&entry->color, search) < EPSILON)
{
index = entry->position;
break;
}
}
}
else if (gimp_rgb_distance (&editor->color->color, search) > EPSILON)
{
GList *old = g_list_nth (palette->colors, editor->color->position);
GList *next = old->next;
GList *prev = old->prev;
/* proximity-based search */
while (next || prev)
{
if (next)
{
GimpPaletteEntry *entry = next->data;
if (gimp_rgb_distance (&entry->color, search) < EPSILON)
{
index = entry->position;
break;
}
next = next->next;
}
if (prev)
{
GimpPaletteEntry *entry = prev->data;
if (gimp_rgb_distance (&entry->color, search) < EPSILON)
{
index = entry->position;
break;
}
prev = prev->prev;
}
}
}
}
return index;
}
gboolean