简体   繁体   中英

How do I make a color selection scale in Tkinter?

I'm trying to create a color selection scale for a Tkinter application.

I'm not interested in the tkinter.colorchooser module or separate scales for each RGB channel. I need a single scale like the color slider in Photoshop.

What I currently have is a vertical scale widget with RGB integer values going from 0 ( #000 ) to 16777215 ( #FFF ). The problem is that using a range of RGB integers results in a very odd sequence of colors—the selection doesn't go from red to pink to blue, etc. as in the first image below.

Instead I get the sequence of colors in the second image. It's heavily compressed because there are 16777215 different colors squeezed into 300 pixels. The third picture is a zoom. You can see that it goes from black to green, then almost black to green, etc.

Photoshop中的颜色滑块
基于RGB整数的渐变
仔细看看渐变

Here's the relevant part of my code:

MAXRGBINT = 16777215  # Corresponds to (255, 255, 255)
scale = ttk.Scale(slidframe, orient=VERTICAL, length=75,
                  from_=0, to=MAXRGBINT,
                  command=lambda _: set_color('sprite'))

Basically I want a scale widget that will go through colors in the "normal" order. How can I do this with numerical values?

Your problem is that you are using a one-dimensional scale for a three-dimensional color space.

For what you're looking for, I would suggest using the HSL or HSV color space instead, with the scale in question manipulating the Hue parameter. When you do need RGB values, you can obtain them by conversion from the HSL/HSV values (the Wikipedia page I linked explains how).

You could use either use matplotlib's premade colormaps (jet, rainbow, etc.) from matplotlib import cm or create your own and then go through the rgb values. You can use the fact that the standard ones are usually separated in 256 values ( last paragraph ). If that is enough for you the latter would be probably the easiest.

Is this what you were asking?

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