2 # the next line restarts using wish \
6 # This script implements a simple color editor, where you can
7 # create colors using either the RGB, HSB, or CYM color spaces
8 # and apply the color to existing applications.
10 # SCCS: @(#) tcolor 1.11 96/06/24 16:43:11
12 wm title . "Color Editor"
14 # Global variables that control the program:
16 # colorSpace - Color space currently being used for
17 # editing. Must be "rgb", "cmy", or "hsb".
18 # label1, label2, label3 - Labels for the scales.
19 # red, green, blue - Current color intensities in decimal
20 # on a scale of 0-65535.
21 # color - A string giving the current color value
22 # in the proper form for x:
24 # updating - Non-zero means that we're in the middle of
25 # updating the scales to load a new color,so
26 # information shouldn't be propagating back
27 # from the scales to other elements of the
28 # program: this would make an infinite loop.
29 # command - Holds the command that has been typed
30 # into the "Command" entry.
31 # autoUpdate - 1 means execute the update command
32 # automatically whenever the color changes.
33 # name - Name for new color, typed into entry.
39 set color #ffff00000000
44 # Create the menu bar at the top of the window.
46 frame .menu -relief raised -borderwidth 2
47 pack .menu -side top -fill x
48 menubutton .menu.file -text File -menu .menu.file.m -underline 0
50 .menu.file.m add radio -label "RGB color space" -variable colorSpace \
51 -value rgb -underline 0 -command {changeColorSpace rgb}
52 .menu.file.m add radio -label "CMY color space" -variable colorSpace \
53 -value cmy -underline 0 -command {changeColorSpace cmy}
54 .menu.file.m add radio -label "HSB color space" -variable colorSpace \
55 -value hsb -underline 0 -command {changeColorSpace hsb}
56 .menu.file.m add separator
57 .menu.file.m add radio -label "Automatic updates" -variable autoUpdate \
59 .menu.file.m add radio -label "Manual updates" -variable autoUpdate \
61 .menu.file.m add separator
62 .menu.file.m add command -label "Exit program" -underline 0 \
64 pack .menu.file -side left
66 # Create the command entry window at the bottom of the window, along
67 # with the update button.
69 frame .bot -relief raised -borderwidth 2
70 pack .bot -side bottom -fill x
71 label .commandLabel -text "Command:"
72 entry .command -relief sunken -borderwidth 2 -textvariable command \
74 button .update -text Update -command doUpdate
75 pack .commandLabel -in .bot -side left
76 pack .update -in .bot -side right -pady .1c -padx .25c
77 pack .command -in .bot -expand yes -fill x -ipadx 0.25c
79 # Create the listbox that holds all of the color names in rgb.txt,
80 # if an rgb.txt file can be found.
82 frame .middle -relief raised -borderwidth 2
83 pack .middle -side top -fill both
84 foreach i {/usr/local/lib/X11/rgb.txt /usr/lib/X11/rgb.txt
85 /X11/R5/lib/X11/rgb.txt /X11/R4/lib/rgb/rgb.txt
86 /usr/openwin/lib/X11/rgb.txt} {
87 if ![file readable $i] {
92 pack .middle.left -side left -padx .25c -pady .25c
93 listbox .names -width 20 -height 12 -yscrollcommand ".scroll set" \
94 -relief sunken -borderwidth 2 -exportselection false
95 bind .names <Double-1> {
96 tc_loadNamedColor [.names get [.names curselection]]
98 scrollbar .scroll -orient vertical -command ".names yview" \
99 -relief sunken -borderwidth 2
100 pack .names -in .middle.left -side left
101 pack .scroll -in .middle.left -side right -fill y
102 while {[gets $f line] >= 0} {
103 if {[llength $line] == 4} {
104 .names insert end [lindex $line 3]
111 # Create the three scales for editing the color, and the entry for
112 # typing in a color value.
115 pack .middle.middle -side left -expand yes -fill y
116 frame .middle.middle.1
117 frame .middle.middle.2
118 frame .middle.middle.3
119 frame .middle.middle.4
120 pack .middle.middle.1 .middle.middle.2 .middle.middle.3 -side top -expand yes
121 pack .middle.middle.4 -side top -expand yes -fill x
123 label .label$i -textvariable label$i
124 scale .scale$i -from 0 -to 1000 -length 6c -orient horizontal \
125 -command tc_scaleChanged
126 pack .scale$i .label$i -in .middle.middle.$i -side top -anchor w
128 label .nameLabel -text "Name:"
129 entry .name -relief sunken -borderwidth 2 -textvariable name -width 10 \
131 pack .nameLabel -in .middle.middle.4 -side left
132 pack .name -in .middle.middle.4 -side right -expand 1 -fill x
133 bind .name <Return> {tc_loadNamedColor $name}
135 # Create the color display swatch on the right side of the window.
138 pack .middle.right -side left -pady .25c -padx .25c -anchor s
139 frame .swatch -width 2c -height 5c -background $color
140 label .value -textvariable color -width 13 -font {Courier 12}
141 pack .swatch -in .middle.right -side top -expand yes -fill both
142 pack .value -in .middle.right -side bottom -pady .25c
144 # The procedure below is invoked when one of the scales is adjusted.
145 # It propagates color information from the current scale readings
146 # to everywhere else that it is used.
148 proc tc_scaleChanged args {
149 global red green blue colorSpace color updating autoUpdate
153 if {$colorSpace == "rgb"} {
154 set red [format %.0f [expr [.scale1 get]*65.535]]
155 set green [format %.0f [expr [.scale2 get]*65.535]]
156 set blue [format %.0f [expr [.scale3 get]*65.535]]
158 if {$colorSpace == "cmy"} {
159 set red [format %.0f [expr {65535 - [.scale1 get]*65.535}]]
160 set green [format %.0f [expr {65535 - [.scale2 get]*65.535}]]
161 set blue [format %.0f [expr {65535 - [.scale3 get]*65.535}]]
163 set list [hsbToRgb [expr {[.scale1 get]/1000.0}] \
164 [expr {[.scale2 get]/1000.0}] \
165 [expr {[.scale3 get]/1000.0}]]
166 set red [lindex $list 0]
167 set green [lindex $list 1]
168 set blue [lindex $list 2]
171 set color [format "#%04x%04x%04x" $red $green $blue]
172 .swatch config -bg $color
173 if $autoUpdate doUpdate
177 # The procedure below is invoked to update the scales from the
178 # current red, green, and blue intensities. It's invoked after
179 # a change in the color space and after a named color value has
182 proc tc_setScales {} {
183 global red green blue colorSpace updating
185 if {$colorSpace == "rgb"} {
186 .scale1 set [format %.0f [expr $red/65.535]]
187 .scale2 set [format %.0f [expr $green/65.535]]
188 .scale3 set [format %.0f [expr $blue/65.535]]
190 if {$colorSpace == "cmy"} {
191 .scale1 set [format %.0f [expr (65535-$red)/65.535]]
192 .scale2 set [format %.0f [expr (65535-$green)/65.535]]
193 .scale3 set [format %.0f [expr (65535-$blue)/65.535]]
195 set list [rgbToHsv $red $green $blue]
196 .scale1 set [format %.0f [expr {[lindex $list 0] * 1000.0}]]
197 .scale2 set [format %.0f [expr {[lindex $list 1] * 1000.0}]]
198 .scale3 set [format %.0f [expr {[lindex $list 2] * 1000.0}]]
204 # The procedure below is invoked when a named color has been
205 # selected from the listbox or typed into the entry. It loads
206 # the color into the editor.
208 proc tc_loadNamedColor name {
209 global red green blue color autoUpdate
211 if {[string index $name 0] != "#"} {
212 set list [winfo rgb .swatch $name]
213 set red [lindex $list 0]
214 set green [lindex $list 1]
215 set blue [lindex $list 2]
217 case [string length $name] {
218 4 {set format "#%1x%1x%1x"; set shift 12}
219 7 {set format "#%2x%2x%2x"; set shift 8}
220 10 {set format "#%3x%3x%3x"; set shift 4}
221 13 {set format "#%4x%4x%4x"; set shift 0}
222 default {error "syntax error in color name \"$name\""}
224 if {[scan $name $format red green blue] != 3} {
225 error "syntax error in color name \"$name\""
227 set red [expr $red<<$shift]
228 set green [expr $green<<$shift]
229 set blue [expr $blue<<$shift]
232 set color [format "#%04x%04x%04x" $red $green $blue]
233 .swatch config -bg $color
234 if $autoUpdate doUpdate
237 # The procedure below is invoked when a new color space is selected.
238 # It changes the labels on the scales and re-loads the scales with
239 # the appropriate values for the current color in the new color space
241 proc changeColorSpace space {
242 global label1 label2 label3
243 if {$space == "rgb"} {
250 if {$space == "cmy"} {
257 if {$space == "hsb"} {
259 set label2 Saturation
260 set label3 Brightness
266 # The procedure below converts an RGB value to HSB. It takes red, green,
267 # and blue components (0-65535) as arguments, and returns a list containing
268 # HSB components (floating-point, 0-1) as result. The code here is a copy
269 # of the code on page 615 of "Fundamentals of Interactive Computer Graphics"
270 # by Foley and Van Dam.
272 proc rgbToHsv {red green blue} {
287 set range [expr $max-$min]
291 set sat [expr {($max-$min)/$max}]
296 set rc [expr {($max - $red)/$range}]
297 set gc [expr {($max - $green)/$range}]
298 set bc [expr {($max - $blue)/$range}]
300 set hue [expr {.166667*($bc - $gc)}]
302 if {$green == $max} {
303 set hue [expr {.166667*(2 + $rc - $bc)}]
305 set hue [expr {.166667*(4 + $gc - $rc)}]
309 set hue [expr $hue + 1.0]
312 return [list $hue $sat [expr {$max/65535}]]
315 # The procedure below converts an HSB value to RGB. It takes hue, saturation,
316 # and value components (floating-point, 0-1.0) as arguments, and returns a
317 # list containing RGB components (integers, 0-65535) as result. The code
318 # here is a copy of the code on page 616 of "Fundamentals of Interactive
319 # Computer Graphics" by Foley and Van Dam.
321 proc hsbToRgb {hue sat value} {
322 set v [format %.0f [expr 65535.0*$value]]
326 set hue [expr $hue*6.0]
332 set p [format %.0f [expr {65535.0*$value*(1 - $sat)}]]
333 set q [format %.0f [expr {65535.0*$value*(1 - ($sat*$f))}]]
334 set t [format %.0f [expr {65535.0*$value*(1 - ($sat*(1 - $f)))}]]
336 0 {return "$v $t $p"} \
337 1 {return "$q $v $p"} \
338 2 {return "$p $v $t"} \
339 3 {return "$p $q $v"} \
340 4 {return "$t $p $v"} \
341 5 {return "$v $p $q"}
342 error "i value $i is out of range"
346 # The procedure below is invoked when the "Update" button is pressed,
347 # and whenever the color changes if update mode is enabled. It
348 # propagates color information as determined by the command in the
354 regsub -all %% $command $color newCmd