/
ex02.p6
executable file
·133 lines (107 loc) · 3.11 KB
/
ex02.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env perl6
#
# Multiple window and color map 0 demo.
# Original C code is found at http://plplot.sourceforge.net/examples.php?demo=02
#
# Demonstrates multiple windows and color map 0 palette, both default and
# user-modified.
#
use v6;
use lib 'lib';
use Graphics::PLplot;
sub MAIN {
if Graphics::PLplot.new(device => 'wxwidgets') -> $plot {
# Initialize plplot
$plot.begin;
# Run demos
demo1($plot);
demo2($plot);
LEAVE {
# Cleanup
$plot.end;
}
}
}
#
# Draws a set of numbered boxes with colors according to cmap0 entry.
#
sub draw-windows($plot, Int $nw, Int $cmap0-offset)
{
$plot.character-size(
default => 0.0,
scale => 3.5
);
# Script font
$plot.font(4);
for 0..^$nw -> $i {
$plot.color-index0($i + $cmap0-offset);
my $text = sprintf("%d", $i);
$plot.subpage(0);
my ($vmin, $vmax) = (0.1, 0.9);
for 0..2 -> $j {
$plot.pen-width($j + 1);
$plot.subpage-viewport($vmin, $vmax, $vmin, $vmax);
$plot.window(0.0, 1.0, 0.0, 1.0);
$plot.box("bc", 0.0, 0, "bc", 0.0, 0);
$vmin += 0.1;
$vmax -= 0.1;
}
$plot.pen-width(1);
$plot.text(
point => (0.5, 0.5),
inclination => (1.0, 0.0),
just => 0.5,
text => $text
);
}
}
#
# Demonstrates multiple windows and default color map 0 palette.
#
sub demo1($plot) {
$plot.new-page;
# Divide screen into 16 regions
$plot.number-of-subpages(4, 4);
draw-windows($plot, 16, 0);
$plot.clear-or-eject-page;
}
#
# Demonstrates multiple windows, user-modified color map 0 palette, and HLS ->
# RGB translation.
#
sub demo2($plot) {
# Set up cmap0 using 100 custom colors in addition to base 16 (116 total)
my @rgb;
# Min & max lightness values
my ($lmin, $lmax) = (0.15, 0.85);
$plot.new-page;
# Divide screen into 100 regions
$plot.number-of-subpages(10, 10);
for 0..99 -> $i {
#
# Bounds on HLS:
# hue [0.0, 360.0] degrees
# lightness [0.0, 1.0] magnitude
# saturation [0.0, 1.0] magnitude
#
# Vary hue uniformly from left to right
my $h = ((360.0 / 10.0) * ($i % 10));
# Vary lightness uniformly from top to bottom, between min & max
my $l = ($lmin + ($lmax - $lmin) * ($i / 10) / 9.0);
# Use max saturation
my $s = 1.0;
# Convert from HLS to RGB
my ($red, $green, $blue) = $plot.hls-to-rgb($h, $l, $s);
# Use 255.001 to avoid close truncation decisions in this example.
@rgb[$i + 16] = ($red * 255.001).Int, ($green * 255.001).Int,
($blue * 255.001).Int;
}
# Load default cmap0 colors into our custom set
for 0..15 -> $i {
@rgb[$i] = $plot.color-index0-rgb($i);
}
# Now set cmap0 all at once (faster, since fewer driver calls)
$plot.set-cmap0-rgb-colors(@rgb);
draw-windows($plot, 100, 16);
$plot.clear-or-eject-page;
}