Changeset 1075 for trunk/examples/canvas


Ignore:
Timestamp:
Jul 18, 2008 11:57:18 AM (12 years ago)
Author:
mmh
Message:

Rewritten color range code for integers and numbers.
examples/canvas rewritten.
examples/zoo/[integer|number]2 added

Location:
trunk/examples/canvas
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/examples/canvas/tool.xml

    r797 r1075  
    1717      <label>Wavelength</label>
    1818      <description>Enter a wavelength of light in the visible spectrum.</description>
     19      <color>380 380nm 750 750nm</color>
    1920    </about>
    2021    <units>nm</units>
     
    2728    </preset>
    2829    <preset>
    29       <value>685nm</value>
     30      <value>532nm</value>
    3031      <label>532nm (green)</label>
    3132    </preset>
  • trunk/examples/canvas/waveln.tcl

    r797 r1075  
    1111package require Tk
    1212package require Img
     13
     14# ---------------------------------------------------------------------
     15# USAGE: wave2RGB <wavelength>
     16#
     17# Given a visible wavelength in nm, returns a Tk color of the form
     18# #RRGGBB. Returns black for nonvisible wavelengths.  Based on code from
     19# Dan Bruton (astro@tamu.edu) http://www.physics.sfasu.edu/astro/color/spectra.html
     20# ----------------------------------------------------------------------
     21
     22proc wave2RGB {wl} {
     23    if {$wl < 380 || $wl > 780} {
     24        return black
     25    }
     26    set gamma 0.8
     27    set r 0.0
     28    set g 0.0
     29    set b 0.0
     30    if {$wl <= 440} {
     31        set r [expr (440.0 - $wl) / 60.0]
     32        set b 1.0
     33    } elseif {$wl <= 490} {
     34        set g [expr ($wl - 440.0) / 50.0]
     35        set b 1.0
     36    } elseif {$wl <= 510} {
     37        set g 1.0
     38        set b [expr (510.0 - $wl) / 20.0]
     39    } elseif {$wl <= 580} {
     40        set g 1.0
     41        set r [expr ($wl - 510.0) / 70.0]
     42    } elseif {$wl <= 645} {
     43        set r 1.0
     44        set g [expr (645.0 - $wl) / 65.0]
     45    } else {
     46        set r 1.0
     47    }
     48
     49    if {$wl > 700} {
     50        set sss [expr 0.3 + 0.7 * (780.0 - $wl) / 80.0]
     51    } elseif {$wl < 420} {
     52        set sss [expr 0.3 + 0.7 * ($wl - 380.0) / 40.0]
     53    } else {
     54        set sss 1.0
     55    }
     56    set r [expr int(255.0 * pow(($sss * $r), $gamma))]
     57    set g [expr int(255.0 * pow(($sss * $g), $gamma))]
     58    set b [expr int(255.0 * pow(($sss * $b), $gamma))]
     59    return [format "#%.2X%.2X%.2X" $r $g $b]
     60}
     61
     62
    1363wm 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 }
    5364
    5465# open the XML file containing the run parameters
     
    5970set tooldir [file dirname [info script]]
    6071set psfile "plot[pid].ps"
    61 set jpgfile "plot[pid].jpg"
     72set pngfile "plot[pid].png"
    6273
    63 canvas .c
    6474
    65 #
    66 # Create the diagram on the canvas.
    67 #
    68 set x0 20
    69 set x1 280
     75# height and width of spectrum image
     76# It will get scaled to fit window, but render at high
     77# resolution so it looks good when large.
     78set width 720
     79set height 50
     80
     81# load the hand image
     82set imh [image create photo -file [file join $tooldir hand.gif]]
     83
     84# Where to place the spectrum image. Leave room for the hand above it.
     85set x0 [image width $imh]
     86set y0 [image height $imh]
     87
     88# create canvas big enough to hold images
     89canvas .c -height [expr $height + $y0 + 100] -width [expr $width + $x0 + 50]
     90
     91# spectrum range
     92set start 380
     93set stop 740
     94
     95# calculate some offsets
     96set x1 [expr $x0 + $width]
     97set y1 [expr $y0 + $height]
    7098set xmid [expr {0.5*($x0+$x1)}]
    71 set nx [expr {[llength $spectrum]/2}]
    72 set dx [expr {($x1-$x0)/double($nx)}]
     99set dwl [expr ($stop - $start)/$width.0]
    73100
    74 set y0 35
    75 set y1 65
    76 
     101# draw spectrum image
    77102set 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}]
     103for {set wl $start} {$wl < $stop} {set wl [expr $wl + $dwl]} {
     104    set color [wave2RGB $wl]
     105    .c create rectangle $x $y0 [expr $x+1] $y1 -outline "" -fill $color
     106    incr x
    81107}
    82108.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"
     109.c create text $xmid [expr {$y1+20}] -anchor n -text "Visible Spectrum" -font "Helvetica 18"
    85110
    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]]
     111# now place the hand image
     112set x [expr {($x1-$x0)/double($stop-$start) * ($wavel-380) + $x0}]
    89113.c create image $x $y0 -image $imh
    90 .c create text $x [expr {$y0-15}] \
     114.c create text $x [expr {$y0-25}] \
    91115    -anchor s -text "$wavel nm" -font "Helvetica 14"
    92116
     
    94118# Convert the PostScript from the canvas into an image.
    95119#
    96 set psdata [.c postscript -width 300 -height 100]
     120set psdata [.c postscript -height [expr $height + $y0 + 100] -width [expr $width + $x0 + 50]]
    97121set fid [open $psfile w]
    98122puts $fid $psdata
    99123close $fid
    100124
    101 set status [catch {exec convert $psfile $jpgfile} result]
     125# convert postscript to png image
     126set status [catch {exec convert $psfile $pngfile} result]
    102127
    103128if {$status == 0} {
    104     set dest [image create photo -format jpeg -file $jpgfile]
    105     $xml put output.image(diagram).current [$dest data -format jpeg]
     129    set dest [image create photo -format png -file $pngfile]
     130    $xml put output.image(diagram).current [$dest data -format png]
    106131    $xml put output.image(diagram).about.label "Visible Spectrum"
    107     file delete -force $psfile $jpgfile
     132    file delete -force $psfile $pngfile
    108133} else {
    109134    puts stderr "ERROR during postscript conversion:\n$result"
    110     file delete -force $psfile $jpgfile
     135    file delete -force $psfile $pngfile
    111136    exit 1
    112137}
Note: See TracChangeset for help on using the changeset viewer.