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.
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?
3
Answers
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?
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).
It’s a bit late answer but my solution is creating a custom class. I have used it many times in my image processing projects in order to get color mask (or filter) from the user. Here comes the class…
And call it like that;
And results:
Just drag&drop [S]tart and [E]nd sliders on selector’s canvas
click here for screenshot