03-15-2020, 10:37 AM
For the colors:
To swap FG/BG colors use gimp_context_swap_colors
To set a given color you need the help of the gimpcolor module:
Note that in these calls/constructors, when you use integers they are understood to be in the 0-255 range (or 0-360 for hues) and when you use floats they are understood as values in the 0.0-1.0 range. gimpcolors has several color definitions (RHB, HSV, HSL, CYMK) but the Gimp API wants the RGB type (hence the to_rgb() method on most).
Since you change the context, you should normally restore it before exiting the script, this is done by bracketing your script code between
gimp_context_push() and gimp_context_pop(). In practice to make sure that this happens you also bracket your code with a try/catch and restore the context after you have caught any exception. And while you are at it you can also bracket your code with undo markers so that you can undo all the script actions with a single Ctrl-Z.
The problem with catching the exception is that it masks errors and makes debugging difficult but you can retrieve the exception information to display it, so your plugin code becomes:
Or if you prefer the lighter form with Gimp objects:
To swap FG/BG colors use gimp_context_swap_colors
To set a given color you need the help of the gimpcolor module:
Code:
import gimpcolor
yellow=gimpcolor.RGB(255,255,0)
yellow=gimpcolor.RGB(1.,1.,0.)
yellow=gimpcolor.HSV(60,100,100).to_rgb()
yellow=gimpcolor.HSV(.166666,1.,1.).to_rgb(()
pdb.gimp_context_set_foreground(yellow)
Note that in these calls/constructors, when you use integers they are understood to be in the 0-255 range (or 0-360 for hues) and when you use floats they are understood as values in the 0.0-1.0 range. gimpcolors has several color definitions (RHB, HSV, HSL, CYMK) but the Gimp API wants the RGB type (hence the to_rgb() method on most).
Since you change the context, you should normally restore it before exiting the script, this is done by bracketing your script code between
gimp_context_push() and gimp_context_pop(). In practice to make sure that this happens you also bracket your code with a try/catch and restore the context after you have caught any exception. And while you are at it you can also bracket your code with undo markers so that you can undo all the script actions with a single Ctrl-Z.
The problem with catching the exception is that it masks errors and makes debugging difficult but you can retrieve the exception information to display it, so your plugin code becomes:
Code:
import traceback
def pluginFunction(image,...):
pdb.gimp_image_undo_group_start(image)
pdb.gimp_context_push()
try:
##
# Your actual code goes here
##
except Exception as e:
traceback.print_exc() # Terminal (Linux, OSX...)
pdb_gimp_message(traceback.format_exc()) # Gimp error console
pdb.gimp_context_pop()
pdb.gimp_image_undo_group_end(image)
Or if you prefer the lighter form with Gimp objects:
Code:
import traceback
def pluginFunction(image,...):
image.undo_group_start()
gimp.context_push()
try:
##
# Your actual code goes here
##
except Exception as e:
traceback.print_exc() # Terminal (Linux, OSX...)
gimp.message(traceback.format_exc()) # Gimp error console
gimp.context_pop()
image.undo_group_end()