Update tk to version 8.5.7
[git/jnareb-git.git] / mingw / lib / tk8.5 / demos / tcolor
blobb695c60827d9584d54b98558d1dd06e3021f1f49
1 #!/bin/sh
2 # the next line restarts using wish \
3 exec wish "$0" "$@"
5 # tcolor --
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 # RCS: @(#) $Id: tcolor,v 1.6 2007/12/13 15:27:07 dgp Exp $
12 package require Tk 8.4
13 wm title . "Color Editor"
15 # Global variables that control the program:
17 # colorSpace - Color space currently being used for
18 # editing. Must be "rgb", "cmy", or "hsb".
19 # label1, label2, label3 - Labels for the scales.
20 # red, green, blue - Current color intensities in decimal
21 # on a scale of 0-65535.
22 # color - A string giving the current color value
23 # in the proper form for x:
24 # #RRRRGGGGBBBB
25 # updating - Non-zero means that we're in the middle of
26 # updating the scales to load a new color,so
27 # information shouldn't be propagating back
28 # from the scales to other elements of the
29 # program: this would make an infinite loop.
30 # command - Holds the command that has been typed
31 # into the "Command" entry.
32 # autoUpdate - 1 means execute the update command
33 # automatically whenever the color changes.
34 # name - Name for new color, typed into entry.
36 set colorSpace hsb
37 set red 65535
38 set green 0
39 set blue 0
40 set color #ffff00000000
41 set updating 0
42 set autoUpdate 1
43 set name ""
45 # Create the menu bar at the top of the window.
47 . configure -menu [menu .menu]
48 menu .menu.file
49 .menu add cascade -menu .menu.file -label File -underline 0
50 .menu.file add radio -label "RGB color space" -variable colorSpace \
51 -value rgb -underline 0 -command {changeColorSpace rgb}
52 .menu.file add radio -label "CMY color space" -variable colorSpace \
53 -value cmy -underline 0 -command {changeColorSpace cmy}
54 .menu.file add radio -label "HSB color space" -variable colorSpace \
55 -value hsb -underline 0 -command {changeColorSpace hsb}
56 .menu.file add separator
57 .menu.file add radio -label "Automatic updates" -variable autoUpdate \
58 -value 1 -underline 0
59 .menu.file add radio -label "Manual updates" -variable autoUpdate \
60 -value 0 -underline 0
61 .menu.file add separator
62 .menu.file add command -label "Exit program" -underline 0 -command {exit}
64 # Create the command entry window at the bottom of the window, along
65 # with the update button.
67 labelframe .command -text "Command:" -padx {1m 0}
68 entry .command.e -textvariable command
69 button .command.update -text Update -command doUpdate
70 pack .command.update -side right -pady .1c -padx {.25c 0}
71 pack .command.e -expand yes -fill x -ipadx 0.25c
74 # Create the listbox that holds all of the color names in rgb.txt,
75 # if an rgb.txt file can be found.
77 grid .command -sticky nsew -row 2 -columnspan 3 -padx 1m -pady {0 1m}
79 grid columnconfigure . {1 2} -weight 1
80 grid rowconfigure . 0 -weight 1
81 foreach i {
82 /usr/local/lib/X11/rgb.txt /usr/lib/X11/rgb.txt
83 /X11/R5/lib/X11/rgb.txt /X11/R4/lib/rgb/rgb.txt
84 /usr/openwin/lib/X11/rgb.txt
85 } {
86 if {![file readable $i]} {
87 continue;
89 set f [open $i]
90 labelframe .names -text "Select:" -padx .1c -pady .1c
91 grid .names -row 0 -column 0 -sticky nsew -padx .15c -pady .15c -rowspan 2
92 grid columnconfigure . 0 -weight 1
93 listbox .names.lb -width 20 -height 12 -yscrollcommand ".names.s set" \
94 -exportselection false
95 bind .names.lb <Double-1> {
96 tc_loadNamedColor [.names.lb get [.names.lb curselection]]
98 scrollbar .names.s -orient vertical -command ".names.lb yview"
99 pack .names.lb .names.s -side left -fill y -expand 1
100 while {[gets $f line] >= 0} {
101 if {[regexp {^\s*\d+\s+\d+\s+\d+\s+(\S+)$} $line -> col]} {
102 .names.lb insert end $col
105 close $f
106 break
109 # Create the three scales for editing the color, and the entry for
110 # typing in a color value.
112 frame .adjust
113 foreach i {1 2 3} {
114 label .adjust.l$i -textvariable label$i -pady 0
115 labelframe .adjust.$i -labelwidget .adjust.l$i -padx 1m -pady 1m
116 scale .scale$i -from 0 -to 1000 -length 6c -orient horizontal \
117 -command tc_scaleChanged
118 pack .scale$i -in .adjust.$i
119 pack .adjust.$i
121 grid .adjust -row 0 -column 1 -sticky nsew -padx .15c -pady .15c
123 labelframe .name -text "Name:" -padx 1m -pady 1m
124 entry .name.e -textvariable name -width 10
125 pack .name.e -side right -expand 1 -fill x
126 bind .name.e <Return> {tc_loadNamedColor $name}
127 grid .name -column 1 -row 1 -sticky nsew -padx .15c -pady .15c
129 # Create the color display swatch on the right side of the window.
131 labelframe .sample -text "Color:" -padx 1m -pady 1m
132 frame .sample.swatch -width 2c -height 5c -background $color
133 label .sample.value -textvariable color -width 13 -font {Courier 12}
134 pack .sample.swatch -side top -expand yes -fill both
135 pack .sample.value -side bottom -pady .25c
136 grid .sample -row 0 -column 2 -sticky nsew -padx .15c -pady .15c -rowspan 2
139 # The procedure below is invoked when one of the scales is adjusted.
140 # It propagates color information from the current scale readings
141 # to everywhere else that it is used.
143 proc tc_scaleChanged args {
144 global red green blue colorSpace color updating autoUpdate
145 if {$updating} {
146 return
148 switch $colorSpace {
149 rgb {
150 set red [format %.0f [expr {[.scale1 get]*65.535}]]
151 set green [format %.0f [expr {[.scale2 get]*65.535}]]
152 set blue [format %.0f [expr {[.scale3 get]*65.535}]]
154 cmy {
155 set red [format %.0f [expr {65535 - [.scale1 get]*65.535}]]
156 set green [format %.0f [expr {65535 - [.scale2 get]*65.535}]]
157 set blue [format %.0f [expr {65535 - [.scale3 get]*65.535}]]
159 hsb {
160 set list [hsbToRgb [expr {[.scale1 get]/1000.0}] \
161 [expr {[.scale2 get]/1000.0}] \
162 [expr {[.scale3 get]/1000.0}]]
163 set red [lindex $list 0]
164 set green [lindex $list 1]
165 set blue [lindex $list 2]
168 set color [format "#%04x%04x%04x" $red $green $blue]
169 .sample.swatch config -bg $color
170 if {$autoUpdate} doUpdate
171 update idletasks
174 # The procedure below is invoked to update the scales from the
175 # current red, green, and blue intensities. It's invoked after
176 # a change in the color space and after a named color value has
177 # been loaded.
179 proc tc_setScales {} {
180 global red green blue colorSpace updating
181 set updating 1
182 switch $colorSpace {
183 rgb {
184 .scale1 set [format %.0f [expr {$red/65.535}]]
185 .scale2 set [format %.0f [expr {$green/65.535}]]
186 .scale3 set [format %.0f [expr {$blue/65.535}]]
188 cmy {
189 .scale1 set [format %.0f [expr {(65535-$red)/65.535}]]
190 .scale2 set [format %.0f [expr {(65535-$green)/65.535}]]
191 .scale3 set [format %.0f [expr {(65535-$blue)/65.535}]]
193 hsb {
194 set list [rgbToHsv $red $green $blue]
195 .scale1 set [format %.0f [expr {[lindex $list 0] * 1000.0}]]
196 .scale2 set [format %.0f [expr {[lindex $list 1] * 1000.0}]]
197 .scale3 set [format %.0f [expr {[lindex $list 2] * 1000.0}]]
200 set updating 0
203 # The procedure below is invoked when a named color has been
204 # selected from the listbox or typed into the entry. It loads
205 # the color into the editor.
207 proc tc_loadNamedColor name {
208 global red green blue color autoUpdate
210 if {[string index $name 0] != "#"} {
211 set list [winfo rgb .sample.swatch $name]
212 set red [lindex $list 0]
213 set green [lindex $list 1]
214 set blue [lindex $list 2]
215 } else {
216 switch [string length $name] {
217 4 {set format "#%1x%1x%1x"; set shift 12}
218 7 {set format "#%2x%2x%2x"; set shift 8}
219 10 {set format "#%3x%3x%3x"; set shift 4}
220 13 {set format "#%4x%4x%4x"; set shift 0}
221 default {error "syntax error in color name \"$name\""}
223 if {[scan $name $format red green blue] != 3} {
224 error "syntax error in color name \"$name\""
226 set red [expr {$red<<$shift}]
227 set green [expr {$green<<$shift}]
228 set blue [expr {$blue<<$shift}]
230 tc_setScales
231 set color [format "#%04x%04x%04x" $red $green $blue]
232 .sample.swatch config -bg $color
233 if {$autoUpdate} doUpdate
236 # The procedure below is invoked when a new color space is selected.
237 # It changes the labels on the scales and re-loads the scales with
238 # the appropriate values for the current color in the new color space
240 proc changeColorSpace space {
241 global label1 label2 label3
242 switch $space {
243 rgb {
244 set label1 "Adjust Red:"
245 set label2 "Adjust Green:"
246 set label3 "Adjust Blue:"
247 tc_setScales
248 return
250 cmy {
251 set label1 "Adjust Cyan:"
252 set label2 "Adjust Magenta:"
253 set label3 "Adjust Yellow:"
254 tc_setScales
255 return
257 hsb {
258 set label1 "Adjust Hue:"
259 set label2 "Adjust Saturation:"
260 set label3 "Adjust Brightness:"
261 tc_setScales
262 return
267 # The procedure below converts an RGB value to HSB. It takes red, green,
268 # and blue components (0-65535) as arguments, and returns a list containing
269 # HSB components (floating-point, 0-1) as result. The code here is a copy
270 # of the code on page 615 of "Fundamentals of Interactive Computer Graphics"
271 # by Foley and Van Dam.
273 proc rgbToHsv {red green blue} {
274 if {$red > $green} {
275 set max [expr {double($red)}]
276 set min [expr {double($green)}]
277 } else {
278 set max [expr {double($green)}]
279 set min [expr {double($red)}]
281 if {$blue > $max} {
282 set max [expr {double($blue)}]
283 } elseif {$blue < $min} {
284 set min [expr {double($blue)}]
286 set range [expr {$max-$min}]
287 if {$max == 0} {
288 set sat 0
289 } else {
290 set sat [expr {($max-$min)/$max}]
292 if {$sat == 0} {
293 set hue 0
294 } else {
295 set rc [expr {($max - $red)/$range}]
296 set gc [expr {($max - $green)/$range}]
297 set bc [expr {($max - $blue)/$range}]
298 if {$red == $max} {
299 set hue [expr {($bc - $gc)/6.0}]
300 } elseif {$green == $max} {
301 set hue [expr {(2 + $rc - $bc)/6.0}]
302 } else {
303 set hue [expr {(4 + $gc - $rc)/6.0}]
305 if {$hue < 0.0} {
306 set hue [expr {$hue + 1.0}]
309 return [list $hue $sat [expr {$max/65535}]]
312 # The procedure below converts an HSB value to RGB. It takes hue, saturation,
313 # and value components (floating-point, 0-1.0) as arguments, and returns a
314 # list containing RGB components (integers, 0-65535) as result. The code
315 # here is a copy of the code on page 616 of "Fundamentals of Interactive
316 # Computer Graphics" by Foley and Van Dam.
318 proc hsbToRgb {hue sat value} {
319 set v [format %.0f [expr {65535.0*$value}]]
320 if {$sat == 0} {
321 return "$v $v $v"
322 } else {
323 set hue [expr {$hue*6.0}]
324 if {$hue >= 6.0} {
325 set hue 0.0
327 scan $hue. %d i
328 set f [expr {$hue-$i}]
329 set p [format %.0f [expr {65535.0*$value*(1 - $sat)}]]
330 set q [format %.0f [expr {65535.0*$value*(1 - ($sat*$f))}]]
331 set t [format %.0f [expr {65535.0*$value*(1 - ($sat*(1 - $f)))}]]
332 switch $i {
333 0 {return "$v $t $p"}
334 1 {return "$q $v $p"}
335 2 {return "$p $v $t"}
336 3 {return "$p $q $v"}
337 4 {return "$t $p $v"}
338 5 {return "$v $p $q"}
339 default {error "i value $i is out of range"}
344 # The procedure below is invoked when the "Update" button is pressed,
345 # and whenever the color changes if update mode is enabled. It
346 # propagates color information as determined by the command in the
347 # Command entry.
349 proc doUpdate {} {
350 global color command
351 set newCmd $command
352 regsub -all %% $command $color newCmd
353 eval $newCmd
356 changeColorSpace hsb
358 # Local Variables:
359 # mode: tcl
360 # End: