source: trunk/gui/scripts/spectrum.tcl @ 11

Last change on this file since 11 was 11, checked in by mmc, 19 years ago

Major reorganization of the entire package. The config.xml file
is now irrelevant. All the action is in the tool.xml file. The
main program now organizes all input into 1) side-by-side pages,
2) input/result (wizard-style) pages, or 3) a series of wizard-
style pages. The <input> can have <phase> parts representing
the various pages.

Added a new ContourResult? widget based on Swaroop's vtk plotting
code.

Also, added easymesh and showmesh to the "tools" directory.
We need these for Eric Polizzi's code.

File size: 7.9 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: spectrum - maps a range of real values onto a color
3#
4#  This data object represents the mapping of a range of real values
5#  onto a range of colors.  It is used in conjunction with other
6#  widgets, such as the Rappture::Gauge.
7#
8#  EXAMPLE:
9#    Rappture::Spectrum #auto {
10#       0.0   red
11#       1.0   green
12#      10.0   #d9d9d9
13#    }
14# ======================================================================
15#  AUTHOR:  Michael McLennan, Purdue University
16#  Copyright (c) 2004-2005
17#  Purdue Research Foundation, West Lafayette, IN
18# ======================================================================
19package require Itk
20
21itcl::class Rappture::Spectrum {
22    inherit Rappture::Dispatcher
23
24    public variable units ""  ;# default units for all real values
25
26    constructor {{sdata ""} args} { # defined below }
27
28    public method insert {args}
29    public method delete {first {last ""}}
30    public method get {args}
31
32    private variable _axis ""       ;# list of real values along axis
33    private variable _rvals ""      ;# list of red components along axis
34    private variable _gvals ""      ;# list of green components along axis
35    private variable _bvals ""      ;# list of blue components along axis
36}
37                                                                               
38# ----------------------------------------------------------------------
39# CONSTRUCTOR
40# ----------------------------------------------------------------------
41itcl::body Rappture::Spectrum::constructor {{sdata ""} args} {
42    register !change  ;# used to signal changes in spectrum
43
44    if {[llength $sdata] > 0} {
45        regsub -all {\n} $sdata { } sdata
46        eval insert $sdata
47    }
48    eval configure $args
49}
50
51# ----------------------------------------------------------------------
52# USAGE: insert ?<value1> <color1> <value2> <color2> ...?
53#
54# Clients use this to insert one or more values into the spectrum.
55# Each value has an associated color.  These values are used in the
56# "get" method to map any incoming value to its interpolated color
57# in the spectrum.
58# ----------------------------------------------------------------------
59itcl::body Rappture::Spectrum::insert {args} {
60    set changed 0
61    foreach {value color} $args {
62        if {"" != $units} {
63            set value [Rappture::Units::convert $value \
64                -context $units -to $units -units off]
65        }
66        foreach {r g b} [winfo rgb . $color] { break }
67
68        set i 0
69        foreach v $_axis {
70            if {$value == $v} {
71                set _rvals [lreplace $_rvals $i $i $r]
72                set _gvals [lreplace $_gvals $i $i $g]
73                set _bvals [lreplace $_bvals $i $i $b]
74                set changed 1
75                break
76            } elseif {$value < $v} {
77                set _axis  [linsert $_axis $i $value]
78                set _rvals [linsert $_rvals $i $r]
79                set _gvals [linsert $_gvals $i $g]
80                set _bvals [linsert $_bvals $i $b]
81                set changed 1
82                break
83            }
84            incr i
85        }
86
87        if {$i >= [llength $_axis]} {
88            lappend _axis $value
89            lappend _rvals $r
90            lappend _gvals $g
91            lappend _bvals $b
92            set changed 1
93        }
94    }
95
96    # let any clients know if something has changed
97    if {$changed} {
98        event !change
99    }
100}
101
102# ----------------------------------------------------------------------
103# USAGE: delete <first> ?<last>?
104#
105# Clients use this to delete one or more entries from the spectrum.
106# The <first> and <last> represent the integer index of the desired
107# element.  If only <first> is specified, then that one element is
108# deleted.  If <last> is specified, then all elements in the range
109# <first> to <last> are deleted.
110# ----------------------------------------------------------------------
111itcl::body Rappture::Spectrum::delete {first {last ""}} {
112    if {$last == ""} {
113        set last $first
114    }
115    if {![regexp {^[0-9]+|end$} $first]} {
116        error "bad index \"$first\": should be integer or \"end\""
117    }
118    if {![regexp {^[0-9]+|end$} $last]} {
119        error "bad index \"$last\": should be integer or \"end\""
120    }
121
122    if {[llength [lrange $_axis $first $last]] > 0} {
123        set _axis [lreplace $_axis $first $last]
124        set _rvals [lreplace $_rvals $first $last]
125        set _gvals [lreplace $_gvals $first $last]
126        set _bvals [lreplace $_bvals $first $last]
127        event !change
128    }
129}
130
131# ----------------------------------------------------------------------
132# USAGE: get ?-color|-fraction? ?<value>?
133#
134# Clients use this to get information about the spectrum.  With no args,
135# it returns a list of elements in the form accepted by the "insert"
136# method.  Otherwise, it returns the interpolated value for the given
137# <value>.  By default, it returns the interpolated color, but the
138# -fraction flag can be specified to query the fractional position
139# along the spectrum.
140# ----------------------------------------------------------------------
141itcl::body Rappture::Spectrum::get {args} {
142    if {[llength $args] == 0} {
143        set rlist ""
144        foreach v $_axis r $_rvals g $_gvals b $_bvals {
145            lappend rlist "$v$units" [format {#%.4x%.4x%.4x} $r $g $b]
146        }
147        return $rlist
148    }
149
150    set what "-color"
151    while {[llength $args] > 0} {
152        set first [lindex $args 0]
153        if {[regexp {^-[a-zA-Z]} $first]} {
154            set what $first
155            set args [lrange $args 1 end]
156        } else {
157            break
158        }
159    }
160    if {[llength $args] != 1} {
161        error "wrong # args: should be \"get ?-color|-fraction? ?value?\""
162    }
163
164    set value [lindex $args 0]
165    if {$units != ""} {
166        set value [Rappture::Units::convert $value \
167            -context $units -to $units -units off]
168    }
169
170    switch -- $what {
171        -color {
172            set i 0
173            set ilast ""
174            while {$i <= [llength $_axis]} {
175                set v [lindex $_axis $i]
176
177                if {$v == ""} {
178                    set r [lindex $_rvals $ilast]
179                    set g [lindex $_gvals $ilast]
180                    set b [lindex $_bvals $ilast]
181                    return [format {#%.4x%.4x%.4x} $r $g $b]
182                } elseif {$value < $v} {
183                    if {$ilast == ""} {
184                        set r [lindex $_rvals $i]
185                        set g [lindex $_gvals $i]
186                        set b [lindex $_bvals $i]
187                    } else {
188                        set vlast [lindex $_axis $ilast]
189                        set frac [expr {($value-$vlast)/double($v-$vlast)}]
190
191                        set rlast [lindex $_rvals $ilast]
192                        set r [lindex $_rvals $i]
193                        set r [expr {round($frac*($r-$rlast) + $rlast)}]
194
195                        set glast [lindex $_gvals $ilast]
196                        set g [lindex $_gvals $i]
197                        set g [expr {round($frac*($g-$glast) + $glast)}]
198
199                        set blast [lindex $_bvals $ilast]
200                        set b [lindex $_bvals $i]
201                        set b [expr {round($frac*($b-$blast) + $blast)}]
202                    }
203                    return [format {#%.4x%.4x%.4x} $r $g $b]
204                }
205                set ilast $i
206                incr i
207            }
208        }
209        -fraction {
210            set v0 [lindex $_axis 0]
211            set vend [lindex $_axis end]
212            return [expr {($value-$v0)/double($vend-$v0)}]
213        }
214        default {
215            error "bad flag \"$what\": should be -color or -fraction"
216        }
217    }
218}
219
220# ----------------------------------------------------------------------
221# CONFIGURATION OPTIONS: -units
222# ----------------------------------------------------------------------
223itcl::configbody Rappture::Spectrum::units {
224    if {"" != $units && [Rappture::Units::System::for $units] == ""} {
225        error "bad value \"$units\": should be system of units"
226    }
227    event !change
228}
Note: See TracBrowser for help on using the repository browser.