2 # the next line restarts using wish \
3 exec wish85
"$0" ${1+"$@"}
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 package require Tk
8.4
11 wm title .
"Color Editor"
13 # Global variables that control the program:
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:
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.
38 set color
#ffff00000000
43 # Create the menu bar at the top of the window.
45 . configure
-menu [menu .menu
]
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 \
57 .menu.
file add radio
-label "Manual updates" -variable autoUpdate \
59 .menu.
file add separator
60 .menu.
file add
command -label "Exit program" -underline 0 -command {exit}
62 # Create the command entry window at the bottom of the window, along
63 # with the update button.
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
72 # Create the listbox that holds all of the color names in rgb.txt,
73 # if an rgb.txt file can be found.
75 grid .
command -sticky nsew
-row 2 -columnspan 3 -padx 1m
-pady {0 1m
}
77 grid columnconfigure .
{1 2} -weight 1
78 grid rowconfigure .
0 -weight 1
80 /usr
/local
/lib
/X11
/rgb.txt
/usr
/lib
/X11
/rgb.txt
81 /X11
/R
5/lib
/X11
/rgb.txt
/X11
/R
4/lib
/rgb
/rgb.txt
82 /usr
/openwin
/lib
/X11
/rgb.txt
84 if {![file readable
$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
]]
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
107 # Create the three scales for editing the color, and the entry for
108 # typing in a color value.
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
119 grid .adjust
-row 0 -column 1 -sticky nsew
-padx .15c
-pady .15c
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
127 # Create the color display swatch on the right side of the window.
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
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.
141 proc tc_scaleChanged args
{
142 global red green blue colorSpace color updating autoUpdate
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}]]
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}]]
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]
166 set color
[format
"#%04x%04x%04x" $red $green $blue]
167 .sample.swatch config
-bg $color
168 if {$autoUpdate} doUpdate
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
177 proc tc_setScales
{} {
178 global red green blue colorSpace updating
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}]]
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}]]
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}]]
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.
205 proc tc_loadNamedColor name
{
206 global red green blue color autoUpdate
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]
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\""}
221 if {[scan
$name $format red green blue
] != 3} {
222 error
"syntax error in color name \"$name\""
224 set red
[expr {$red<<$shift}]
225 set green
[expr {$green<<$shift}]
226 set blue
[expr {$blue<<$shift}]
229 set color
[format
"#%04x%04x%04x" $red $green $blue]
230 .sample.swatch config
-bg $color
231 if {$autoUpdate} doUpdate
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
238 proc changeColorSpace space
{
239 global label1 label2 label3
242 set label1
"Adjust Red:"
243 set label2
"Adjust Green:"
244 set label3
"Adjust Blue:"
249 set label1
"Adjust Cyan:"
250 set label2
"Adjust Magenta:"
251 set label3
"Adjust Yellow:"
256 set label1
"Adjust Hue:"
257 set label2
"Adjust Saturation:"
258 set label3
"Adjust Brightness:"
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.
271 proc rgbToHsv
{red green blue
} {
273 set max
[expr {double
($red)}]
274 set min
[expr {double
($green)}]
276 set max
[expr {double
($green)}]
277 set min
[expr {double
($red)}]
280 set max
[expr {double
($blue)}]
281 } elseif
{$blue < $min} {
282 set min
[expr {double
($blue)}]
284 set range
[expr {$max-$min}]
288 set sat
[expr {($max-$min)/$max}]
293 set rc
[expr {($max - $red)/$range}]
294 set gc
[expr {($max - $green)/$range}]
295 set bc [expr {($max - $blue)/$range}]
297 set hue
[expr {($bc - $gc)/6.0}]
298 } elseif
{$green == $max} {
299 set hue
[expr {(2 + $rc - $bc)/6.0}]
301 set hue
[expr {(4 + $gc - $rc)/6.0}]
304 set hue
[expr {$hue + 1.0}]
307 return [list
$hue $sat [expr {$max/65535}]]
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.
316 proc hsbToRgb
{hue sat value
} {
317 set v
[format
%.0f
[expr {65535.0*$value}]]
321 set hue
[expr {$hue*6.0}]
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)))}]]
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"}
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
350 regsub
-all %% $command $color newCmd