summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/asymptote/plain_pens.asy
blob: 2e3a4449a38b39cf2bc096254a50eb14b2725961 (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
real labelmargin=0.3;
real dotfactor=6;

pen solid=linetype(new real[]);
pen dotted=linetype(new real[] {0,4});
pen dashed=linetype(new real[] {8,8});
pen longdashed=linetype(new real[] {24,8});
pen dashdotted=linetype(new real[] {8,8,0,8});
pen longdashdotted=linetype(new real[] {24,8,0,8});

pen linetype(string pattern, real offset=0, bool scale=true, bool adjust=true)
{
  return linetype((real[]) split(pattern),offset,scale,adjust);
}

void defaultpen(real w) {defaultpen(linewidth(w));}
pen operator +(pen p, real w) {return p+linewidth(w);}
pen operator +(real w, pen p) {return linewidth(w)+p;}

pen Dotted(pen p=currentpen) {return linetype(new real[] {0,3})+2*linewidth(p);}
pen Dotted=Dotted();

restricted pen squarecap=linecap(0);
restricted pen roundcap=linecap(1);
restricted pen extendcap=linecap(2);

restricted pen miterjoin=linejoin(0);
restricted pen roundjoin=linejoin(1);
restricted pen beveljoin=linejoin(2);

restricted pen zerowinding=fillrule(0);
restricted pen evenodd=fillrule(1);

bool interior(int windingnumber, pen fillrule)
{
  return windingnumber != undefined &&
    (fillrule(fillrule) == 1 ? windingnumber % 2 == 1 : windingnumber != 0);
}

restricted pen nobasealign=basealign(0);
restricted pen basealign=basealign(1);

pen invisible=invisible();
pen thin() {return settings.thin ? linewidth(0) : defaultpen;}
pen thick(pen p=currentpen) {return linewidth(linewidth(p));}
pen nullpen=linewidth(0)+invisible;

pen black=gray(0);
pen white=gray(1);
pen gray=gray(0.5);

pen red=rgb(1,0,0);
pen green=rgb(0,1,0);
pen blue=rgb(0,0,1);

pen Cyan=cmyk(1,0,0,0);
pen Magenta=cmyk(0,1,0,0);
pen Yellow=cmyk(0,0,1,0);
pen Black=cmyk(0,0,0,1);

pen cyan=rgb(0,1,1);
pen magenta=rgb(1,0,1);
pen yellow=rgb(1,1,0);

pen palered=rgb(1,0.75,0.75);
pen palegreen=rgb(0.75,1,0.75);
pen paleblue=rgb(0.75,0.75,1);
pen palecyan=rgb(0.75,1,1);
pen palemagenta=rgb(1,0.75,1);
pen paleyellow=rgb(1,1,0.75);
pen palegray=gray(0.95);

pen lightred=rgb(1,0.5,0.5);
pen lightgreen=rgb(0.5,1,0.5);
pen lightblue=rgb(0.5,0.5,1);
pen lightcyan=rgb(0.5,1,1);
pen lightmagenta=rgb(1,0.5,1);
pen lightyellow=rgb(1,1,0.5);
pen lightgray=gray(0.9);

pen mediumred=rgb(1,0.25,0.25);
pen mediumgreen=rgb(0.25,1,0.25);
pen mediumblue=rgb(0.25,0.25,1);
pen mediumcyan=rgb(0.25,1,1);
pen mediummagenta=rgb(1,0.25,1);
pen mediumyellow=rgb(1,1,0.25);
pen mediumgray=gray(0.75);

pen heavyred=rgb(0.75,0,0);
pen heavygreen=rgb(0,0.75,0);
pen heavyblue=rgb(0,0,0.75);
pen heavycyan=rgb(0,0.75,0.75);
pen heavymagenta=rgb(0.75,0,0.75);
pen lightolive=rgb(0.75,0.75,0);
pen heavygray=gray(0.25);

pen deepred=rgb(0.5,0,0);
pen deepgreen=rgb(0,0.5,0);
pen deepblue=rgb(0,0,0.5);
pen deepcyan=rgb(0,0.5,0.5);
pen deepmagenta=rgb(0.5,0,0.5);
pen deepyellow=rgb(0.5,0.5,0);
pen deepgray=gray(0.1);

pen darkred=rgb(0.25,0,0);
pen darkgreen=rgb(0,0.25,0);
pen darkblue=rgb(0,0,0.25);
pen darkcyan=rgb(0,0.25,0.25);
pen darkmagenta=rgb(0.25,0,0.25);
pen darkolive=rgb(0.25,0.25,0);
pen darkgray=gray(0.05);

pen orange=rgb(1,0.5,0);
pen fuchsia=rgb(1,0,0.5);

pen chartreuse=rgb(0.5,1,0);
pen springgreen=rgb(0,1,0.5);

pen purple=rgb(0.5,0,1);
pen royalblue=rgb(0,0.5,1);

// Synonyms:

pen salmon=lightred;
pen brown=deepred;
pen olive=deepyellow;
pen darkbrown=darkred;
pen pink=palemagenta;
pen palegrey=palegray;
pen lightgrey=lightgray;
pen mediumgrey=mediumgray;
pen grey=gray;
pen heavygrey=heavygray;
pen deepgrey=deepgray;
pen darkgrey=darkgray;

// Options for handling label overwriting
restricted int Allow=0;
restricted int Suppress=1;
restricted int SuppressQuiet=2;
restricted int Move=3;
restricted int MoveQuiet=4;

pen[] colorPen={red,blue,green,magenta,cyan,orange,purple,brown,
                deepblue,deepgreen,chartreuse,fuchsia,lightred,
                lightblue,black,pink,yellow,gray};

colorPen.cyclic=true;

pen[] monoPen={solid,dashed,dotted,longdashed,dashdotted,
               longdashdotted};
monoPen.cyclic=true;

pen Pen(int n)
{
  return (settings.gray || settings.bw) ? monoPen[n] : colorPen[n];
}

pen Pentype(int n)
{
  return (settings.gray || settings.bw) ? monoPen[n] : monoPen[n]+colorPen[n];
}

real dotsize(pen p=currentpen)
{
  return dotfactor*linewidth(p);
}

pen fontsize(real size)
{
  return fontsize(size,1.2*size);
}

real labelmargin(pen p=currentpen)
{
  return labelmargin*fontsize(p);
}

void write(file file=stdout, string s="", pen[] p)
{
  for(int i=0; i < p.length; ++i)
    write(file,s,p[i],endl);
}

void usetypescript(string s, string encoding="")
{
  string s="\usetypescript["+s+"]";
  if(encoding != "") s +="["+encoding+"]";
  texpreamble(s);
}

pen font(string name, string options="")
{
  // Work around misalignment in ConTeXt switchtobodyfont if font is not found.
  return fontcommand(settings.tex == "context" ?
                     "\switchtobodyfont["+name+
                     (options == "" ? "" : ","+options)+
                     "]\removeunwantedspaces" :
                     "\font\ASYfont="+name+"\ASYfont");
}

pen font(string name, real size, string options="")
{
  string s=(string) (size/pt)+"pt";
  if(settings.tex == "context")
    return fontsize(size)+font(name+","+s,options);
  return fontsize(size)+font(name+" at "+s);
}

pen font(string encoding, string family, string series, string shape)
{
  return fontcommand("\usefont{"+encoding+"}{"+family+"}{"+series+"}{"+shape+
                     "}");
}

pen AvantGarde(string series="m", string shape="n")
{
  return font("OT1","pag",series,shape);
}
pen Bookman(string series="m", string shape="n")
{
  return font("OT1","pbk",series,shape);
}
pen Courier(string series="m", string shape="n")
{
  return font("OT1","pcr",series,shape);
}
pen Helvetica(string series="m", string shape="n")
{
  return font("OT1","phv",series,shape);
}
pen NewCenturySchoolBook(string series="m", string shape="n")
{
  return font("OT1","pnc",series,shape);
}
pen Palatino(string series="m", string shape="n")
{
  return font("OT1","ppl",series,shape);
}
pen TimesRoman(string series="m", string shape="n")
{
  return font("OT1","ptm",series,shape);
}
pen ZapfChancery(string series="m", string shape="n")
{
  return font("OT1","pzc",series,shape);
}
pen Symbol(string series="m", string shape="n")
{
  return font("OT1","psy",series,shape);
}
pen ZapfDingbats(string series="m", string shape="n")
{
  return font("OT1","pzd",series,shape);
}

pen squarepen=makepen(shift(-0.5,-0.5)*unitsquare);

struct hsv {
  real h;
  real v;
  real s;
  void operator init(real h, real s, real v) {
    this.h=h;
    this.s=s;
    this.v=v;
  }
  void operator init(pen p) {
    real[] c=colors(rgb(p));
    real r=c[0];
    real g=c[1];
    real b=c[2];
    real M=max(r,g,b);
    real m=min(r,g,b);
    if(M == m) this.h=0;
    else {
      real denom=1/(M-m);
      if(M == r) {
	this.h=60*(g-b)*denom;
	if(g < b) h += 360;
      } else if(M == g) {
	this.h=60*(b-r)*denom+120;
      } else
	this.h=60*(r-g)*denom+240;
    }
    this.s=M == 0 ? 0 : 1-m/M;
    this.v=M;
  }
  // return an rgb pen corresponding to h in [0,360) and s and v in [0,1].
  pen rgb() {
    real H=(h % 360)/60;
    int i=floor(H) % 6;
    real f=H-i;
    real[] V={v,v*(1-s),v*(1-(i % 2 == 0 ? 1-f : f)*s)};
    int[] a={0,2,1,1,2,0};
    int[] b={2,0,0,2,1,1};
    int[] c={1,1,2,0,0,2};
    return rgb(V[a[i]],V[b[i]],V[c[i]]);
  }
}

pen operator cast(hsv hsv)
{
  return hsv.rgb();
}

hsv operator cast(pen p)
{
  return hsv(p);
}

real[] rgba(pen p)
{
  real[] a=colors(rgb(p));
  a.push(opacity(p));
  return a;
}

pen rgb(real[] a)
{
  return rgb(a[0],a[1],a[2]);
}

pen rgba(real[] a)
{
  return rgb(a[0],a[1],a[2])+opacity(a[3]);
}

// Return a pen corresponding to a given 6-character RGB hexadecimal string.
pen rgb(string s)
{
  int offset=substr(s,0,1) == '#' ? 1 : 0;
  real value(string s, int i) {return byteinv(hex(substr(s,2i+offset,2)));}
  return rgb(value(s,0),value(s,1),value(s,2));
}

pen RGB(int r, int g, int b)
{
  return rgb(r/255,g/255,b/255);
}

pen[] operator +(pen[] a, pen b)
{
  return sequence(new pen(int i) {return a[i]+b;},a.length);
}

pen[] operator +(pen a, pen[] b)
{
  return sequence(new pen(int i) {return a+b[i];},b.length);
}

// Interpolate an array of pens in rgb space using by default their minimum
// opacity.
pen mean(pen[] p, real opacity(real[])=min)
{
  if(p.length == 0) return nullpen;
  real[] a=rgba(p[0]);
  real[] t=new real[p.length];
  t[0]=a[3];
  for(int i=1; i < p.length; ++i) {
    real[] b=rgba(p[i]);
    a += b;
    t[i]=b[3];
  }
  a /= p.length;
  return rgb(a[0],a[1],a[2])+opacity(opacity(t));
}

pen[] mean(pen[][] palette, real opacity(real[])=min)
{
  return sequence(new pen(int i) {return mean(palette[i],opacity);},
		  palette.length);
}