FE 'tkGooie' Utilities

'SELECTORtools' group

A simple, non-obfuscated
RGB Color Selector GUI

(FE = Freedom Environment)

RGB Color Selector GUI
--- used in the several
FE subsystems.

FE Home Page > FE Downloads Page >

FE 'tkGooies' Description Page >

FE 'tkGooies' 'SELECTORtools' Page >

This RGB Color Selector Page

simple, non-obfuscated RGB Color Selector GUI

POSTED: 2012aug06

Way back around 1998 when I was first learning the Tcl-Tk language, I would do web searches on keyword strings such as 'usr bin wish button' or 'usr bin wish scale rgb' to find examples of complete Tcl-Tk scripts --- because the code 'snippets' in Tcl-Tk books and tutorials were not very helpful in creating 'production' scripts (relatively quickly).

In doing those kinds of web searches, I ran across a Tcl-Tk script that offered a nice, simple color selector GUI consisting of 3 'scale' widgets for setting RGB values (from 0 to 255) --- along with a 'frame' widget color-swatch for dynamically showing the color as the user moved any of the three scale-sliders with the mouse-button-1.

I used the word 'non-obfuscated' in the title of this page because many of the color selectors that I have seen are quite confusing.

They hit my eyes with so many features --- color shaded disks, RGB sliders, HS-whatever sliders, etc. etc. --- that I get a headache (and waste too much time) trying to figure out how to use them.

This color selector is so simple that it is obvious 'from the git-go' how to use it.

And yet it has done all that I need it to do in all the 'use cases' that I have encountered so far.

The original script was posted in a web page for a computer science class (probably by a professor or student) at an Australian university (USQ = the University of Southern Queensland).

When I tried going back to the web page around 2004, I found it was gone.

So I am glad that I 'harvested' that script code before it disappeared into that great bit bucket in the sky.

(The URL for that dearly-departed Australian web page is in the comments of the code below.)

Changes to the Australian script :

I changed the original script quite a bit. Some of the changes that I can remember are:

1) The 'place' geometry manager was originally used in the script. I switched to the pack command.

2) The original script showed the current RGB value in '#rrggbb' hex format, in a label widget. Like the color swatch, the hex values changed dynamically as the sliders were moved with the mouse.

I changed the label widget to a text widget, so that one could copy-and-paste the hex-string with a 'left mouse button swipe' for the 'copy' and a middle-mouse-button click for the 'drop' ('paste') --- handy for pasting into a text editor window containing some Tk code or some HTML code.

I also added a text widget to show the RGB values (between 0 and 255) as percents (between 0 and 100).

3) Using a pack forget command technique, I made a Toggle-Side button on the GUI so that I could quickly switch the color swatch from one side of the GUI to the other.

4) I made a "put_vars" proc that uses 'puts' to output the current RGB values (decimal and hex), when a 'UseIt' button is clicked --- so that I can use the color selector GUI within shell scripts and within other Tcl-Tk scripts --- to pass a selected RGB value back to the calling script.

5) There are a lot of changes that I made to the script to share parameter setting code that is used in other Tk scripts of mine.

6) The font setting code was updated to use the newer constructs in Tk that came in 8.x releases of Tk.

7) I added some argc,argv code to, optionally, accept initial RGB values from the command line.

8) I added some 'catch' statements to, optionally, accept initial RGB values from environment variables --- R255, G255, B255.

9) I added some 'catch' statements to, optionally, accept window and window-icon titles from environment variables --- and to, optionally, accept window geometry (location or location-and-size) from an environment variable.

Here is an image of the resultant color selector GUI.

After I retired and started assembling the software in my 'Freedom Environment' software system (see www.freedomenv.com), I used the color selector Tk script as a palette-color setting utility in the 'xpg', 'feAppMenus, and 'feHandyTools' subsystems of my Freedom Environment software.

I provide all that FE code as free and open source code.

And, to help make sure that the code from that Australian professor/student (along with my enhancements) does not disappear into the Internet Graveyard of Dead Links, here is a resurrection of the code.

I have 'left in' many of my comments that explain the usage of the GUI and the structure and intent of the code.

In my FE subsystems, I use some Tk 'include' files in my Tk scripts to provide some parameter setting that is shared with other utility scripts.

I have replaced the corresponding 'source' statements with the essential Tk statements from those 'include' code-files.

    (Some of the 'include' statements involving parameters for 'listbox' and 'entry' and 'message' widgets, widgets not used in this GUI, may be left in this code --- but commented, for the most part.

    Those statments can be useful if other GUIs are made using this code as a starting point.)

As I do with all my Tk scripts, I have put the four main pack parameters --- '-side', '-anchor', '-fill', '-expand' --- on the 'pack' command for the various frames and widgets.

That helps me when I am initially testing the behavior of a GUI (the various widgets within it) as I resize the main window.

For this particular GUI, I have used the statement

wm resizeable . 0 0

to make the window (and its widgets) a fixed size --- since I have found no advantage in having the color swatch change size --- and no advantage to having the scale widgets change size.

However, if anyone sees an advantage to allowing the GUI to change size, they can comment the 'wm resizeable' statement --- and then experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various widgets --- to get the widget behavior that they want.

    (Since I did some experimenting with the GUI before making it non-resizable, the pack parameters in this code are probably at reasonable settings.

    So if you comment out the 'wm resizeable' statement, the GUI elements may resize --- or not resize --- 'nicely' as you change window size.)


Here is a link to the CODE for Tk script
sho_colorvals_via_sliders3rgb.tk --- or use a shorter name, if you prefer.

I hope that some Tcl-Tk 'newbies' can learn from this useful script, just as I learned a lot from its predecessor script --- thanks to some (unknown to me) computer science professor/student at an Australian University half-way around the world from me --- me sitting at an SGI/IRIX (Unix) workstation in the eastern United States back in the late 1990's.

2014mar13 UPDATE

It has been more than a year and a half since my last update to this utility.

Since I use (and refer to) this utility in more than 20 different code contributions on this FE web site (and the wiki.tcl.tk wiki), I think it is worth taking the time to make a few improvements:

1) I added an 'aRtext' array to hold text for labels, buttons, and other widgets of the GUI. In mid-2013, I started doing this in all my Tk code contributions on this wiki. This makes it easier for people to 'internationalize' this script. Most of the text-setting is done in one section near the top of the script.

2) I added some relief, borderwidth, and padding parameters to widget definition statements --- to facilitate experiments in changing/improving the appearance of the GUI.

3) I changed the '-relief' parameter for the two text widgets that hold the RGB values in percents and in hex --- from "raised" to "ridge" --- so that the two text widgets do not look like buttons to be clicked.

I replaced the code above with code containing these changes.

Bottom of this page for a
simple, unobfuscated RGB Color Selector
--- a utility in the FE 'tkGooies' system,
in the 'SELECTORtools' group.

To return to a previously visited web page location, click on the Back button of your web browser a sufficient number of times. OR, use the History-list option of your web browser.
OR ...

< Go to Top of Page, above. >

Page history:

This code was posted on 2012 Aug 06 at

This FE web page was created 2014 May 05 ---
as a backup and alternative to the wiki.tc.tk page.

This page was changed slightly 2015 Oct 05.

Page was changed 2019 Feb 23.
(Added css and javascript to try to handle text-size for smartphones, esp. in portrait orientation.)

Page was changed 2019 Jun 13.
(Specified image widths in percents to size the images according to width of the browser window.)

The code here MAY become more 'up-to-date' than the code posted on the Tcler's Wiki --- wiki.tcl-lang.org --- formerly wiki.tcl.tk.