Search code examples
pythongimpgimpfu

GIMP Python No matter what format I use I always get "unable to parse color name"


I am trying to pass colors from these constants to to the Set fontcolor function below but everytime I do I get "unable to parse color name" unless I pass it directly from a GIMP Dialog. I even logged the variables being passed in directly, the value from number 2 is a direct copy from the log. Can anyone see what I am doing wrong or missing here. Thanks

FontGREEN1 = '(RGB(0,255,0,))'
FontGREEN2 = 'RGB (0.0, 1.0, 0.0, 1.0)'

#This causes the error
def setColor1 ( txtlayer, color):
    color = FontGREEN1  
    pdb.gimp_text_layer_set_color(txtlayer, color)

#This causes the error
def setColor2 ( txtlayer ):
    color = FontGREEN2  
    pdb.gimp_text_layer_set_color(txtlayer, color)



#this work fine, color passed directly from GIMP Dialog
def setColor3 ( txtlayer, color):
    pdb.gimp_text_layer_set_color(txtlayer, color)

def setTextColor (img, drw, color ):
    txtlayer = img.active_layer
    setColor3(txtlayer, color)

register(
    'setTextColor',
    'Changes the color of the text with python',
    'Changes the color of the text with python',
    'RS',
    '(cc)',
    '2014',
    '<Image>/File/Change Text Color...',
    '',  # imagetypes
    [
        (PF_COLOR,"normal_color","Green Color of the Normal Font",(0,234,0)   ),
    ], # Parameters
    [], # Results
    setTextColor)
    main()

Solution

  • The Color parameter passed to GIMP's PDB functions may be a string, or a 3-number sequence that can be interpreted in a variety of ways.

    If you pass a string, it accepts CSS color names, like "red", "blue" - or Hexadecimal RGB codes prefixed by an "#" like "#ff0000" or "#0f0" - but it does not accept CSS function style syntax as a string i.e. no "RGB(val1, val2, val3)" passed as strings.

    Instead, you can either pass a 3 number sequence as any argument that accepts color values. If your three numbers are integers, they are interpreted as being on the traditional 0-255 range for each component. Ex:

    pdb.gimp_context_set_foreground((255,0,0))

    sets the foreground color to Red

    If any of the numbers is a float, the 3-sequence is interpreted as RGB numbers in the 0-1.0 range:

    pdb.gimp_context_set_foreground((0,0,1.0))

    sets FG to Blue.

    So, if you get a CSS string somehwere else that might contain a "RGB (...)" sequence, perhaps the simplest way to make it work is to strip out the "RGB" characters and parse the colors as a tuple - and feed that tuple to GIMP:

    >>> mycolor = "RGB (230, 128, 0)"
    >>> from ast import literal_eval
    >>> color_tuple = literal_eval("(" + mycolor.split("(", 1)[-1])
    >>> pdb.gimp_context_set_foreground(color_tuple)
    

    If you want more control and have real "color" objects that can be passed around, check the "gimpcolor" module that can be imported from a GIMP plug-in. It is not needed in most cases, but can be usefull if you need to generate the output, or parse color names yourself, or even do some naive RGB<->HSL<->CMYK conversions. (they don't take into account color profiles or color spaces though - one should use GEGL and its Python bindings for that)