summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/colors/colors-metafun.tex
blob: 82514659bf08ef1c26b9411668315f87db87bc97 (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
% language=uk

\startcomponent colors-basics

\environment colors-environment

\startchapter[title=Metafun][color=darkyellow]

\startsection[title=Defining and using]

In \METAPOST\ itself colors are defined as numbers or sets:

\starttyping
color     red  ; red  := (1,0,0) ;
cmykcolor cyan ; cyan := (1,0,0,0) ;
numeric   gray ; gray := 0.5 ;
\stoptyping

You don't need much fantasy to see that this fits well in the data model of
\METAPOST. In fact, transparency could be represented as a \type {pair}. The
disadvantage of having no generic color type is that you cannot mix them. In case
you need to manipulate them, you can check the type:

\starttyping
if cmykcolor cyan : ... fi ;
\stoptyping

because \METAFUN\ is tightly integrated in \CONTEXT\ you can refer to colors
known at the \TEX\ end by string. So,

\starttyping
string mycolor ; mycolor := "red" ;
\stoptyping

and then:

\starttyping
fill fullcircle scaled 4cm withcolor mycolor ;
\stoptyping

is quite okay. For completeness we also have \type {namedcolor} but it's not
really needed:

\starttyping
fill fullcircle scaled 4cm withcolor namedcolor("red");
\stoptyping

You can define spot colors too but normally you will refer to colors
defined at the \TEX\ end.

\startbuffer[spot]
\startMPcode
    fill fullcircle scaled 3cm withcolor
        .5 * spotcolor("whatever",(.3,.4,.5)) ;
    fill fullcircle scaled 2cm withcolor
             spotcolor("whatever",(.3,.4,.5)) ;
    fill fullcircle scaled 1cm withcolor
             spotcolor("whatever",(.3,.4,.5)/2) ;
\stopMPcode
\stopbuffer

\startbuffer[multi]
\startMPcode
    fill fullcircle scaled 3cm withcolor
        .5 * multitonecolor("whatever",(.3,.4,.5),(.5,.3,.4)) ;
    fill fullcircle scaled 2cm withcolor
             multitonecolor("whatever",(.3,.4,.5),(.5,.3,.4)) ;
    fill fullcircle scaled 1cm withcolor
             multitonecolor("whatever",(.3,.4,.5)/2,(.5,.3,.4)/2) ;
\stopMPcode
\stopbuffer

\typebuffer[spot]

Multitones are defined as:

\typebuffer[multi]

Some \PDF\ renderers have problems with fractions of such colors and even display
the wrong colors. So, in these examples the third alternative in the sets of
three might be more robust than the first. The result is shown in \in {figure}
[fig:mpspot].

\startplacefigure[reference=fig:mpspot,title={Spot and multitones directly defined in \METAFUN.}]
    \startcombination[2*1]
        {\getbuffer[spot]}  {}
        {\getbuffer[multi]} {}
    \stopcombination
\stopplacefigure

\stopsection

\startsection[title=Passing colors]

Originally \TEX\ and \METAPOST\ were separated processes and even in \LUATEX\
they still are. There can be many independent \METAPOST\ instances present, but
always there is \LUA\ as glue between them. In the early days of \LUATEX\ this
was a one way channel: the \METAPOST\ output is available at the \TEX\ end in
\LUA\ as a table and properties are used to communicate extensions. In today's
\LUATEX\ the \METAPOST\ library has access to \LUA\ itself so that gives us a
channel to \TEX, although with some limitations.

Say that we have a color defined as follows:

\startbuffer
\definecolor[MyColor][r=.25,g=.50,b=.75]
\stopbuffer

\typebuffer \getbuffer

We can apply this to a rule:

\startbuffer
\blackrule[color=MyColor,width=3cm,height=1cm,depth=0cm]
\stopbuffer

\typebuffer

From this we get:

\startlinecorrection
\getbuffer
\stoplinecorrection

In \TEX\ (code) we can do this:

\startbuffer
\startMPcode
    fill unitsquare xyscaled (3cm,1cm) withcolor \MPcolor {MyColor} ;
\stopMPcode
\stopbuffer

\typebuffer

When the code is pushed to \METAPOST\ the color gets expanded, in this case to
\typ {(0.25, 0.50, 0.75)} because we specified an \RGB\ color but the other
colorspaces are supported too.

\startlinecorrection
\getbuffer
\stoplinecorrection

Equally valid code is:

\starttyping
\startMPcode
    fill unitsquare xyscaled (3cm,1cm) withcolor "MyColor" ;
\stopMPcode
\stoptyping

This is very un-\METAPOST\ as naturally it can only deal with numerics for gray
scales, triplets for \RGB\ colors, and quadruplets for \CMYK\ colors. In
\METAFUN\ (as present in \CONTEXT\ MKIV) the \type {withcolor} operator also
accepts a string, which is resolved to a color specification.

For the record we note that when you use transparent colors, a more complex
specification gets passed with \type {\MPcolor} or resolved (via the string). The
same is true for spot and multitone colors. It will be clear that when you want
to assign a color to a variable you have to make sure the type matches. A rather
safe way to define colors is:

\starttyping
def MyColor =
    \MPcolor{MyColor}
enddef ;
\stoptyping

and because we can use strings, string variables are also an option.

\stopsection

\startsection[title=Grouping]

The reason for discussing these details is that there is a rather fundamental
concept of grouping in \TEX\ which can lead to unexpected side effects. The
reason is that there is no grouping at the \LUA\ end, unless one uses a kind of
stack, and that in \METAPOST\ grouping is an explicit feature.

\starttyping
\scratchcounter=123
\bgroup
    \scratchcounter=456
\egroup
\stoptyping

After this \TEX\ code is expanded the counter has value 123. In \METAPOST\ you
can do the following:

\starttyping
scratchcounter := 123 ;
\begingroup
    scratchcounter := 456 ;
\endgroup
\stoptyping

but here the counter is 456 afterwards! You explicitly need to save a value:

\starttyping
scratchcounter := 123 ;
\begingroup
    save scratchcounter ;
    numeric scratchcounter ; % variables are numeric by default anyway
    scratchcounter := 456 ;
\endgroup
\stoptyping

The difference perfectly makes sense if you think about the kind of applications
\TEX\ and \METAPOST\ are used for. In \LUA\ you can do this:


\starttyping
scratchcounter = 123
do
    local scratchcounter = 456
end
\stoptyping

and in fact, a \type {then}, \type {else}, \type {while}, \type {repeat}, \type
{do} and function body also behave this way.

So, what is the impact on colors? Imagine that you do this:

\startbuffer
\bgroup
    \definecolor[MyColor][s=.5]
    \startMPcode
        pickup pencircle scaled 4mm ;
        draw fullcircle scaled 30mm withcolor \MPcolor{MyColor} ;
        draw fullcircle scaled 15mm withcolor "MyColor" ;
    \stopMPcode
\egroup
\quad
\startMPcode
    pickup pencircle scaled 4mm ;
    draw fullcircle scaled 30mm withcolor \MPcolor{MyColor} ;
    draw fullcircle scaled 15mm withcolor "MyColor" ;
\stopMPcode
\stopbuffer

\typebuffer

We get the following colors:

\startlinecorrection
\hbox{\getbuffer}
\stoplinecorrection

Because \type {\MPcolor} is a \TEX\ macro, its value gets expanded when the
graphic is calculated. After the group (first graphic) the color is restored.
But, in order to access the colors defined at the \TEX\ end in \METAPOST\ by name
(using \LUA) we need to make sure that a defined color is registered at that end.
Before we could use the string accessor in \METAPOST\ colors, this was never a
real issue. We kept the values in a (global) \LUA\ table which was good enough
for the cases where we wanted to access color specifications, for instance for
tracing. Such colors never changed in a document. But with the more dynamic
\METAPOST\ graphics we cannot do that: there is no way that \METAPOST\ (or \LUA)
later on can know that the color was defined inside a group as clone. For daily
usage it's enough to know that we have found a way around it in \CONTEXT\ at
neglectable overhead. In the rare case this mechanism fails, you can always
revert to the \type {\MPcolor} method.

\startbuffer
\definecolor[DemoOne][red]
\definecolor[DemoTwo][s=.8,t=0.5,a=1]

%definepalet[DemoPalet][NumberColor={g=1}]
\definepalet[DemoPalet][NumberColor=red,red=cyan]
\definepalet[DemoPalet][NumberColor=red]

\setuppalet[DemoPalet]

\bgroup
    \definecolor[red]    [b=.8]
    \definecolor[DemoOne][yellow]
    \startMPcode
        fill fullcircle scaled 10 withcolor "NumberColor" ;
        fill fullcircle scaled  7 withcolor "red" ;
        fill fullcircle scaled  6 withcolor .5\MPcolor{red} ;
        fill fullcircle scaled  4 shifted (-4,0) withcolor \MPcolor{DemoTwo} ;
        fill fullcircle scaled  4 shifted ( 4,0) withcolor "DemoTwo" ;
        fill fullcircle scaled  2 withcolor "DemoOne" ;
        fill fullcircle scaled  1 withcolor \MPcolor{NumberColor} ;
        currentpicture := currentpicture xysized(5cm,3cm) ;
    \stopMPcode
\egroup
\hskip1cm
\startMPcode
    fill fullcircle scaled 10 withcolor "NumberColor" ;
    fill fullcircle scaled  7 withcolor "red" ;
    fill fullcircle scaled  6 withcolor .5\MPcolor{red} ;
    fill fullcircle scaled  4 shifted (-4,0) withcolor \MPcolor{DemoTwo} ;
    fill fullcircle scaled  4 shifted ( 4,0) withcolor "DemoTwo" ;
    fill fullcircle scaled  2 withcolor "DemoOne" ;
    fill fullcircle scaled  1 withcolor \MPcolor{NumberColor} ;
        currentpicture := currentpicture xysized(5cm,3cm) ;
\stopMPcode
\stopbuffer

The following example was used when developing the string based color resolver.
The complication was in getting the color palets resolved right without too much
overhead. Again we demonstrate this because border cases might occur that are not
catched (yet).

\startlinecorrection
    \hbox {\getbuffer}
\stoplinecorrection

\stopsection

\startsection[title=Transparency]

Transparency is supported at the \TEX\ end: either or not bound to colors. We
already saw how to use colors, here's how to apply transparency:

\startbuffer
\startMPcode
    fill fullsquare xyscaled (4cm,2cm) randomized 5mm
        withcolor "darkred" ;
    fill fullsquare xyscaled (2cm,4cm) randomized 5mm
        withcolor "darkblue" withtransparency ("normal",0.5) ;

    fill fullsquare xyscaled (4cm,2cm) randomized 5mm shifted (45mm,0)
        withcolor "darkred"  withtransparency ("normal",0.5) ;
    fill fullsquare xyscaled (2cm,4cm) randomized 5mm shifted (45mm,0)
        withcolor "darkblue" withtransparency ("normal",0.5) ;

    fill fullsquare xyscaled (4cm,2cm) randomized 5mm shifted (90mm,0)
        withcolor "darkred"  withtransparency ("normal",0.5) ;
    fill fullsquare xyscaled (2cm,4cm) randomized 5mm shifted (90mm,0)
        withcolor "darkblue" ;
\stopMPcode
\stopbuffer

\typebuffer

We get a mixture of normal and transparent colors. Instead of \type {normal}
you can also pass a number (with \type {1} being \type {normal}).

\startlinecorrection
    \getbuffer
\stoplinecorrection

\stopsection

\startsection[title=Shading]

Shading is available too. This mechanism is a bit more complex deep down because
it needs resources as well as shading vectors that adapt themselves to the current
scale. We will not go into detail about the shading properties here.

\startbuffer
\startMPcode
    comment("two shades with mp colors");
    fill fullcircle scaled 5cm
        withshademethod "circular"
        withshadevector (2cm,1cm)
        withshadecenter (.1,.5)
        withshadedomain (.2,.6)
        withshadefactor 1.2
        withshadecolors (red,white)
        ;
    fill fullcircle scaled 5cm shifted (6cm,0)
        withshademethod "circular"
        withcolor "red" shadedinto "blue"
    ;
\stopMPcode
\stopbuffer

\typebuffer

You can use normal \METAPOST\ colors as well as named colors.

\startlinecorrection
    \getbuffer
\stoplinecorrection

\startbuffer
\startMPcode
    comment("two shades with named colors");
    fill fullcircle scaled 5cm
        withshademethod "circular"
        withshadecolors ((1,0,0),(0,0,1,0))
    ;
    fill fullcircle scaled 5cm shifted (6cm,0)
        withshademethod "circular"
        withcolor (1,0,0,0) shadedinto "blue"
    ;
\stopMPcode
\stopbuffer

The color space of the first color determines if the second one needs
to be converted, so this is valid:

\typebuffer

The first circle is in \RGB\ colors and the second in \CMYK.

\startlinecorrection
    \getbuffer
\stoplinecorrection

You cannot use spot colors but you can use transparency, so with:

\startbuffer
\startMPcode
    comment("three transparent shades");
    fill fullcircle scaled 5cm
        withshademethod "circular"
        withshadecolors ("red","green")
        withtransparency ("normal",0.5)
    ;
    fill fullcircle scaled 5cm shifted (30mm,0)
        withshademethod "circular"
        withshadecolors ("green","blue")
        withtransparency ("normal",0.5)
    ;
    fill fullcircle scaled 5cm shifted (60mm,0)
        withshademethod "circular"
        withshadecolors ("blue","yellow")
        withtransparency ("normal",0.5)
    ;
\stopMPcode
\stopbuffer

\typebuffer

we get:

\startlinecorrection
    \getbuffer
\stoplinecorrection

You can define a shade and use it later on, for example:

\startbuffer
\startMPcode
    defineshade myshade
        withshademethod "circular"
        withshadefactor 1
        withshadedomain (0,1)
        withshadecolors (black,white)
        withtransparency (1,.5)
    ;

    fill fullcircle xyscaled(.75TextWidth,4cm)
        shaded myshade ;
    fill fullcircle xyscaled(.75TextWidth,4cm) shifted (.125TextWidth,0)
        shaded myshade ;
    fill fullcircle xyscaled(.75TextWidth,4cm) shifted (.25TextWidth,0)
        shaded myshade ;
\stopMPcode
\stopbuffer

\typebuffer

This gives three transparent shaded shapes:

\startlinecorrection
    \getbuffer
\stoplinecorrection

A very special shade is the following:

\startbuffer
\startMPcode
    fill fullsquare yscaled 5ExHeight xscaled TextWidth
        withshademethod "linear"
        withshadevector (0,1)
        withshadestep (
            withshadefraction .3
            withshadecolors (red,green)
        )
        withshadestep (
            withshadefraction .5
            withshadecolors (green,blue)
        )
        withshadestep (
            withshadefraction .7
            withshadecolors (blue,red)
        )
        withshadestep (
            withshadefraction 1
            withshadecolors (red,yellow)
        )
    ;
\stopMPcode
\stopbuffer

\typebuffer

The result is a colorful band:

\startlinecorrection
    \getbuffer
\stoplinecorrection

\stopsection

\startsection[title=Text]

The text typeset with \type {textext} is processed in \TEX\ using the
current settings. A text can of course have color directives embedded.

\startbuffer
\startMPcode
numeric u ; u := 8mm ;
draw thetextext("RED",                     (0,0u)) withcolor darkred ;
draw thetextext("\darkgreen GREEN",        (0,1u)) ;
draw thetextext("\darkblue   BLUE",        (0,2u)) withcolor darkred ;
draw thetextext("BLACK {\darkgreen GREEN}",(0,3u)) ;
draw thetextext("RED   {\darkblue   BLUE}",(0,4u)) withcolor darkred ;
\stopMPcode
\stopbuffer

\typebuffer

In this example we demonstrate that you can also apply a color to the
resulting picture.

\startlinecorrection
\tttfd \getbuffer
\stoplinecorrection

\stopsection

\startsection[title=Helpers]

\stopsection

There are several color related macros in \METAFUN\ and these are discussed
in the \METAFUN\ manual, so we only mention a few here.

\startbuffer
\startMPcode
    fill fullsquare xyscaled(TextWidth,4cm)
        withcolor darkred ;
    fill fullsquare xyscaled(TextWidth,3cm)
        withcolor complementary darkred ;
    fill fullsquare xyscaled(TextWidth,2cm)
        withcolor complemented darkred ;
    fill fullsquare xyscaled(TextWidth,1cm)
        withcolor grayed darkred ;
\stopMPcode
\stopbuffer

\typebuffer

This example code is shown in \in {figure} [fig:complemen-1]. The \type
{complementary} operator subtracts the given color from white, the \type
{complemented} operator calculates its values from opposites (so a zero becomes a
one). In \in {figure} [fig:complemen-2] a more extensive example is shown.

\startplacefigure
    [reference=fig:complemen-1,
     title={The \type {complementary}, \type {complemented} and \type
     {grayed} methodscompared.}]
    \getbuffer
\stopplacefigure

\startMPdefinitions
    % This is an old example I had laying around since 2005. The original was just
    % a framed text with the graphic as background but here I use textext instead.
    def MyCompare (text method) =

        picture p ; p := textext("\quad \bf
            I don't understand about complementary colors\quad
            And what they say\quad
            Side by side they both get bright\quad
            Together they both get gray\quad"
        ) ;

        numeric w ; w := bbwidth  p ;
        numeric h ; h := bbheight p ;

        for i = 1 upto 10 :
            fill fullsquare
                xscaled (w/10)
                yscaled 5h
                shifted (-w/2-w/20+i*w/10,-3h/2)
                withcolor (i*red/10)
                withtransparency(1,.5) ;
            fill fullsquare
                xscaled (w/10)
                yscaled 5h
                shifted (-w/2-w/20+i*w/10,3h/2)
                withcolor method (i*red/10)
                withtransparency(1,.5) ;
        endfor ;
        addbackground withcolor .75white ;

        draw p withcolor white ;

        currentpicture := currentpicture xsized TextWidth ;
    enddef ;
\stopMPdefinitions

\startplacefigure[reference=fig:complemen-2,title={Two methods to complement colors compared (text: Fiona Apple).}]
    \startcombination[1*2]
        {\startMPcode MyCompare(complemented)  ; \stopMPcode} {complemented}
        {\startMPcode MyCompare(complementary) ; \stopMPcode} {complementary}
    \stopcombination
\stopplacefigure

As we discussed before, the different color models in \METAPOST\ cannot be mixed
in expressions. We therefore have two macros that expand into white or black
in the right colorspace.

\typebuffer

\startbuffer
\startMPcode
    fill fullsquare xyscaled(TextWidth,4cm)
        withcolor .5[(.5,0,0),   whitecolor (.5,0,0)] ;
    fill fullsquare xyscaled(TextWidth,3cm)
        withcolor .5[(.5,0,0),   blackcolor (.5,0,0)] ;
    fill fullsquare xyscaled(TextWidth,2cm)
        withcolor .5[(.5,0,0,0), whitecolor (.5,0,0,0)] ;
    fill fullsquare xyscaled(TextWidth,1cm)
        withcolor .5[(.5,0,0,0), blackcolor (.5,0,0,0)] ;
\stopMPcode
\stopbuffer

\typebuffer

\startlinecorrection
\getbuffer
\stoplinecorrection

There are two macros that can be used to resolve string to colors: \type
{resolvedcolor} and \type {namedcolor}. A resolved color is expanded via \LUA\
while a named color is handled in the backend, when the result is converted to
\PDF. The resolved approach is more recent and is the same as a string color
specification.

\startbuffer
\startMPcode
    fill fullcircle scaled 4cm withcolor .5 resolvedcolor "darkred" ;
    fill fullcircle scaled 3cm withcolor .5 resolvedcolor "gray" ;
    fill fullcircle scaled 2cm withcolor .5 namedcolor    "darkblue" ;
    fill fullcircle scaled 1cm withcolor .5 namedcolor    "gray" ;
\stopMPcode
\stopbuffer

\typebuffer

\startlinecorrection
\getbuffer
\stoplinecorrection

There is a \type {drawoptions} macro that can be used to define properties in one go.

\startbuffer
\startMPcode
    drawoptions(withcolor "darkgreen");
    fill fullcircle scaled 4cm  ;
    fill fullcircle scaled 3cm withcolor white ;
    fill fullcircle scaled 2cm ;
\stopMPcode
\stopbuffer

\typebuffer

We get:

\startlinecorrection
\getbuffer
\stoplinecorrection

The drawback of this approach is that, because we use so called pre- and
postscripts for achieving special effects (like spotcolors and shading)
successive \type {withcolor} calls can interfere in a way that unexpected results
turn up. A way out is to use properties:

\startbuffer
\startMPcode
    property p[] ;
    p1 = properties(withcolor "darkred") ;
    p2 = properties(withcolor "white") ;
    fill fullcircle scaled 4cm withproperties p1 ;
    fill fullcircle scaled 3cm withproperties p2 ;
    fill fullcircle scaled 2cm withproperties p1 ;
\stopMPcode
\stopbuffer

\typebuffer

This results in:

\startlinecorrection
\getbuffer
\stoplinecorrection

\stopchapter

\stopcomponent