source: trunk/examples/canvas/waveln.tcl @ 797

Last change on this file since 797 was 797, checked in by mmc, 17 years ago

Fixed the canvas example so that it can be launched with tclsh and load
Tk later as a package.

File size: 2.8 KB
Line 
1# ----------------------------------------------------------------------
2#  EXAMPLE: Rappture <number> elements
3# ======================================================================
4#  AUTHOR:  Michael McLennan, Purdue University
5#  Copyright (c) 2004-2005  Purdue Research Foundation
6#
7#  See the file "license.terms" for information on usage and
8#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9# ======================================================================
10package require Rappture
11package require Tk
12package require Img
13wm withdraw .
14
15set spectrum {
16    380 #ff00ff
17    390 #e300ff
18    400 #c700ff
19    410 #ab00ff
20    420 #9000ff
21    430 #7400ff
22    440 #5800ff
23    450 #3c00ff
24    460 #2100ff
25    470 #0500ff
26    480 #0023db
27    490 #004faf
28    500 #007b83
29    510 #00a757
30    520 #00d32b
31    530 #00ff00
32    540 #33ff00
33    550 #66ff00
34    560 #99ff00
35    570 #ccff00
36    580 #ffff00
37    590 #ffe500
38    600 #ffcc00
39    610 #ffb200
40    620 #ff9900
41    630 #ff7f00
42    640 #ff6600
43    650 #ff4c00
44    660 #ff3200
45    670 #ff1900
46    680 #ff0000
47    690 #e50000
48    700 #cc0000
49    710 #b20000
50    720 #990000
51    730 #7f0000
52}
53
54# open the XML file containing the run parameters
55set xml [Rappture::library [lindex $argv 0]]
56set wavel [$xml get input.number(wavel).current]
57set wavel [Rappture::Units::convert $wavel -to nm -units off]
58
59set tooldir [file dirname [info script]]
60set psfile "plot[pid].ps"
61set jpgfile "plot[pid].jpg"
62
63canvas .c
64
65#
66# Create the diagram on the canvas.
67#
68set x0 20
69set x1 280
70set xmid [expr {0.5*($x0+$x1)}]
71set nx [expr {[llength $spectrum]/2}]
72set dx [expr {($x1-$x0)/double($nx)}]
73
74set y0 35
75set y1 65
76
77set x $x0
78foreach {wl color} $spectrum {
79    .c create rectangle $x $y0 [expr {$x+$dx}] $y1 -outline "" -fill $color
80    set x [expr {$x+$dx}]
81}
82.c create rectangle $x0 $y0 $x1 $y1 -outline black -fill ""
83.c create text $xmid [expr {$y1+6}] \
84    -anchor n -text "Visible Spectrum" -font "Helvetica 18"
85
86set x [expr {($x1-$x0)/double(740-380) * ($wavel-380) + $x0}]
87
88set imh [image create photo -file [file join $tooldir hand.gif]]
89.c create image $x $y0 -image $imh
90.c create text $x [expr {$y0-15}] \
91    -anchor s -text "$wavel nm" -font "Helvetica 14"
92
93#
94# Convert the PostScript from the canvas into an image.
95#
96set psdata [.c postscript -width 300 -height 100]
97set fid [open $psfile w]
98puts $fid $psdata
99close $fid
100
101set status [catch {exec convert $psfile $jpgfile} result]
102
103if {$status == 0} {
104    set dest [image create photo -format jpeg -file $jpgfile]
105    $xml put output.image(diagram).current [$dest data -format jpeg]
106    $xml put output.image(diagram).about.label "Visible Spectrum"
107    file delete -force $psfile $jpgfile
108} else {
109    puts stderr "ERROR during postscript conversion:\n$result"
110    file delete -force $psfile $jpgfile
111    exit 1
112}
113
114# save the updated XML describing the run...
115Rappture::result $xml
116exit 0
Note: See TracBrowser for help on using the repository browser.