summaryrefslogtreecommitdiff
path: root/graphics/pgf/base/tex/pgflibraryshadings.code.tex
blob: 81e0386a60563ae3a2852617c0fbf738333fdad4 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
% Copyright 2019 by Till Tantau and others
% CMYK and grayscale shadings adaptation copyright 2019 by David Purton
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS{pgflibraryshadings.code.tex}


%
% An hsv color wheel. Initial code graciously donated by Ken Starks.
%

\pgfdeclarefunctionalshading[black]{color wheel white center}
{\pgfpoint{-50bp}{-50bp}}
{\pgfpoint{50bp}{50bp}}
{}
{ % x y
  2 copy % ... x y x y
  2 copy abs exch abs add 0.0001 ge
  {atan 360.0 div} % ... x y heading;  heading being in
                   %the interval [0, 1.0]
  { pop } % silently deal with error: return
                  % arbitrary heading of zero for origin
  ifelse  % because we will use it for 'Hue'
  3 1 roll % ... heading x y
  dup mul % ... heading x y*y
  exch dup mul % ... heading y*y x*x
  add sqrt % ... heading ra_pt (distance from origin in points)
  25.0 div % scale it means a ra of 25bp
  dup 1.0 ge % BOOLEAN. ready to clamp to interval [0, 1.0]
  { pop 1.0 }{} ifelse % We shall use the scaled ra as 'Saturation'
  %2.5 mul 0.25 sub % now, Ra in [0.1, 0.5] --> Saturation
                   % in [0.0, 1.0]. Saturation varies between the two radii
  1.0 % ... H S V ( with 'Value' set to literal constant of 1.0 )
  % C version to use as model:
  % H' = H * 6
  % i = floor(H')
  % f = H' - i
  % P = V * (1.0 - S)
  % Q = V * (1.0 - (S*f))
  % T = V * (1.0 - (S * (1.0 - f)))
  3 2 roll 6.0 mul dup 4 1 roll % H' S V H'
  floor cvr  % H' S V i
  dup 5 1 roll % i H' S V i
  3 index sub neg % i H' S V f
  1.0 3 index sub % i H' S V f (1.0 - S )
  2 index mul % i H' S V f P
  6 1 roll % P i H' S V f
  dup 3 index mul neg 1.0 add % P i H' S V f ( 1.0 - (f*S))
  2 index mul % P i H' S V f Q
  7 1 roll % Q P i H' S V f
  neg 1.0 add % Q P i H' S V (1.0 - f)
  2 index mul neg 1.0  add % Q P i H' S V (1.0 - S * (1.0 - f))
  1 index mul % Q P i H' S V T
  7 2 roll % V T Q P i H' S
  pop pop % V T Q P i
  %%%
  % end of BLOCK B. The rest is just stack manipulation
  dup 0.5 le % TEST II [ i == 0 ]
  { % BLOCK C [ take stack to V T P ]
    pop exch pop
  }
  { dup 1.5 le % TEST III [ i == 1 ]
    { % BLOCK D [ take stack to Q V P ]
      pop exch 4 1 roll exch pop
    }
    { dup 2.5 le % TEST IV [ i == 2 ]
      { % BLOCK E [ take stack to P V T ]
        pop 4 1 roll pop
      }
      { dup 3.5 le % TEST V [ i == 3 ]
        { % BLOCK F [ take stack to P Q V ]
          pop exch 4 2 roll pop
        }
        { dup 4.5 le % TEST VI [ i == 4 ]
          { % BLOCK G [ take stack to T P V ]
            pop exch pop 3 -1 roll
          }
          { % BLOCK H [ take stack to V P Q ]
            pop 3 1 roll exch pop
          }
          ifelse
        }
        ifelse % for V
      }
      ifelse % for IV
    }
    ifelse % for III
  }
  ifelse % for II
  \ifpgfshadingmodelcmyk
    \pgffuncshadingrgbtocmyk
  \fi
  \ifpgfshadingmodelgray
    \pgffuncshadingrgbtogray
  \fi
}%

\pgfdeclarefunctionalshading[black]{color wheel black center}
{\pgfpoint{-50bp}{-50bp}}
{\pgfpoint{50bp}{50bp}}
{}
{ % x y
  2 copy % ... x y x y
  2 copy abs exch abs add 0.0001 ge
  {atan 360.0 div} % ... x y heading;  heading being in
                   %the interval [0, 1.0]
  { pop } % silently deal with error: return
                  % arbitrary heading of zero for origin
  ifelse  % because we will use it for 'Hue'
  3 1 roll % ... heading x y
  dup mul % ... heading x y*y
  exch dup mul % ... heading y*y x*x
  add sqrt % ... heading ra_pt (distance from origin in points)
  25.0 div % scale it means a ra of 25bp
  dup 1.0 ge % BOOLEAN. ready to clamp to interval [0, 1.0]
  { pop 1.0 }{} ifelse % We shall use the scaled ra as 'Saturation'
  %2.5 mul 0.25 sub % now, Ra in [0.1, 0.5] --> Saturation
                   % in [0.0, 1.0]. Saturation varies between the two radii
  1.0 exch % ... H S V ( with 'Value' set to literal constant of 1.0 )
  % C version to use as model:
  % H' = H * 6
  % i = floor(H')
  % f = H' - i
  % P = V * (1.0 - S)
  % Q = V * (1.0 - (S*f))
  % T = V * (1.0 - (S * (1.0 - f)))
  3 2 roll 6.0 mul dup 4 1 roll % H' S V H'
  floor cvr  % H' S V i
  dup 5 1 roll % i H' S V i
  3 index sub neg % i H' S V f
  1.0 3 index sub % i H' S V f (1.0 - S )
  2 index mul % i H' S V f P
  6 1 roll % P i H' S V f
  dup 3 index mul neg 1.0 add % P i H' S V f ( 1.0 - (f*S))
  2 index mul % P i H' S V f Q
  7 1 roll % Q P i H' S V f
  neg 1.0 add % Q P i H' S V (1.0 - f)
  2 index mul neg 1.0  add % Q P i H' S V (1.0 - S * (1.0 - f))
  1 index mul % Q P i H' S V T
  7 2 roll % V T Q P i H' S
  pop pop % V T Q P i
  %%%
  % end of BLOCK B. The rest is just stack manipulation
  dup 0.5 le % TEST II [ i == 0 ]
  { % BLOCK C [ take stack to V T P ]
    pop exch pop
  }
  { dup 1.5 le % TEST III [ i == 1 ]
    { % BLOCK D [ take stack to Q V P ]
      pop exch 4 1 roll exch pop
    }
    { dup 2.5 le % TEST IV [ i == 2 ]
      { % BLOCK E [ take stack to P V T ]
        pop 4 1 roll pop
      }
      { dup 3.5 le % TEST V [ i == 3 ]
        { % BLOCK F [ take stack to P Q V ]
          pop exch 4 2 roll pop
        }
        { dup 4.5 le % TEST VI [ i == 4 ]
          { % BLOCK G [ take stack to T P V ]
            pop exch pop 3 -1 roll
          }
          { % BLOCK H [ take stack to V P Q ]
            pop 3 1 roll exch pop
          }
          ifelse
        }
        ifelse % for V
      }
      ifelse % for IV
    }
    ifelse % for III
  }
  ifelse % for II
  \ifpgfshadingmodelcmyk
    \pgffuncshadingrgbtocmyk
  \fi
  \ifpgfshadingmodelgray
    \pgffuncshadingrgbtogray
  \fi
}%

\pgfdeclarefunctionalshading[black]{color wheel}
{\pgfpoint{-50bp}{-50bp}}
{\pgfpoint{50bp}{50bp}}
{}
{ % x y
  2 copy abs exch abs add 0.0001 ge
  {atan 360.0 div} % ... x y heading;  heading being in
                   %the interval [0, 1.0]
  { pop } % silently deal with error: return
                  % arbitrary heading of zero for origin
  ifelse  % because we will use it for 'Hue'
  1.0 1.0 % ... H S V
  % C version to use as model:
  % H' = H * 6
  % i = floor(H')
  % f = H' - i
  % P = V * (1.0 - S)
  % Q = V * (1.0 - (S*f))
  % T = V * (1.0 - (S * (1.0 - f)))
  3 2 roll 6.0 mul dup 4 1 roll % H' S V H'
  floor cvr  % H' S V i
  dup 5 1 roll % i H' S V i
  3 index sub neg % i H' S V f
  1.0 3 index sub % i H' S V f (1.0 - S )
  2 index mul % i H' S V f P
  6 1 roll % P i H' S V f
  dup 3 index mul neg 1.0 add % P i H' S V f ( 1.0 - (f*S))
  2 index mul % P i H' S V f Q
  7 1 roll % Q P i H' S V f
  neg 1.0 add % Q P i H' S V (1.0 - f)
  2 index mul neg 1.0  add % Q P i H' S V (1.0 - S * (1.0 - f))
  1 index mul % Q P i H' S V T
  7 2 roll % V T Q P i H' S
  pop pop % V T Q P i
  %%%
  % end of BLOCK B. The rest is just stack manipulation
  dup 0.5 le % TEST II [ i == 0 ]
  { % BLOCK C [ take stack to V T P ]
    pop exch pop
  }
  { dup 1.5 le % TEST III [ i == 1 ]
    { % BLOCK D [ take stack to Q V P ]
      pop exch 4 1 roll exch pop
    }
    { dup 2.5 le % TEST IV [ i == 2 ]
      { % BLOCK E [ take stack to P V T ]
        pop 4 1 roll pop
      }
      { dup 3.5 le % TEST V [ i == 3 ]
        { % BLOCK F [ take stack to P Q V ]
          pop exch 4 2 roll pop
        }
        { dup 4.5 le % TEST VI [ i == 4 ]
          { % BLOCK G [ take stack to T P V ]
            pop exch pop 3 -1 roll
          }
          { % BLOCK H [ take stack to V P Q ]
            pop 3 1 roll exch pop
          }
          ifelse
        }
        ifelse % for V
      }
      ifelse % for IV
    }
    ifelse % for III
  }
  ifelse % for II
  \ifpgfshadingmodelcmyk
    \pgffuncshadingrgbtocmyk
  \fi
  \ifpgfshadingmodelgray
    \pgffuncshadingrgbtogray
  \fi
}%


%
% A bilinear interpolation.
%

\colorlet{lower left}{white}%
\colorlet{lower right}{white}%
\colorlet{upper left}{white}%
\colorlet{upper right}{white}%

\pgfdeclarefunctionalshading[lower left,lower right,upper left,upper right]{bilinear interpolation}
{\pgfpointorigin}
{\pgfpoint{100bp}{100bp}}
{
  \pgfshadecolortorgb{lower left}{\pgf@lib@shadings@ll}\pgfshadecolortorgb{lower right}{\pgf@lib@shadings@lr}
  \pgfshadecolortorgb{upper right}{\pgf@lib@shadings@ur}\pgfshadecolortorgb{upper left}{\pgf@lib@shadings@ul}
}{
  25 sub 50 div exch 25 sub 50 div 2 copy                   % Calculate y/100 x/100.
%  100 div exch 100 div 2 copy                   % Calculate y/100 x/100.
  neg 1 add exch neg 1 add                      % Calculate 1-y/100 1-x/100.
  3 1 roll 2 copy exch 5 2 roll 6 copy 6 copy   % Set up stack.
  \pgf@lib@shadings@llred mul exch \pgf@lib@shadings@lrred mul add mul     % Process red component.
  4 1 roll
  \pgf@lib@shadings@urred mul exch \pgf@lib@shadings@ulred mul add mul
  add
  13 1 roll
  \pgf@lib@shadings@llgreen mul exch \pgf@lib@shadings@lrgreen mul add mul % Process green component.
  4 1 roll
  \pgf@lib@shadings@urgreen mul exch \pgf@lib@shadings@ulgreen mul add mul
  add
  7 1 roll
  \pgf@lib@shadings@llblue mul exch \pgf@lib@shadings@lrblue mul add mul   % Process blue component.
  4 1 roll
  \pgf@lib@shadings@urblue mul exch \pgf@lib@shadings@ulblue mul add mul
  add
  \ifpgfshadingmodelcmyk
    \pgffuncshadingrgbtocmyk
  \fi
  \ifpgfshadingmodelgray
    \pgffuncshadingrgbtogray
  \fi
}%


%
% A Mandelbrot set shading. Just for fun...
%

\pgfdeclarefunctionalshading[black]{Mandelbrot set}
{\pgfpoint{-50bp}{-50bp}}
{\pgfpoint{50bp}{50bp}}{}
{
  12.5 div exch 12.5 div exch
  1 index 1 index
  % Stack: c_r c_i z_r z_i
  % Formula: z' = z^2 + c = (z_r + i z_i)^2 + c_r + i c_i
  %             = (z_r^2 - z_i^2 + c_r) + i (2 z_r z_i + c_i)
  %
  % First iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Second iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Third iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Fourth iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Check for break (to avoid too large numbers)
  1 index dup mul 1 index dup mul add
  4 lt {
  % Fifth iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Sixth iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Seventh iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Check for break (to avoid too large numbers)
  1 index dup mul 1 index dup mul add
  4 lt {
  % Eighth iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Ninth iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  % Tenth iteration
  % 1. Compute z_r^2 -z_i^2 + c_r
  1 index dup mul % z_r^2
  1 index dup mul % z_i^2
  sub             % z_r^2 - z_i^2
  4 index add     % z_r^2 -z_i^2 + c_r
  % 2. Compute 2 z_r z_i + c_i
  3 1 roll
  mul 2 mul       % 2 z_r z_i
  2 index add     % 2 z_r z_i + c_i
  } { pop pop 1000.0 1000.0 } ifelse
  } { pop pop 1000.0 1000.0 } ifelse
  % Compute distance
  dup mul exch
  dup mul
  add sqrt
  dup 4 1 roll
  2 gt { pop pop 2.0 exch div 1.0 exch sub dup dup} {pop pop pop 0.0 0.0 0.0} ifelse
  \ifpgfshadingmodelcmyk
    \pgffuncshadingrgbtocmyk
  \fi
  \ifpgfshadingmodelgray
    \pgffuncshadingrgbtogray
  \fi
}%