Debian Codegraph @

texmf-dist/tex/generic/pgf/libraries/pgflibraryshadings.code.tex

raw
1 % Copyright 2019 by Till Tantau and others
2 % CMYK and grayscale shadings adaptation copyright 2019 by David Purton
3 %
4 % This file may be distributed and/or modified
5 %
6 % 1. under the LaTeX Project Public License and/or
7 % 2. under the GNU Public License.
8 %
9 % See the file doc/generic/pgf/licenses/LICENSE for more details.
10
11 \ProvidesFileRCS{pgflibraryshadings.code.tex}
12
13
14 %
15 % An hsv color wheel. Initial code graciously donated by Ken Starks.
16 %
17
18 \pgfdeclarefunctionalshading[black]{color wheel white center}
19 {\pgfpoint{-50bp}{-50bp}}
20 {\pgfpoint{50bp}{50bp}}
21 {}
22 { % x y
23 2 copy % ... x y x y
24 2 copy abs exch abs add 0.0001 ge
25 {atan 360.0 div} % ... x y heading; heading being in
26 %the interval [0, 1.0]
27 { pop } % silently deal with error: return
28 % arbitrary heading of zero for origin
29 ifelse % because we will use it for 'Hue'
30 3 1 roll % ... heading x y
31 dup mul % ... heading x y*y
32 exch dup mul % ... heading y*y x*x
33 add sqrt % ... heading ra_pt (distance from origin in points)
34 25.0 div % scale it means a ra of 25bp
35 dup 1.0 ge % BOOLEAN. ready to clamp to interval [0, 1.0]
36 { pop 1.0 }{} ifelse % We shall use the scaled ra as 'Saturation'
37 %2.5 mul 0.25 sub % now, Ra in [0.1, 0.5] --> Saturation
38 % in [0.0, 1.0]. Saturation varies between the two radii
39 1.0 % ... H S V ( with 'Value' set to literal constant of 1.0 )
40 % C version to use as model:
41 % H' = H * 6
42 % i = floor(H')
43 % f = H' - i
44 % P = V * (1.0 - S)
45 % Q = V * (1.0 - (S*f))
46 % T = V * (1.0 - (S * (1.0 - f)))
47 3 2 roll 6.0 mul dup 4 1 roll % H' S V H'
48 floor cvr % H' S V i
49 dup 5 1 roll % i H' S V i
50 3 index sub neg % i H' S V f
51 1.0 3 index sub % i H' S V f (1.0 - S )
52 2 index mul % i H' S V f P
53 6 1 roll % P i H' S V f
54 dup 3 index mul neg 1.0 add % P i H' S V f ( 1.0 - (f*S))
55 2 index mul % P i H' S V f Q
56 7 1 roll % Q P i H' S V f
57 neg 1.0 add % Q P i H' S V (1.0 - f)
58 2 index mul neg 1.0 add % Q P i H' S V (1.0 - S * (1.0 - f))
59 1 index mul % Q P i H' S V T
60 7 2 roll % V T Q P i H' S
61 pop pop % V T Q P i
62 %%%
63 % end of BLOCK B. The rest is just stack manipulation
64 dup 0.5 le % TEST II [ i == 0 ]
65 { % BLOCK C [ take stack to V T P ]
66 pop exch pop
67 }
68 { dup 1.5 le % TEST III [ i == 1 ]
69 { % BLOCK D [ take stack to Q V P ]
70 pop exch 4 1 roll exch pop
71 }
72 { dup 2.5 le % TEST IV [ i == 2 ]
73 { % BLOCK E [ take stack to P V T ]
74 pop 4 1 roll pop
75 }
76 { dup 3.5 le % TEST V [ i == 3 ]
77 { % BLOCK F [ take stack to P Q V ]
78 pop exch 4 2 roll pop
79 }
80 { dup 4.5 le % TEST VI [ i == 4 ]
81 { % BLOCK G [ take stack to T P V ]
82 pop exch pop 3 -1 roll
83 }
84 { % BLOCK H [ take stack to V P Q ]
85 pop 3 1 roll exch pop
86 }
87 ifelse
88 }
89 ifelse % for V
90 }
91 ifelse % for IV
92 }
93 ifelse % for III
94 }
95 ifelse % for II
96 \ifpgfshadingmodelcmyk
97 \pgffuncshadingrgbtocmyk
98 \fi
99 \ifpgfshadingmodelgray
100 \pgffuncshadingrgbtogray
101 \fi
102 }%
103
104 \pgfdeclarefunctionalshading[black]{color wheel black center}
105 {\pgfpoint{-50bp}{-50bp}}
106 {\pgfpoint{50bp}{50bp}}
107 {}
108 { % x y
109 2 copy % ... x y x y
110 2 copy abs exch abs add 0.0001 ge
111 {atan 360.0 div} % ... x y heading; heading being in
112 %the interval [0, 1.0]
113 { pop } % silently deal with error: return
114 % arbitrary heading of zero for origin
115 ifelse % because we will use it for 'Hue'
116 3 1 roll % ... heading x y
117 dup mul % ... heading x y*y
118 exch dup mul % ... heading y*y x*x
119 add sqrt % ... heading ra_pt (distance from origin in points)
120 25.0 div % scale it means a ra of 25bp
121 dup 1.0 ge % BOOLEAN. ready to clamp to interval [0, 1.0]
122 { pop 1.0 }{} ifelse % We shall use the scaled ra as 'Saturation'
123 %2.5 mul 0.25 sub % now, Ra in [0.1, 0.5] --> Saturation
124 % in [0.0, 1.0]. Saturation varies between the two radii
125 1.0 exch % ... H S V ( with 'Value' set to literal constant of 1.0 )
126 % C version to use as model:
127 % H' = H * 6
128 % i = floor(H')
129 % f = H' - i
130 % P = V * (1.0 - S)
131 % Q = V * (1.0 - (S*f))
132 % T = V * (1.0 - (S * (1.0 - f)))
133 3 2 roll 6.0 mul dup 4 1 roll % H' S V H'
134 floor cvr % H' S V i
135 dup 5 1 roll % i H' S V i
136 3 index sub neg % i H' S V f
137 1.0 3 index sub % i H' S V f (1.0 - S )
138 2 index mul % i H' S V f P
139 6 1 roll % P i H' S V f
140 dup 3 index mul neg 1.0 add % P i H' S V f ( 1.0 - (f*S))
141 2 index mul % P i H' S V f Q
142 7 1 roll % Q P i H' S V f
143 neg 1.0 add % Q P i H' S V (1.0 - f)
144 2 index mul neg 1.0 add % Q P i H' S V (1.0 - S * (1.0 - f))
145 1 index mul % Q P i H' S V T
146 7 2 roll % V T Q P i H' S
147 pop pop % V T Q P i
148 %%%
149 % end of BLOCK B. The rest is just stack manipulation
150 dup 0.5 le % TEST II [ i == 0 ]
151 { % BLOCK C [ take stack to V T P ]
152 pop exch pop
153 }
154 { dup 1.5 le % TEST III [ i == 1 ]
155 { % BLOCK D [ take stack to Q V P ]
156 pop exch 4 1 roll exch pop
157 }
158 { dup 2.5 le % TEST IV [ i == 2 ]
159 { % BLOCK E [ take stack to P V T ]
160 pop 4 1 roll pop
161 }
162 { dup 3.5 le % TEST V [ i == 3 ]
163 { % BLOCK F [ take stack to P Q V ]
164 pop exch 4 2 roll pop
165 }
166 { dup 4.5 le % TEST VI [ i == 4 ]
167 { % BLOCK G [ take stack to T P V ]
168 pop exch pop 3 -1 roll
169 }
170 { % BLOCK H [ take stack to V P Q ]
171 pop 3 1 roll exch pop
172 }
173 ifelse
174 }
175 ifelse % for V
176 }
177 ifelse % for IV
178 }
179 ifelse % for III
180 }
181 ifelse % for II
182 \ifpgfshadingmodelcmyk
183 \pgffuncshadingrgbtocmyk
184 \fi
185 \ifpgfshadingmodelgray
186 \pgffuncshadingrgbtogray
187 \fi
188 }%
189
190 \pgfdeclarefunctionalshading[black]{color wheel}
191 {\pgfpoint{-50bp}{-50bp}}
192 {\pgfpoint{50bp}{50bp}}
193 {}
194 { % x y
195 2 copy abs exch abs add 0.0001 ge
196 {atan 360.0 div} % ... x y heading; heading being in
197 %the interval [0, 1.0]
198 { pop } % silently deal with error: return
199 % arbitrary heading of zero for origin
200 ifelse % because we will use it for 'Hue'
201 1.0 1.0 % ... H S V
202 % C version to use as model:
203 % H' = H * 6
204 % i = floor(H')
205 % f = H' - i
206 % P = V * (1.0 - S)
207 % Q = V * (1.0 - (S*f))
208 % T = V * (1.0 - (S * (1.0 - f)))
209 3 2 roll 6.0 mul dup 4 1 roll % H' S V H'
210 floor cvr % H' S V i
211 dup 5 1 roll % i H' S V i
212 3 index sub neg % i H' S V f
213 1.0 3 index sub % i H' S V f (1.0 - S )
214 2 index mul % i H' S V f P
215 6 1 roll % P i H' S V f
216 dup 3 index mul neg 1.0 add % P i H' S V f ( 1.0 - (f*S))
217 2 index mul % P i H' S V f Q
218 7 1 roll % Q P i H' S V f
219 neg 1.0 add % Q P i H' S V (1.0 - f)
220 2 index mul neg 1.0 add % Q P i H' S V (1.0 - S * (1.0 - f))
221 1 index mul % Q P i H' S V T
222 7 2 roll % V T Q P i H' S
223 pop pop % V T Q P i
224 %%%
225 % end of BLOCK B. The rest is just stack manipulation
226 dup 0.5 le % TEST II [ i == 0 ]
227 { % BLOCK C [ take stack to V T P ]
228 pop exch pop
229 }
230 { dup 1.5 le % TEST III [ i == 1 ]
231 { % BLOCK D [ take stack to Q V P ]
232 pop exch 4 1 roll exch pop
233 }
234 { dup 2.5 le % TEST IV [ i == 2 ]
235 { % BLOCK E [ take stack to P V T ]
236 pop 4 1 roll pop
237 }
238 { dup 3.5 le % TEST V [ i == 3 ]
239 { % BLOCK F [ take stack to P Q V ]
240 pop exch 4 2 roll pop
241 }
242 { dup 4.5 le % TEST VI [ i == 4 ]
243 { % BLOCK G [ take stack to T P V ]
244 pop exch pop 3 -1 roll
245 }
246 { % BLOCK H [ take stack to V P Q ]
247 pop 3 1 roll exch pop
248 }
249 ifelse
250 }
251 ifelse % for V
252 }
253 ifelse % for IV
254 }
255 ifelse % for III
256 }
257 ifelse % for II
258 \ifpgfshadingmodelcmyk
259 \pgffuncshadingrgbtocmyk
260 \fi
261 \ifpgfshadingmodelgray
262 \pgffuncshadingrgbtogray
263 \fi
264 }%
265
266
267 %
268 % A bilinear interpolation.
269 %
270
271 \colorlet{lower left}{white}%
272 \colorlet{lower right}{white}%
273 \colorlet{upper left}{white}%
274 \colorlet{upper right}{white}%
275
276 \pgfdeclarefunctionalshading[lower left,lower right,upper left,upper right]{bilinear interpolation}
277 {\pgfpointorigin}
278 {\pgfpoint{100bp}{100bp}}
279 {
280 \pgfshadecolortorgb{lower left}{\pgf@lib@shadings@ll}\pgfshadecolortorgb{lower right}{\pgf@lib@shadings@lr}
281 \pgfshadecolortorgb{upper right}{\pgf@lib@shadings@ur}\pgfshadecolortorgb{upper left}{\pgf@lib@shadings@ul}
282 }{
283 25 sub 50 div exch 25 sub 50 div 2 copy % Calculate y/100 x/100.
284 % 100 div exch 100 div 2 copy % Calculate y/100 x/100.
285 neg 1 add exch neg 1 add % Calculate 1-y/100 1-x/100.
286 3 1 roll 2 copy exch 5 2 roll 6 copy 6 copy % Set up stack.
287 \pgf@lib@shadings@llred mul exch \pgf@lib@shadings@lrred mul add mul % Process red component.
288 4 1 roll
289 \pgf@lib@shadings@urred mul exch \pgf@lib@shadings@ulred mul add mul
290 add
291 13 1 roll
292 \pgf@lib@shadings@llgreen mul exch \pgf@lib@shadings@lrgreen mul add mul % Process green component.
293 4 1 roll
294 \pgf@lib@shadings@urgreen mul exch \pgf@lib@shadings@ulgreen mul add mul
295 add
296 7 1 roll
297 \pgf@lib@shadings@llblue mul exch \pgf@lib@shadings@lrblue mul add mul % Process blue component.
298 4 1 roll
299 \pgf@lib@shadings@urblue mul exch \pgf@lib@shadings@ulblue mul add mul
300 add
301 \ifpgfshadingmodelcmyk
302 \pgffuncshadingrgbtocmyk
303 \fi
304 \ifpgfshadingmodelgray
305 \pgffuncshadingrgbtogray
306 \fi
307 }%
308
309
310 %
311 % A Mandelbrot set shading. Just for fun...
312 %
313
314 \pgfdeclarefunctionalshading[black]{Mandelbrot set}
315 {\pgfpoint{-50bp}{-50bp}}
316 {\pgfpoint{50bp}{50bp}}{}
317 {
318 12.5 div exch 12.5 div exch
319 1 index 1 index
320 % Stack: c_r c_i z_r z_i
321 % Formula: z' = z^2 + c = (z_r + i z_i)^2 + c_r + i c_i
322 % = (z_r^2 - z_i^2 + c_r) + i (2 z_r z_i + c_i)
323 %
324 % First iteration
325 % 1. Compute z_r^2 -z_i^2 + c_r
326 1 index dup mul % z_r^2
327 1 index dup mul % z_i^2
328 sub % z_r^2 - z_i^2
329 4 index add % z_r^2 -z_i^2 + c_r
330 % 2. Compute 2 z_r z_i + c_i
331 3 1 roll
332 mul 2 mul % 2 z_r z_i
333 2 index add % 2 z_r z_i + c_i
334 % Second iteration
335 % 1. Compute z_r^2 -z_i^2 + c_r
336 1 index dup mul % z_r^2
337 1 index dup mul % z_i^2
338 sub % z_r^2 - z_i^2
339 4 index add % z_r^2 -z_i^2 + c_r
340 % 2. Compute 2 z_r z_i + c_i
341 3 1 roll
342 mul 2 mul % 2 z_r z_i
343 2 index add % 2 z_r z_i + c_i
344 % Third iteration
345 % 1. Compute z_r^2 -z_i^2 + c_r
346 1 index dup mul % z_r^2
347 1 index dup mul % z_i^2
348 sub % z_r^2 - z_i^2
349 4 index add % z_r^2 -z_i^2 + c_r
350 % 2. Compute 2 z_r z_i + c_i
351 3 1 roll
352 mul 2 mul % 2 z_r z_i
353 2 index add % 2 z_r z_i + c_i
354 % Fourth iteration
355 % 1. Compute z_r^2 -z_i^2 + c_r
356 1 index dup mul % z_r^2
357 1 index dup mul % z_i^2
358 sub % z_r^2 - z_i^2
359 4 index add % z_r^2 -z_i^2 + c_r
360 % 2. Compute 2 z_r z_i + c_i
361 3 1 roll
362 mul 2 mul % 2 z_r z_i
363 2 index add % 2 z_r z_i + c_i
364 % Check for break (to avoid too large numbers)
365 1 index dup mul 1 index dup mul add
366 4 lt {
367 % Fifth iteration
368 % 1. Compute z_r^2 -z_i^2 + c_r
369 1 index dup mul % z_r^2
370 1 index dup mul % z_i^2
371 sub % z_r^2 - z_i^2
372 4 index add % z_r^2 -z_i^2 + c_r
373 % 2. Compute 2 z_r z_i + c_i
374 3 1 roll
375 mul 2 mul % 2 z_r z_i
376 2 index add % 2 z_r z_i + c_i
377 % Sixth iteration
378 % 1. Compute z_r^2 -z_i^2 + c_r
379 1 index dup mul % z_r^2
380 1 index dup mul % z_i^2
381 sub % z_r^2 - z_i^2
382 4 index add % z_r^2 -z_i^2 + c_r
383 % 2. Compute 2 z_r z_i + c_i
384 3 1 roll
385 mul 2 mul % 2 z_r z_i
386 2 index add % 2 z_r z_i + c_i
387 % Seventh iteration
388 % 1. Compute z_r^2 -z_i^2 + c_r
389 1 index dup mul % z_r^2
390 1 index dup mul % z_i^2
391 sub % z_r^2 - z_i^2
392 4 index add % z_r^2 -z_i^2 + c_r
393 % 2. Compute 2 z_r z_i + c_i
394 3 1 roll
395 mul 2 mul % 2 z_r z_i
396 2 index add % 2 z_r z_i + c_i
397 % Check for break (to avoid too large numbers)
398 1 index dup mul 1 index dup mul add
399 4 lt {
400 % Eighth iteration
401 % 1. Compute z_r^2 -z_i^2 + c_r
402 1 index dup mul % z_r^2
403 1 index dup mul % z_i^2
404 sub % z_r^2 - z_i^2
405 4 index add % z_r^2 -z_i^2 + c_r
406 % 2. Compute 2 z_r z_i + c_i
407 3 1 roll
408 mul 2 mul % 2 z_r z_i
409 2 index add % 2 z_r z_i + c_i
410 % Ninth iteration
411 % 1. Compute z_r^2 -z_i^2 + c_r
412 1 index dup mul % z_r^2
413 1 index dup mul % z_i^2
414 sub % z_r^2 - z_i^2
415 4 index add % z_r^2 -z_i^2 + c_r
416 % 2. Compute 2 z_r z_i + c_i
417 3 1 roll
418 mul 2 mul % 2 z_r z_i
419 2 index add % 2 z_r z_i + c_i
420 % Tenth iteration
421 % 1. Compute z_r^2 -z_i^2 + c_r
422 1 index dup mul % z_r^2
423 1 index dup mul % z_i^2
424 sub % z_r^2 - z_i^2
425 4 index add % z_r^2 -z_i^2 + c_r
426 % 2. Compute 2 z_r z_i + c_i
427 3 1 roll
428 mul 2 mul % 2 z_r z_i
429 2 index add % 2 z_r z_i + c_i
430 } { pop pop 1000.0 1000.0 } ifelse
431 } { pop pop 1000.0 1000.0 } ifelse
432 % Compute distance
433 dup mul exch
434 dup mul
435 add sqrt
436 dup 4 1 roll
437 2 gt { pop pop 2.0 exch div 1.0 exch sub dup dup} {pop pop pop 0.0 0.0 0.0} ifelse
438 \ifpgfshadingmodelcmyk
439 \pgffuncshadingrgbtocmyk
440 \fi
441 \ifpgfshadingmodelgray
442 \pgffuncshadingrgbtogray
443 \fi
444 }%