1 //
2 // Alpha color values demonstration.
3 //
4 // Copyright (C) 2008 Hazen Babcock
5 //
6 //
7 // This file is part of PLplot.
8 //
9 // PLplot is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Library General Public License as published
11 // by the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // PLplot is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU Library General Public License for more details.
18 //
19 // You should have received a copy of the GNU Library General Public License
20 // along with PLplot; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // This example will only really be interesting when used with devices that
24 // support or alpha (or transparency) values, such as the cairo device family.
25 //
26
27
28 #include "plcdemos.h"
29 static PLINT red[] = { 0, 255, 0, 0 };
30 static PLINT green[] = { 0, 0, 255, 0 };
31 static PLINT blue[] = { 0, 0, 0, 255 };
32 static PLFLT alpha[] = { 1.0, 1.0, 1.0, 1.0 };
33
34 static PLFLT px[] = { 0.1, 0.5, 0.5, 0.1 };
35 static PLFLT py[] = { 0.1, 0.1, 0.5, 0.5 };
36
37 static PLFLT pos[] = { 0.0, 1.0 };
38 static PLFLT rcoord[] = { 1.0, 1.0 };
39 static PLFLT gcoord[] = { 0.0, 0.0 };
40 static PLFLT bcoord[] = { 0.0, 0.0 };
41 static PLFLT acoord[] = { 0.0, 1.0 };
42 static PLBOOL rev[] = { 0, 0 };
43
44 int
main(int argc,char * argv[])45 main( int argc, char *argv[] )
46 {
47 int i, j;
48 PLINT icol, r, g, b;
49 PLFLT a;
50
51 plparseopts( &argc, argv, PL_PARSE_FULL );
52
53 plinit();
54 plscmap0n( 4 );
55 plscmap0a( red, green, blue, alpha, 4 );
56
57 //
58 // Page 1:
59 //
60 // This is a series of red, green and blue rectangles overlaid
61 // on each other with gradually increasing transparency.
62 //
63
64 // Set up the window
65 pladv( 0 );
66 plvpor( 0.0, 1.0, 0.0, 1.0 );
67 plwind( 0.0, 1.0, 0.0, 1.0 );
68 plcol0( 0 );
69 plbox( "", 1.0, 0, "", 1.0, 0 );
70
71 // Draw the boxes
72 for ( i = 0; i < 9; i++ )
73 {
74 icol = i % 3 + 1;
75
76 // Get a color, change its transparency and
77 // set it as the current color.
78 plgcol0a( icol, &r, &g, &b, &a );
79 plscol0a( icol, r, g, b, 1.0 - (PLFLT) i / 9.0 );
80 plcol0( icol );
81
82 // Draw the rectangle
83 plfill( 4, px, py );
84
85 // Shift the rectangles coordinates
86 for ( j = 0; j < 4; j++ )
87 {
88 px[j] += 0.5 / 9.0;
89 py[j] += 0.5 / 9.0;
90 }
91 }
92
93 //
94 // Page 2:
95 //
96 // This is a bunch of boxes colored red, green or blue with a single
97 // large (red) box of linearly varying transparency overlaid. The
98 // overlaid box is completely transparent at the bottom and completely
99 // opaque at the top.
100 //
101
102 // Set up the window
103 pladv( 0 );
104 plvpor( 0.1, 0.9, 0.1, 0.9 );
105 plwind( 0.0, 1.0, 0.0, 1.0 );
106
107 // Draw the boxes. There are 25 of them drawn on a 5 x 5 grid.
108 for ( i = 0; i < 5; i++ )
109 {
110 // Set box X position
111 px[0] = 0.05 + 0.2 * i;
112 px[1] = px[0] + 0.1;
113 px[2] = px[1];
114 px[3] = px[0];
115
116 // We don't want the boxes to be transparent, so since we changed
117 // the colors transparencies in the first example we have to change
118 // the transparencies back to completely opaque.
119 icol = i % 3 + 1;
120 plgcol0a( icol, &r, &g, &b, &a );
121 plscol0a( icol, r, g, b, 1.0 );
122 plcol0( icol );
123 for ( j = 0; j < 5; j++ )
124 {
125 // Set box y position and draw the box.
126 py[0] = 0.05 + 0.2 * j;
127 py[1] = py[0];
128 py[2] = py[0] + 0.1;
129 py[3] = py[2];
130 plfill( 4, px, py );
131 }
132 }
133
134 // Create the color map with 128 colors and use plscmap1la to initialize
135 // the color values with a linearly varying red transparency (or alpha)
136 plscmap1n( 128 );
137 plscmap1la( 1, 2, pos, rcoord, gcoord, bcoord, acoord, rev );
138
139 // Use that cmap1 to create a transparent red gradient for the whole
140 // window.
141 px[0] = 0.;
142 px[1] = 1.;
143 px[2] = 1.;
144 px[3] = 0.;
145
146 py[0] = 0.;
147 py[1] = 0.;
148 py[2] = 1.;
149 py[3] = 1.;
150
151 plgradient( 4, px, py, 90. );
152
153 plend();
154 exit( 0 );
155 }
156