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 | # ====================================================================== |
---|
10 | package require Rappture |
---|
11 | package require Tk |
---|
12 | package require Img |
---|
13 | wm withdraw . |
---|
14 | |
---|
15 | set 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 |
---|
55 | set xml [Rappture::library [lindex $argv 0]] |
---|
56 | set wavel [$xml get input.number(wavel).current] |
---|
57 | set wavel [Rappture::Units::convert $wavel -to nm -units off] |
---|
58 | |
---|
59 | set tooldir [file dirname [info script]] |
---|
60 | set psfile "plot[pid].ps" |
---|
61 | set jpgfile "plot[pid].jpg" |
---|
62 | |
---|
63 | canvas .c |
---|
64 | |
---|
65 | # |
---|
66 | # Create the diagram on the canvas. |
---|
67 | # |
---|
68 | set x0 20 |
---|
69 | set x1 280 |
---|
70 | set xmid [expr {0.5*($x0+$x1)}] |
---|
71 | set nx [expr {[llength $spectrum]/2}] |
---|
72 | set dx [expr {($x1-$x0)/double($nx)}] |
---|
73 | |
---|
74 | set y0 35 |
---|
75 | set y1 65 |
---|
76 | |
---|
77 | set x $x0 |
---|
78 | foreach {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 | |
---|
86 | set x [expr {($x1-$x0)/double(740-380) * ($wavel-380) + $x0}] |
---|
87 | |
---|
88 | set 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 | # |
---|
96 | set psdata [.c postscript -width 300 -height 100] |
---|
97 | set fid [open $psfile w] |
---|
98 | puts $fid $psdata |
---|
99 | close $fid |
---|
100 | |
---|
101 | set status [catch {exec convert $psfile $jpgfile} result] |
---|
102 | |
---|
103 | if {$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... |
---|
115 | Rappture::result $xml |
---|
116 | exit 0 |
---|