2

我试图将这些常量中的颜色传递给下面的 Set fontcolor 函数,但每次我这样做时都会得到“无法解析颜色名称”,除非我直接从 GIMP 对话框传递它。我什至记录了直接传入的变量,数字 2 中的值是日志的直接副本。谁能看到我做错了什么或错过了什么。谢谢

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()
4

1 回答 1

3

传递给 GIMP 的 PDB 函数的 Color 参数可以是字符串,也可以是可以以多种方式解释的 3 位数序列。

如果您传递一个字符串,它接受 CSS 颜色名称,如"red", - 或以“#”或“ ”"blue" 为前缀的十六进制 RGB 代码- 但它不接受 CSS 函数样式语法作为字符串,即不作为字符串传递。"#ff0000"#0f0"RGB(val1, val2, val3)"

相反,您可以将 3 数字序列作为任何接受颜色值的参数传递。如果您的三个数字是整数,则它们被解释为每个组件的传统 0-255 范围内。前任:

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

将前景色设置为红色

如果任何数字是浮点数,则 3 序列被解释为 0-1.0 范围内的 RGB 数字:

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

将 FG 设置为蓝色。

所以,如果你得到一个可能包含“RGB (...)”序列的 CSS 字符串,也许最简单的让它工作的方法是去掉“RGB”字符并将颜色解析为一个元组 - 和将该元组提供给 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)

如果您想要更多控制并拥有可以传递的真实“颜色”对象,请检查可以从 GIMP 插件导入的“gimpcolor”模块。在大多数情况下不需要它,但如果您需要生成输出,或者自己解析颜色名称,甚至进行一些简单的 RGB<->HSL<->CMYK 转换,它会很有用。(尽管它们没有考虑颜色配置文件或颜色空间 - 应该使用 GEGL 及其 Python 绑定)

于 2014-09-22T20:49:16.720 回答