简体   繁体   English

从 tkinter 颜色名称中获取 rgb 值

[英]get rgb values from tkinter color names

I need a mapping between every tkinter "color name" and "color value(in rgb eg)".我需要每个 tkinter“颜色名称”和“颜色值(在 rgb 中)”之间的映射。 It could be a one-by-one corresponding list or a function to convert those.它可以是一一对应的列表或 function 来转换它们。

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");有像 matplotlib 的 hex2color 或 to_rgba 这样的函数,但它们不支持所有 tkinter 颜色名称,因为其中一些对模块来说太特殊了(比如“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.我想知道当然必须有一个特定于 tkinter 本身,因为它的开发人员需要它来实现该模块。

Use the winfo_rgb method.使用winfo_rgb方法。 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.如果您将其传递为黄色,它将返回 rgb 值(16 位),然后您可以将其除以 256 以获得大约 8 位的值。

root.winfo_rgb('yellow')

returns (65535, 65535, 0)返回 (65535, 65535, 0)

root.winfo_rgb('ghostwhite')

returns (63736, 63736, 65535)返回(63736、63736、65535)

A oneliner to get the 8bit RGB values as a tuple is将 8 位 RGB 值作为元组获取的 oneliner 是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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