get rgb values from tkinter color names

1.2k Views Asked by At

I need a mapping between every tkinter "color name" and "color value(in rgb e.g.)". It could be a one-by-one corresponding list or a function to convert those.

There are functions like matplotlib's hex2color or to_rgba but they don't support all tkinter color names as some of them are too special to the module(like "ghost white"); and I wonder of course there must be one specific to tkinter itself as its developers has needed that to implement the module.

1

There are 1 best solutions below

0
On

Use the winfo_rgb method. If you pass it yellow, it will return the rgb value (16 bit) which you can then just divide by 256 to get the approx 8 bit value.

root.winfo_rgb('yellow')

returns (65535, 65535, 0)

root.winfo_rgb('ghostwhite')

returns (63736, 63736, 65535)

A oneliner to get the 8bit RGB values as a tuple is

rgb = tuple((c//256 for c in root.winfo_rgb('ghostwhite')))