views:

91

answers:

2

I know I can call Tkinter.Tk().winfo_rgb(color) to get a tuple of values that represent the named color.

for instance Tkinter.Tk().winfo_rgb("red") returns (65535, 0, 0)

The problem is it also opens a window. I was hoping to abstract some color calculations into a generic color class, and handle whether or not the class was instantiated with "red" or "#ff0000" or maybe even some other formats.

With the class abstracted, I don't have a tk parent to pull this info from without instantiating a new window, or passing in a parent.

Is there any way to get this kind of colorname to hex value conversion without having an instantiated Tk window?

A: 

You should look at the PIL (Python Imaging Library), and in particular the ImageDraw module, which support Hex, RGB, HSL and common color names. You should be able to get the information you need there.

Shane C. Mason
I guess the problem there might be if tkinter and PIL don't share the same dictionary of colors. Do you know if they do?
Gaidin
No, but these are just common HTML colors. If it is important that you get the tkinter color, cant you just create a REALLY small window without decorations (1x1px) or write a script that would prebuild these conversions for you and then just have a class of constants?
Shane C. Mason
A: 

Instantiate Tk(), run the code once, and then stick the information into your source as a dict literal?

John Fouhy