How can I switch colors using keyboard shortcuts in GIMP?

9

1

I'm making screencasts (like the ones on Khan Academy) with GIMP as a virtual blackboard.

Right now, switching foreground colors is a bit of a hassle--I have to move my pen over to the palette in my toolbox, click on a color, then move my pen back over to the image window. This takes time, especially when switching colors rapidly.

How can I assign keyboard shortcuts to colors in my palette, so I can access them easier?

Qrtn

Posted 2014-07-12T22:03:14.490

Reputation: 191

Answers

13

In my case (which brought me to your question) D for resetting and X for swapping colors is sufficient. Combined with O you can maybe set up some nice workarounds.

Default Colors

By default, GIMP sets the foreground color to black and the background color to white and it can be surprising how often you want to use these two colors. To reset these colors quickly, just press the D key. Also you can easily swap the foreground and background colors by pressing the X key.

Source: http://graphicssoft.about.com/od/gimptutorials/a/useful-keyboard-shortcuts.htm

nuala

Posted 2014-07-12T22:03:14.490

Reputation: 277

Thanks for replying! I've resorted to using AutoHotkey to map my function keys to click coordinates on-screen then refocus the canvas when in GIMP. It's not very flexible, though, since I have to make sure the windows are in the same spot every time I start up my blackboard. – Qrtn – 2014-08-26T21:33:11.863

1

As far as I know, no such functionality exists in GIMP. As you probably already know, GIMP was neither designed for art nor screencasting, and, as such, would have little need for such a feature.

However, assuming that you don't need to see the entire screen (your screen recorder just uses the part of GIMP that is the canvas), you could set up several colors using the Pencil or Paintbrush tool outside the visible area to create an actual virtual "palette." It would then be as simple as pressing the O key to get the eyedropper tool, then clicking on one of the colors you put out.

fakedad

Posted 2014-07-12T22:03:14.490

Reputation: 384

Thank you for the reply. O is indeed a very useful shortcut! In my case, however, my screen recorder (OBS) is most easily set up to record an entire window, which would be my shrink-wrapped, 1024x768 canvas. – Qrtn – 2014-08-26T21:32:23.677

1

I wanted something similar so I've written a short script. This is my first effort with a lisp like language so I'm sure the example below could be improved, but it works for me on gimp 2.8.

;; script-fu to cycle between a set of foreground colours
;; edit the variable colours to modify the set
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License.

(define (script-fu-cycle-foreground-colour)

 ;add to or edit the list of colours to cycle here:   
 (define colours (list '(255 255 255) '(255 0 0) '(0 255 0) '(0 0 255) '(100 100 100)))

 (define list-index
  (lambda (e lst)
  (if (null? lst)
   -1
   (if (equal? (car lst) e)
     0
     (if (= (list-index e (cdr lst)) -1)
      -1
      (+ 1 (list-index e (cdr lst)))
     )
    )
   )
  )
 )


 (gimp-context-set-foreground (list-ref colours (modulo (+ 1 (list-index (car (gimp-context-get-foreground)) colours)) (length colours))))

 (gimp-displays-flush)
)

(script-fu-register "script-fu-cycle-foreground-colour"
         _"<Image>/Colors/Cycle FG"
         _"Cycles foreground colour"
         "Jan Marchant"
         "Jan Marchant"
         "January 2015"
         "*"
)

If you just want to assign individual shortcuts for specific colours, I guess something really simple like this would work (not tested):

;; script-fu to cycle between foreground colours
;; 
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License.

(define (script-fu-foreground-red)

 (gimp-context-set-foreground '(255 0 0))

 (gimp-displays-flush)
)

(script-fu-register "script-fu-foreground-red"
         _"<Image>/Colors/Foreground/Red"
         _"Sets foreground to red"
         "Jan Marchant"
         "Jan Marchant"
         "January 2015"
         "*"
)

For Linux save your finished scripts in your $HOME/gimp-x.y/scripts directory edit: you must use the extension .scm (similar for other OSes I guess) and then navigate to "Filters - Script-Fu - Refresh Scripts" in gimp. You should find new menu items "Colors - Cycle FG" and "Colors - Foreground - Red", and you can assign keyboard shortcuts to them (under plug-ins but easiest if you use the search box).

Of course you can extend to as many colours as you like.

Cheers, Jan

JM1

Posted 2014-07-12T22:03:14.490

Reputation: 11

1

The scheme solution is really slow when using 15+ colors, so I created this python script to cycle through a list of 20+ colors, and it does so much quicker.

#!/usr/bin/env python

# this plug-in would normally be installed in the GIMP 2\lib\gimp\2.0\plug-ins folder 
# you may need to restart GIMP for this plug-in to appear in the Colors menu
# this has been tested on Windows with Gimp 2.8.14
from gimpfu import *

def color_cycle() :
    #this code sends a message back to Gimp communicating status
    pdb.gimp_message('Attempting to set the foreground color...')

    #you can change the colors in this list to suit your needs, but every color should be unique
    colors = [(0, 0, 0), 
              (236, 236, 236), 
              (110, 110, 110), 
              (87, 87, 87), 
              (41, 28, 19),
              (255, 255, 35),
              (216, 216, 1),
              (1, 216, 6),
              (0, 119, 3),
              (0, 44, 1),
              (86, 160, 211),
              (2, 41, 255),
              (1, 22, 142),
              (0, 13, 81),
              (38, 0, 58),
              (125, 1, 188),
              (255, 192, 203),
              (255, 129, 213),
              (223, 1, 41),
              (134, 1, 25),
              (42, 0, 8),
              (224, 97, 2)
              ]

    #backup the background color
    bg = gimp.get_background()

    i = 0
    bFound = False

    while (bFound == False):
        #using background to compare helps with floating point precision problems
        gimp.set_background(colors[i])
        if (gimp.get_foreground() == gimp.get_background()):
            bFound = True
            i += 1
        else:
            i += 1
            if (i > (len(colors) - 1)):
                i = 0
                bFound = True

    if i > len(colors) - 1:
        i = 0

    #if current color found in colors, then return the next one, otherwise return the first one
    color = colors[i]
    gimp.set_foreground(color)
    #restore the backed-up background color
    gimp.set_background(bg)
    pdb.gimp_message('Done setting the foreground color...')

register(
    "python_fu_color_cycle_fg",
    "Color Cycling",
    "Cycle the foreground through a list of colors.",
    "David Zahn",
    "David Zahn",
    "2015",
    "Color Cycle ForeGround",
    "*",      # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
    [
    ],
    [],
    color_cycle, menu="<Image>/Colors")

main()

David Zahn

Posted 2014-07-12T22:03:14.490

Reputation: 11