简体   繁体   中英

get rgb values from tkinter color names

I need a mapping between every tkinter "color name" and "color value(in rgb eg)". 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.

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')))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM