2014-09-04 13 views
1

我想將顏色從這些常量傳遞到下面的設置fontcolor函數,但是每次我都會得到「無法解析顏色名稱」除非我直接從GIMP對話框傳遞它。我甚至直接記錄了正在傳入的變量,來自編號2的值是來自日誌的直接拷貝。任何人都可以看到我在做什麼錯或在這裏失蹤。由於GIMP Python無論我使用什麼格式,我總是會得到「無法解析顏色名稱」

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() 
+0

對不起,這是我的一個錯字,在我的原始代碼中是正確的。 – rtsnhth 2014-09-04 08:45:24

回答

1

傳遞給GIMP的PDB函數的顏色參數可以是一個字符串,或可在各種不同的方式來解釋3號序列。

如果你傳遞一個字符串,它接受CSS顏色名稱,如"red""blue" - 或十六進制RGB代碼由「#」像"#ff0000"或「#0f0」爲前綴 - 但它不接受CSS功能風格的語法爲字符串即沒有"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爲藍色。因此,如果您得到一個CSS字符串,其他可能包含「RGB(...)」序列的字符串,可能使其工作的最簡單方法是去除「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綁定)

+0

謝謝你,我發現就像你說的那樣,問題在於我傳遞了一個字符串而不是預期的元組。當我將我的常量更改爲:FontGREEN2 =(0.0,1.0,0.0,1.0)時,它按預期工作。 – rtsnhth 2014-10-14 08:05:10

相關問題