OSDN Git Service

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