3
|
1 |
#!/usr/bin/wish
|
0
|
2 |
|
1
|
3 |
set wheight 400
|
|
4 |
set wwidth 800
|
|
5 |
set wwidthsingle [expr $wwidth / 3 ]
|
2
|
6 |
set gradient 10
|
|
7 |
# Precision on color selection
|
|
8 |
set th_prec 2000
|
1
|
9 |
|
|
10 |
# Aquest -2 és perque els butons, per alguna raó, són 2 pixels més alts i
|
|
11 |
# amples.
|
|
12 |
set wheightsingle [expr $wheight / $gradient - 2]
|
|
13 |
|
|
14 |
proc stepvalue { min max step } {
|
|
15 |
global gradient
|
|
16 |
return [expr $min + ( ( $max - $min ) * $step / ($gradient - 1) )]
|
|
17 |
}
|
|
18 |
|
|
19 |
proc color { r g b } {
|
|
20 |
return [format "#%04x%04x%04x" $r $g $b]
|
|
21 |
}
|
|
22 |
|
|
23 |
# Build the labels
|
2
|
24 |
frame .c
|
|
25 |
pack .c -side bottom
|
1
|
26 |
|
|
27 |
# Left
|
0
|
28 |
frame .leftcol
|
1
|
29 |
label .leftcol.c -height $wheight -width $wwidthsingle -bg #12f \
|
|
30 |
-bitmap @gray50.xbm -borderwidth 0
|
0
|
31 |
pack .leftcol.c
|
1
|
32 |
|
|
33 |
# Center
|
0
|
34 |
frame .centercol
|
1
|
35 |
for {set i 0} {$i < $gradient} {incr i} {
|
|
36 |
set r [stepvalue 0 65535 $i]
|
|
37 |
set g $r
|
|
38 |
set b $r
|
|
39 |
button .centercol.b$i -height $wheightsingle -width $wwidthsingle \
|
|
40 |
-bg [color $r $g $b] -bitmap gray50 -borderwidth 0 \
|
|
41 |
-highlightthickness 0 -command "selection $i"
|
|
42 |
pack .centercol.b$i -side top
|
|
43 |
}
|
|
44 |
|
|
45 |
# Right
|
0
|
46 |
frame .rightcol
|
1
|
47 |
label .rightcol.c -height $wheight -width $wwidthsingle -bg #f12 \
|
|
48 |
-bitmap @gray50.xbm -borderwidth 0
|
0
|
49 |
pack .rightcol.c
|
|
50 |
|
|
51 |
pack .leftcol -side left
|
|
52 |
pack .centercol -side left
|
|
53 |
pack .rightcol -side left
|
|
54 |
|
2
|
55 |
# Menu
|
|
56 |
menu .mb
|
|
57 |
. config -menu .mb
|
|
58 |
set mFile [menu .mb.mFile]
|
|
59 |
.mb add cascade -label File -menu .mb.mFile
|
1
|
60 |
|
3
|
61 |
$mFile add command -label New -command new_points
|
2
|
62 |
$mFile add command -label Load -command load_file
|
|
63 |
$mFile add command -label Save -command save_file
|
|
64 |
$mFile add separator
|
|
65 |
$mFile add cascade -label Export -menu ${mFile}.mExport
|
|
66 |
$mFile add command -label Quit -command exit
|
|
67 |
set mExport [menu ${mFile}.mExport]
|
|
68 |
$mExport add command -label "GNUPlot data" -command export_gnuplotdata
|
|
69 |
$mExport add command -label ICC -command export_icc
|
|
70 |
|
|
71 |
# Globals
|
|
72 |
set rpoints {{0 0.} {65535 1.}}
|
|
73 |
set gpoints {{0 0.} {65535 1.}}
|
|
74 |
set bpoints {{0 0.} {65535 1.}}
|
1
|
75 |
|
|
76 |
# Loop functions
|
|
77 |
|
2
|
78 |
# Returns { type }
|
|
79 |
proc get_next_ptype { } {
|
|
80 |
global rpoints gpoints bpoints
|
|
81 |
|
|
82 |
set nrp [llength $rpoints]
|
|
83 |
set ngp [llength $gpoints]
|
|
84 |
set nbp [llength $bpoints]
|
|
85 |
|
|
86 |
puts "nrp: $nrp"
|
|
87 |
puts "ngp: $ngp"
|
|
88 |
puts "nbp: $nbp"
|
|
89 |
|
|
90 |
if { $nrp > $ngp } {
|
|
91 |
set type g
|
|
92 |
} else {
|
|
93 |
set type r
|
|
94 |
}
|
|
95 |
|
|
96 |
if { [set n${type}p] > $nbp } {
|
|
97 |
set type b
|
|
98 |
}
|
|
99 |
|
|
100 |
return $type
|
|
101 |
}
|
1
|
102 |
|
2
|
103 |
proc get_next_point { type } {
|
|
104 |
global ${type}points
|
|
105 |
upvar 0 ${type}points points
|
|
106 |
|
|
107 |
set np [llength $points]
|
|
108 |
|
|
109 |
set last [lindex [lindex $points 0] 0 ]
|
|
110 |
|
|
111 |
set maxdist 0
|
|
112 |
|
|
113 |
set imin 0
|
|
114 |
|
|
115 |
for {set i 1} {$i < $np} {incr i 1} {
|
|
116 |
set actual [lindex [lindex $points $i] 0]
|
|
117 |
set dist [expr {abs($last - $actual)}]
|
|
118 |
puts "dist: $dist"
|
|
119 |
if { $dist > $maxdist } {
|
|
120 |
set imin [expr {$i - 1}]
|
|
121 |
puts "New imin"
|
|
122 |
set maxdist $dist
|
|
123 |
}
|
|
124 |
set last $actual
|
|
125 |
}
|
|
126 |
return $imin
|
1
|
127 |
}
|
|
128 |
|
2
|
129 |
proc prepare_colors_newpoint { } {
|
|
130 |
global icolors rpoints gpoints bpoints gradient type xmin xmax nxmin nxmax
|
|
131 |
|
|
132 |
set type [get_next_ptype]
|
|
133 |
set icolors [get_next_point $type]
|
|
134 |
upvar 0 ${type}points points
|
|
135 |
|
|
136 |
puts "type: $type"
|
|
137 |
puts "icolors: $icolors"
|
|
138 |
|
|
139 |
set xmin [lindex [lindex $points $icolors] 0]
|
|
140 |
set xmax [lindex [lindex $points [expr {$icolors + 1}]] 0]
|
|
141 |
set nxmin $xmin
|
|
142 |
set nxmax $xmax
|
|
143 |
prepare_colors $type $nxmin $nxmax
|
|
144 |
}
|
|
145 |
|
|
146 |
proc prepare_colors { type nxmin nxmax } {
|
|
147 |
global gradient
|
|
148 |
|
|
149 |
switch -- $type {
|
|
150 |
r {
|
|
151 |
set cmin [color $nxmin 0 0]
|
|
152 |
set cmax [color $nxmax 0 0]
|
|
153 |
} g {
|
|
154 |
set cmin [color 0 $nxmin 0]
|
|
155 |
set cmax [color 0 $nxmax 0]
|
|
156 |
}
|
|
157 |
b {
|
|
158 |
set cmin [color 0 0 $nxmin]
|
|
159 |
set cmax [color 0 0 $nxmax]
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
# Set the bg and fg color to xmin and xmax
|
|
164 |
.leftcol.c configure -background $cmin -foreground $cmax
|
|
165 |
.rightcol.c configure -background $cmin -foreground $cmax
|
|
166 |
|
|
167 |
# Prepare the colors of the middle buttons
|
|
168 |
for {set i 0} {$i < $gradient} {incr i} {
|
|
169 |
set x [stepvalue $nxmin $nxmax $i]
|
|
170 |
switch -- $type {
|
|
171 |
r { set c [color $x 0 0] }
|
|
172 |
g { set c [color 0 $x 0] }
|
|
173 |
b { set c [color 0 0 $x] }
|
|
174 |
}
|
|
175 |
.centercol.b$i configure -background $c -foreground $c
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
proc padd { point } {
|
1
|
180 |
}
|
|
181 |
|
|
182 |
proc selection { i } {
|
2
|
183 |
global rpoints gpoints bpoints nxmin nxmax gradient type th_prec
|
|
184 |
|
|
185 |
upvar 0 ${type}points points
|
|
186 |
|
|
187 |
set nstep_min [expr {$i - 1}]
|
|
188 |
set nstep_max [expr {$i + 1}]
|
|
189 |
if {$nstep_min < 0} { set nstep_min 0 }
|
|
190 |
if {$nstep_max >= $gradient} { set nstep_max [expr {$gradient - 1}] }
|
|
191 |
set nvalue_min [stepvalue $nxmin $nxmax $nstep_min]
|
|
192 |
set nvalue_max [stepvalue $nxmin $nxmax $nstep_max]
|
|
193 |
puts "$type - nvalue_min: $nvalue_min, nvalue_max: $nvalue_max"
|
|
194 |
|
|
195 |
set nxmin $nvalue_min
|
|
196 |
set nxmax $nvalue_max
|
|
197 |
|
|
198 |
if {[expr {$nxmax - $nxmin}] > $th_prec} {
|
|
199 |
puts "$type - More precision needed"
|
|
200 |
prepare_colors $type $nxmin $nxmax
|
|
201 |
} else {
|
|
202 |
|
|
203 |
puts "$type - New point!"
|
|
204 |
# Add the point - middle range
|
|
205 |
set middle [expr {$nxmin + ($nxmax - $nxmin)/2}]
|
|
206 |
set i 0
|
|
207 |
while { [lindex [lindex $points $i] 0] < $middle } {
|
|
208 |
incr i 1
|
|
209 |
}
|
|
210 |
# Get the middle of the light intesity
|
|
211 |
set vmin [lindex [lindex $points [expr {$i - 1}]] 1]
|
|
212 |
set vmax [lindex [lindex $points $i] 1]
|
|
213 |
set nval [expr {$vmin + ($vmax - $vmin)/2.0}]
|
|
214 |
set points [linsert $points $i [list $middle $nval]]
|
|
215 |
puts "$type - $points"
|
|
216 |
|
|
217 |
prepare_colors_newpoint
|
|
218 |
}
|
1
|
219 |
}
|
|
220 |
|
2
|
221 |
proc save_file { } {
|
|
222 |
global rpoints gpoints bpoints
|
|
223 |
|
|
224 |
set file [tk_getSaveFile]
|
|
225 |
if { $file == "" } {
|
|
226 |
return
|
|
227 |
}
|
|
228 |
set fd [open $file w]
|
3
|
229 |
puts $fd "# plot for gnuplot"
|
|
230 |
puts $fd "# plot 'data' index 0 title 'vermell', 'data' index 1 title 'verd', 'data' index 2 title 'blau'"
|
2
|
231 |
|
|
232 |
foreach t { rpoints gpoints bpoints } {
|
3
|
233 |
puts $fd "# $t"
|
2
|
234 |
foreach i [set $t] {
|
|
235 |
puts -nonewline $fd [join $i " "]
|
|
236 |
puts $fd ""
|
|
237 |
}
|
|
238 |
if { $t != "bpoints"} {
|
|
239 |
puts $fd ""
|
3
|
240 |
puts $fd ""
|
2
|
241 |
}
|
|
242 |
}
|
|
243 |
|
|
244 |
close $fd
|
|
245 |
|
|
246 |
}
|
|
247 |
|
|
248 |
proc load_file { } {
|
|
249 |
global rpoints gpoints bpoints
|
|
250 |
|
|
251 |
set file [tk_getOpenFile]
|
|
252 |
if { $file == "" } {
|
|
253 |
return
|
|
254 |
}
|
|
255 |
set fd [open $file r]
|
|
256 |
|
|
257 |
set rpoints {}
|
|
258 |
set gpoints {}
|
|
259 |
set bpoints {}
|
|
260 |
|
|
261 |
set type r
|
|
262 |
while { [eof $fd] != 1 } {
|
|
263 |
set line [gets $fd]
|
|
264 |
if { $line == "" } {
|
|
265 |
set line [gets $fd]
|
|
266 |
if { $line == "" } {
|
|
267 |
switch -- $type {
|
|
268 |
r { set type g }
|
|
269 |
g { set type b }
|
|
270 |
}
|
|
271 |
}
|
|
272 |
} else {
|
3
|
273 |
if { [string index $line 0] != "#" } {
|
2
|
274 |
lappend ${type}points [split $line " "]
|
|
275 |
}
|
|
276 |
}
|
|
277 |
}
|
|
278 |
|
|
279 |
close $fd
|
|
280 |
# Debug
|
|
281 |
set nrp [llength $rpoints]
|
|
282 |
set ngp [llength $gpoints]
|
|
283 |
set nbp [llength $bpoints]
|
|
284 |
puts "Read nrp: $nrp, ngp: $ngp, nbp: $nbp"
|
|
285 |
|
|
286 |
prepare_colors_newpoint
|
|
287 |
}
|
|
288 |
|
3
|
289 |
proc new_points { } {
|
|
290 |
global rpoints gpoints bpoints
|
|
291 |
|
|
292 |
set rpoints {{0 0.} {65535 1.}}
|
|
293 |
set gpoints {{0 0.} {65535 1.}}
|
|
294 |
set bpoints {{0 0.} {65535 1.}}
|
|
295 |
|
|
296 |
prepare_colors_newpoint
|
|
297 |
}
|
|
298 |
|
|
299 |
|
2
|
300 |
# Prepare the colors for the user, for the first time!
|
|
301 |
prepare_colors_newpoint
|