Index: app/actions/context-actions.c
===================================================================
RCS file: /cvs/gnome/gimp/app/actions/context-actions.c,v
retrieving revision 1.10
diff -u -p -r1.10 context-actions.c
--- app/actions/context-actions.c	11 Aug 2004 23:14:02 -0000	1.10
+++ app/actions/context-actions.c	19 Aug 2004 10:13:32 -0000
@@ -67,7 +67,28 @@ static GimpActionEntry context_actions[]
   { "context-colors-swap", GIMP_STOCK_SWAP_COLORS,
     N_("S_wap Colors"), "X", NULL,
     G_CALLBACK (context_colors_swap_cmd_callback),
+    GIMP_HELP_TOOLBOX_SWAP_COLORS },
+
+  { "context-next-palette-fgcolor", GIMP_STOCK_SWAP_COLORS,
+    N_("Next Palette FGColor"), "", NULL,
+    G_CALLBACK (context_colors_next_fgcolor_cmd_callback),
+    GIMP_HELP_TOOLBOX_SWAP_COLORS },
+
+  { "context-prev-palette-fgcolor", GIMP_STOCK_SWAP_COLORS,
+    N_("Previous Palette FGColor"), "", NULL,
+    G_CALLBACK (context_colors_prev_fgcolor_cmd_callback),
+    GIMP_HELP_TOOLBOX_SWAP_COLORS },
+
+  { "context-map-foreground-to-palette", GIMP_STOCK_SWAP_COLORS,
+    N_("Map Foreground Color to Palette"), "", NULL,
+    G_CALLBACK (context_colors_palettize_fgcolor_cmd_callback),
+    GIMP_HELP_TOOLBOX_SWAP_COLORS },
+
+  { "context-map-foreground-to-palette", GIMP_STOCK_SWAP_COLORS,
+    N_("Map Foreground Color to Palette"), "", NULL,
+    G_CALLBACK (context_colors_palettize_bgcolor_cmd_callback),
     GIMP_HELP_TOOLBOX_SWAP_COLORS }
+
 };
 
 static GimpEnumActionEntry context_foreground_red_actions[] =
Index: app/actions/context-commands.c
===================================================================
RCS file: /cvs/gnome/gimp/app/actions/context-commands.c,v
retrieving revision 1.6
diff -u -p -r1.6 context-commands.c
--- app/actions/context-commands.c	5 Aug 2004 10:29:19 -0000	1.6
+++ app/actions/context-commands.c	19 Aug 2004 10:13:35 -0000
@@ -31,6 +31,7 @@
 #include "core/gimpcontext.h"
 #include "core/gimpdatafactory.h"
 #include "core/gimplist.h"
+#include "core/gimppalette.h"
 
 #include "actions.h"
 #include "context-commands.h"
@@ -233,6 +234,76 @@ context_palette_select_cmd_callback (Gtk
 }
 
 void
+context_colors_next_fgcolor_cmd_callback (GtkAction *action,
+                                         gpointer   data)
+{
+  GimpContext     *context; 
+  GimpPalette    *palette;
+  GList           *color;
+  gint             index;
+
+  return_if_no_context(context,data);
+  palette = context->palette;
+  if (!palette)
+	return;
+
+  color = palette->colors;
+
+  index = gimp_palette_validate_index(palette, context->foreground_index);
+
+  if (g_list_nth(color,index)->next)
+        index++;
+
+  gimp_context_set_foreground(context,&(((GimpPaletteEntry *)(g_list_nth_data(color,index)))->color));
+  context->foreground_index = index;
+
+}
+
+void
+context_colors_prev_fgcolor_cmd_callback (GtkAction *action,
+                                         gpointer   data)
+{
+  GimpContext     *context; 
+  GimpPalette    *palette;
+  GList           *color;
+  gint             index;
+
+  return_if_no_context(context,data);
+  palette = context->palette;
+  if (!palette)
+	return;
+
+  color = palette->colors;
+
+  index = gimp_palette_validate_index(palette, context->foreground_index);
+
+  if (g_list_nth(color,index)->prev)
+    index--;
+
+  gimp_context_set_foreground(context,&(((GimpPaletteEntry *)(g_list_nth_data(color,index)))->color));
+  context->foreground_index = index;
+
+}
+
+void
+context_colors_palettize_fgcolor_cmd_callback (GtkAction *action,
+                                         gpointer   data)
+{
+  GimpContext     *context; 
+  return_if_no_context(context,data);
+  gimp_context_map_foreground_to_palette(context);
+}
+
+void
+context_colors_palettize_bgcolor_cmd_callback (GtkAction *action,
+                                         gpointer   data)
+{
+  GimpContext     *context; 
+  return_if_no_context(context,data);
+  gimp_context_map_background_to_palette(context);
+}
+
+void
 context_gradient_select_cmd_callback (GtkAction *action,
                                       gint       value,
                                       gpointer   data)
Index: app/actions/context-commands.h
===================================================================
RCS file: /cvs/gnome/gimp/app/actions/context-commands.h,v
retrieving revision 1.5
diff -u -p -r1.5 context-commands.h
--- app/actions/context-commands.h	5 Aug 2004 10:29:19 -0000	1.5
+++ app/actions/context-commands.h	19 Aug 2004 10:13:35 -0000
@@ -24,6 +24,16 @@ void   context_colors_default_cmd_callba
                                              gpointer     data);
 void   context_colors_swap_cmd_callback     (GtkAction   *action,
                                              gpointer     data);
+void   context_colors_next_fgcolor_cmd_callback(GtkAction *action,
+                                                 gpointer   data);
+void   context_colors_prev_fgcolor_cmd_callback(GtkAction *action,
+                                                 gpointer   data);
+void   context_colors_palettize_fgcolor_cmd_callback(GtkAction *action,
+                                                     gpointer   data);
+void   context_colors_palettize_bgcolor_cmd_callback(GtkAction *action,
+                                                     gpointer   data);
+
+
 
 void   context_foreground_red_cmd_callback  (GtkAction *action,
                                              gint       value,
}
Index: app/core/core-enums.h
===================================================================
RCS file: /cvs/gnome/gimp/app/core/core-enums.h,v
retrieving revision 1.84
diff -u -p -r1.84 core-enums.h
--- app/core/core-enums.h	1 Aug 2004 03:06:57 -0000	1.84
+++ app/core/core-enums.h	19 Aug 2004 10:14:05 -0000
@@ -525,17 +525,19 @@ typedef enum  /*< pdb-skip, skip >*/
   GIMP_CONTEXT_PROP_DISPLAY    =  3,
   GIMP_CONTEXT_PROP_TOOL       =  4,
   GIMP_CONTEXT_PROP_FOREGROUND =  5,
-  GIMP_CONTEXT_PROP_BACKGROUND =  6,
-  GIMP_CONTEXT_PROP_OPACITY    =  7,
-  GIMP_CONTEXT_PROP_PAINT_MODE =  8,
-  GIMP_CONTEXT_PROP_BRUSH      =  9,
-  GIMP_CONTEXT_PROP_PATTERN    = 10,
-  GIMP_CONTEXT_PROP_GRADIENT   = 11,
-  GIMP_CONTEXT_PROP_PALETTE    = 12,
-  GIMP_CONTEXT_PROP_FONT       = 13,
-  GIMP_CONTEXT_PROP_BUFFER     = 14,
-  GIMP_CONTEXT_PROP_IMAGEFILE  = 15,
-  GIMP_CONTEXT_PROP_TEMPLATE   = 16,
+  GIMP_CONTEXT_PROP_FOREGROUND_INDEX =  6,
+  GIMP_CONTEXT_PROP_BACKGROUND =  7,
+  GIMP_CONTEXT_PROP_BACKGROUND_INDEX =  8,
+  GIMP_CONTEXT_PROP_OPACITY    =  9,
+  GIMP_CONTEXT_PROP_PAINT_MODE = 10,
+  GIMP_CONTEXT_PROP_BRUSH      = 11,
+  GIMP_CONTEXT_PROP_PATTERN    = 12,
+  GIMP_CONTEXT_PROP_GRADIENT   = 13,
+  GIMP_CONTEXT_PROP_PALETTE    = 14,
+  GIMP_CONTEXT_PROP_FONT       = 15,
+  GIMP_CONTEXT_PROP_BUFFER     = 16,
+  GIMP_CONTEXT_PROP_IMAGEFILE  = 17,
+  GIMP_CONTEXT_PROP_TEMPLATE   = 18,
 
   GIMP_CONTEXT_LAST_PROP       = GIMP_CONTEXT_PROP_TEMPLATE
 } GimpContextPropType;
Index: app/core/gimpcontext.c
===================================================================
RCS file: /cvs/gnome/gimp/app/core/gimpcontext.c,v
retrieving revision 1.115
diff -u -p -r1.115 gimpcontext.c
--- app/core/gimpcontext.c	27 Jul 2004 16:38:58 -0000	1.115
+++ app/core/gimpcontext.c	19 Aug 2004 10:14:26 -0000
@@ -281,7 +281,9 @@ static gchar *gimp_context_prop_names[] 
   "display",
   "tool",
   "foreground",
+  "foreground-index",
   "background",
+  "background-index",
   "opacity",
   "paint-mode",
   "brush",
@@ -593,12 +595,29 @@ gimp_context_class_init (GimpContextClas
                                 &black,
                                 0);
 
+  GIMP_CONFIG_INSTALL_PROP_INT (object_class, GIMP_CONTEXT_PROP_FOREGROUND_INDEX,
+                                gimp_context_prop_names[GIMP_CONTEXT_PROP_FOREGROUND_INDEX],
+                                NULL,
+                                0,
+                                16777216,
+                                0,
+                                0);
+
+
   GIMP_CONFIG_INSTALL_PROP_RGB (object_class, GIMP_CONTEXT_PROP_BACKGROUND,
                                 gimp_context_prop_names[GIMP_CONTEXT_PROP_BACKGROUND],
                                 NULL,
                                 &white,
                                 0);
 
+  GIMP_CONFIG_INSTALL_PROP_INT (object_class, GIMP_CONTEXT_PROP_BACKGROUND_INDEX,
+                                gimp_context_prop_names[GIMP_CONTEXT_PROP_BACKGROUND_INDEX],
+                                NULL,
+                                0,
+                                16777216,
+                                0,
+                                0);
+
   GIMP_CONFIG_INSTALL_PROP_DOUBLE (object_class, GIMP_CONTEXT_PROP_OPACITY,
                                    gimp_context_prop_names[GIMP_CONTEXT_PROP_OPACITY],
                                    NULL,
@@ -661,6 +680,7 @@ gimp_context_class_init (GimpContextClas
                                                         NULL, NULL,
                                                         GIMP_TYPE_TEMPLATE,
                                                         G_PARAM_READWRITE));
+
 }
 
 static void
@@ -702,6 +722,7 @@ gimp_context_init (GimpContext *context)
 
   context->template        = NULL;
   context->template_name   = NULL;
+
 }
 
 static void
@@ -953,9 +974,15 @@ gimp_context_set_property (GObject      
     case GIMP_CONTEXT_PROP_FOREGROUND:
       gimp_context_set_foreground (context, g_value_get_boxed (value));
       break;
+    case GIMP_CONTEXT_PROP_FOREGROUND_INDEX:
+      context->foreground_index = g_value_get_int(value);
+      break;
     case GIMP_CONTEXT_PROP_BACKGROUND:
       gimp_context_set_background (context, g_value_get_boxed (value));
       break;
+    case GIMP_CONTEXT_PROP_BACKGROUND_INDEX:
+      context->background_index = g_value_get_int(value);
+      break;
     case GIMP_CONTEXT_PROP_OPACITY:
       gimp_context_set_opacity (context, g_value_get_double (value));
       break;
@@ -1022,6 +1049,9 @@ gimp_context_get_property (GObject    *o
         g_value_set_boxed (value, &color);
       }
       break;
+    case GIMP_CONTEXT_PROP_FOREGROUND_INDEX:
+      g_value_set_int(value,context->foreground_index);
+      break;
     case GIMP_CONTEXT_PROP_BACKGROUND:
       {
         GimpRGB color;
@@ -1030,6 +1060,9 @@ gimp_context_get_property (GObject    *o
         g_value_set_boxed (value, &color);
       }
       break;
+    case GIMP_CONTEXT_PROP_BACKGROUND_INDEX:
+      g_value_set_int(value,context->background_index);
+      break;
     case GIMP_CONTEXT_PROP_OPACITY:
       g_value_set_double (value, gimp_context_get_opacity (context));
       break;
@@ -1250,7 +1283,6 @@ gimp_context_deserialize_property (GimpC
   return TRUE;
 }
 
-
 /*****************************************************************************/
 /*  public functions  ********************************************************/
 
@@ -1472,10 +1504,18 @@ gimp_context_copy_property (GimpContext 
       gimp_context_real_set_foreground (dest, &src->foreground);
       break;
 
+    case GIMP_CONTEXT_PROP_FOREGROUND_INDEX:
+      dest->foreground_index = src->foreground_index;
+      break;
+
     case GIMP_CONTEXT_PROP_BACKGROUND:
       gimp_context_real_set_background (dest, &src->background);
       break;
 
+    case GIMP_CONTEXT_PROP_BACKGROUND_INDEX:
+      dest->background_index = src->background_index;
+      break;
+
     case GIMP_CONTEXT_PROP_OPACITY:
       gimp_context_real_set_opacity (dest, src->opacity);
       break;
@@ -1960,7 +2000,6 @@ gimp_context_real_set_foreground (GimpCo
   gimp_context_foreground_changed (context);
 }
 
-
 /*****************************************************************************/
 /*  background color  ********************************************************/
 
@@ -2010,7 +2049,6 @@ gimp_context_real_set_background (GimpCo
   gimp_context_background_changed (context);
 }
 
-
 /*****************************************************************************/
 /*  color utility functions  *************************************************/
 
@@ -2033,6 +2071,8 @@ gimp_context_set_default_colors (GimpCon
 
   gimp_context_real_set_foreground (context, &fg);
   gimp_context_real_set_background (bg_context, &bg);
+  context->foreground_index = 0;
+  context->background_index = 0;
 }
 
 void
@@ -2041,6 +2081,7 @@ gimp_context_swap_colors (GimpContext *c
   GimpContext *bg_context;
   GimpRGB      fg;
   GimpRGB      bg;
+  gint         temp;
 
   g_return_if_fail (GIMP_IS_CONTEXT (context));
 
@@ -2054,8 +2095,56 @@ gimp_context_swap_colors (GimpContext *c
 
   gimp_context_real_set_foreground (context, &bg);
   gimp_context_real_set_background (bg_context, &fg);
+  temp = context->foreground_index;
+  context->foreground_index = context->background_index; ;
+  context->background_index = temp;
 }
 
+void
+gimp_context_map_foreground_to_palette(GimpContext *context)
+{
+  g_return_if_fail (GIMP_IS_CONTEXT (context));
+
+  if (context->palette)
+  {
+    GimpRGB    color;
+
+    context->foreground_index = 
+        gimp_palette_find_color_index (context->palette, &(context->foreground));
+
+    color = ((GimpPaletteEntry *)(
+                g_list_nth_data (context->palette->colors, 
+                                 context->foreground_index)))->color;
+
+    gimp_context_real_set_foreground (context, &color);
+
+  }
+
+}
+
+void
+gimp_context_map_background_to_palette(GimpContext *context)
+{
+  g_return_if_fail (GIMP_IS_CONTEXT (context));
+
+  if (context->palette)
+  {
+    GimpRGB    color;
+
+    context->background_index = 
+        gimp_palette_find_color_index (context->palette, &(context->background));
+
+    color = ((GimpPaletteEntry *)(
+                g_list_nth_data (context->palette->colors, 
+                                 context->background_index)))->color;
+
+
+    gimp_context_real_set_background (context, &color);
+  }
+
+}
+
+
 /*****************************************************************************/
 /*  opacity  *****************************************************************/
 
Index: app/core/gimpcontext.h
===================================================================
RCS file: /cvs/gnome/gimp/app/core/gimpcontext.h,v
retrieving revision 1.47
diff -u -p -r1.47 gimpcontext.h
--- app/core/gimpcontext.h	3 Jul 2004 20:27:28 -0000	1.47
+++ app/core/gimpcontext.h	19 Aug 2004 10:14:28 -0000
@@ -55,6 +55,8 @@ struct _GimpContext
 
   GimpRGB               foreground;
   GimpRGB               background;
+  gint                  foreground_index;
+  gint                  background_index;
 
   gdouble               opacity;
   GimpLayerModeEffects  paint_mode;
@@ -213,6 +215,9 @@ void            gimp_context_get_foregro
                                                  GimpRGB         *color);
 void            gimp_context_set_foreground     (GimpContext     *context,
                                                  const GimpRGB   *color);
+void            gimp_context_set_foreground_index(GimpContext    *context,
+                                                  const gint      index);
+
 void            gimp_context_foreground_changed (GimpContext     *context);
 
 
@@ -221,12 +226,16 @@ void            gimp_context_get_backgro
                                                  GimpRGB         *color);
 void            gimp_context_set_background     (GimpContext     *context,
                                                  const GimpRGB   *color);
+void            gimp_context_set_background_index(GimpContext    *context,
+                                                  const gint      index);
 void            gimp_context_background_changed (GimpContext     *context);
 
 
 /*  color utility functions  */
 void            gimp_context_set_default_colors (GimpContext     *context);
 void            gimp_context_swap_colors        (GimpContext     *context);
+void            gimp_context_map_foreground_to_palette(GimpContext *context);
+void            gimp_context_map_background_to_palette(GimpContext *context);
 
 
 /*  opacity  */
Index: app/core/gimppalette.c
===================================================================
RCS file: /cvs/gnome/gimp/app/core/gimppalette.c,v
retrieving revision 1.49
diff -u -p -r1.49 gimppalette.c
--- app/core/gimppalette.c	26 Jul 2004 19:00:22 -0000	1.49
+++ app/core/gimppalette.c	19 Aug 2004 10:15:13 -0000
@@ -715,6 +715,43 @@ gimp_palette_get_n_columns  (GimpPalette
   return palette->n_columns;
 }
 
+gint gimp_palette_validate_index(GimpPalette *palette,
+                                 gint         index)
+{
+  GList      *list;  
+
+  g_return_val_if_fail(palette != NULL,-1);
+
+  list = palette->colors;
+  return CLAMP(index, 0, g_list_length(list) - 1);
+
+}
+
+gint gimp_palette_find_color_index(GimpPalette   *palette,
+                                   const GimpRGB *color)
+{
+  GList      *list;
+  GimpRGB    *rgb;
+  gint        i;  
+
+  g_return_val_if_fail(palette != NULL,-1);
+  list = palette->colors;
+  i = 0;
+
+  while (list)
+  {
+    rgb = &(((GimpPaletteEntry *)(list->data))->color);
+    if (color->r == rgb->r && color->g == rgb->g && color->b == rgb->b)
+	return i;
+	
+    list = list->next;
+    i++;
+  }
+  
+  return 0;
+}
+
+
 
 /*  private functions  */
 
Index: app/core/gimppalette.h
===================================================================
RCS file: /cvs/gnome/gimp/app/core/gimppalette.h,v
retrieving revision 1.19
diff -u -p -r1.19 gimppalette.h
--- app/core/gimppalette.h	26 Jul 2004 19:00:22 -0000	1.19
+++ app/core/gimppalette.h	19 Aug 2004 10:15:14 -0000
@@ -81,5 +81,10 @@ void               gimp_palette_set_n_co
                                                 gint              n_columns);
 gint               gimp_palette_get_n_columns  (GimpPalette      *palette);
 
+gint               gimp_palette_validate_index (GimpPalette      *palette,
+                                                gint              index);
+gint               gimp_palette_find_color_index(GimpPalette     *palette,
+                                                 const GimpRGB   *color);
+
 
 #endif /* __GIMP_PALETTE_H__ */
Index: app/widgets/gimppaletteeditor.c
===================================================================
RCS file: /cvs/gnome/gimp/app/widgets/gimppaletteeditor.c,v
retrieving revision 1.165
diff -u -p -r1.165 gimppaletteeditor.c
--- app/widgets/gimppaletteeditor.c	30 Jun 2004 12:10:08 -0000	1.165
+++ app/widgets/gimppaletteeditor.c	19 Aug 2004 10:15:58 -0000
@@ -610,11 +610,19 @@ palette_editor_color_area_button_press (
       if (list)
         {
           if (bevent->state & GDK_CONTROL_MASK)
-            gimp_context_set_background (user_context,
-                                         &editor->color->color);
+	    {
+              gimp_context_set_background (user_context,
+                                           &editor->color->color);
+              user_context->background_index = g_list_index(palette->colors,editor->color);
+	      g_warning("setting bg_index %d", user_context->background_index);
+            }
           else
-            gimp_context_set_foreground (user_context,
-                                         &editor->color->color);
+            {
+              gimp_context_set_foreground (user_context,
+                                           &editor->color->color);
+              user_context->foreground_index = g_list_index(palette->colors,editor->color);
+	      g_warning("setting fg_index %d", user_context->foreground_index);
+            }
 
           palette_editor_draw_entries (editor, row, col);
         }
