Commit 98ae7326 authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer

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.
parent 14ddf98d
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
......
......@@ -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 */
......
......@@ -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__ */
......@@ -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)
{
gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (select->view),
entry);
break;
}
}
if (entry)
gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (select->view),
entry);
}
}
}
......
......@@ -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)
return -1;
if (editor->color)
index = editor->color->position;
if (search)
if (palette && palette->n_colors > 0)
{
if (! editor->color)
{
GList *list;
/* search from the start */
GimpPaletteEntry *entry;
for (list = palette->colors; list; list = g_list_next (list))
{
GimpPaletteEntry *entry = list->data;
entry = gimp_palette_find_entry (palette, search, editor->color);
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;
}
}
}
if (entry)
return entry->position;
}
return index;
return -1;
}
gboolean
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment