summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/metafun/metafun-embedding.tex
blob: 10383fa3afe5402738e9e10d7afb94eaca782842 (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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
% language=uk
%
% copyright=pragma-ade readme=readme.pdf licence=cc-by-nc-sa

\startcomponent metafun-embedding

\environment metafun-environment

\startchapter[reference=sec:embedding,title={Embedded graphics}]

\startintro

In addition to the \type {beginfig}||\type {endfig} method, there are other ways
to define and include a \METAPOST\ graphic. Each method has its advantages and
disadvantages.

In the previous chapter we were still assuming that the graphic was defined in
its own file. In this chapter we will introduce the interface between \CONTEXT\
and \METAPOST\ and demonstrate how the definitions of the graphics can be
embedded in the document source.

\stopintro

\startsection[title={Getting started}]

\index{running}
\index{processing}

From now on, we will assume that you have \CONTEXT\ running on your platform.
Since \PDF\ has full graphics support, we also assume that you use \LUATEX\ in
combination with \CONTEXT\ \MKIV, although most will also work with other engines
and \MKII. Since this document is not meant as a \CONTEXT\ tutorial, we will
limit this introduction to the basics needed to run the examples.

A simple document looks like:

\starttyping
\starttext
  Some text.
\stoptext
\stoptyping

You can process this document with the \LUA\ based command line interface to
\CONTEXT. If the source code is embedded in the file \type {mytext.tex}, you can
say:

\starttyping
context mytext
\stoptyping

We will use color, and in \MKIV\ color is enabled by default. If you don't want
color you can tell \CONTEXT, so

\starttyping
\setupcolors[state=stop]
\starttext
  Some \color[blue]{text} and/or \color[green]{graphics}.
\stoptext
\stoptyping

comes out in black and white.

In later chapters we will occasionally see some more \CONTEXT\ commands show up.
If you want to know more about what \CONTEXT\ can do for you, we recommend the
beginners manual and the reference manual, as well as the wiki pages.

\stopsection

\startsection[title={External graphics}]

\index {graphics+external}

Since \TEX\ has no graphic capabilities built in, a graphic is referred to as an
external figure. A \METAPOST\ graphic often has a number as suffix, so embedding
such a graphic is done by:

\starttyping
\externalfigure[graphic.123][width=4cm]
\stoptyping

An alternative method is to separate the definition from the inclusion. An
example of a definition is:

\starttyping
\useexternalfigure[pentastar][star.803][height=4cm]
\useexternalfigure[octostar] [star.804][pentastar]
\stoptyping

Here, the second definition inherits the characteristics from the first one.
These graphics can be summoned like:

\starttyping
\placefigure
  {A five||point star drawn by \METAPOST.}
  {\externalfigure[pentastar]}
\stoptyping

Here the stars are defined as stand||alone graphics, in a file called \type
{star.mp}. Such a file can look like:

\starttyping
def star (expr size, n, pos) =
  for a=0 step 360/n until round(360*(1-1/n)) :
    draw (origin -- (size/2,0))
      rotatedaround (origin,a) shifted pos ;
  endfor ;
enddef ;

beginfig(803) ;
  pickup pencircle scaled 2mm ; star(2cm,5,origin) ;
endfig ;

beginfig(804) ;
  pickup pencircle scaled 1mm ; star(1cm,8,origin) ;
  pickup pencircle scaled 2mm ; star(2cm,7,(3cm,0)) ;
endfig ;

end.
\stoptyping

This \type {star} macro will produce graphics like:

\startbuffer
def star (expr size, n, pos) =
  for a=0 step 360/n until round(360*(1-1/n)) :
    draw (origin -- (size/2,0))
      rotatedaround (origin,a) shifted pos ;
  endfor ;
enddef ;

for i=5 upto 10 :
  drawoptions (withpen pencircle scaled 2mm withcolor .625red) ;
  star(1cm,i,origin shifted (i*2cm,0)) ;
endfor ;
\stopbuffer

\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection

But, now that we have instant \METAPOST\ available in \LUATEX, there is no need
for external images and we can collect them in libraries, as we will see later
on.

\stopsection

\startsection[title={Integrated graphics}]

\index{graphics+embedded}

An integrated graphic is defined in the document source or in a style definition
file. The most primitive way of doing this is just inserting the code:

\starttyping
\startMPcode
  fill fullcircle scaled 200pt withcolor .625white ;
\stopMPcode
\stoptyping

Such a graphic is used once at the spot where it is defined. In this document we
also generate graphics while we finish a page, so there is a good chance that
when we have constructed a graphic which will be called on the next page, the
wrong graphic is placed.

For this reason there are are more convenient ways of defining and using
graphics, which have the added advantage that you can predefine multiple
graphics, thereby separating the definitions from the usage.

The first alternative is a {\em usable} graphic. Such a graphic is calculated
anew each time it is used. An example of a usable graphic is:

\starttyping
\startuseMPgraphic{name}
  fill fullcircle scaled 200pt withcolor .625yellow ;
\stopuseMPgraphic
\stoptyping

When you put this definition in the preamble of your document, you can place this
graphic anywhere in the file, saying:

\starttyping
\useMPgraphic{name}
\stoptyping

As said, this graphic is calculated each time it is placed, which can be time
consuming. Apart from the time aspect, this also means that the graphic itself is
incorporated many times. Therefore, for graphics that don't change, \CONTEXT\
provides {\em reusable} graphics:

\starttyping
\startreusableMPgraphic{name}
  fill fullcircle scaled 200pt withcolor .625yellow;
\stopreusableMPgraphic
\stoptyping

This definition is accompanied by:

\starttyping
\reuseMPgraphic{name}
\stoptyping

Imagine that we use a graphic as a background for a button. We can create a
unique and reusable graphic by saying:

\starttyping
\def\MyGraphic
  {\startreusableMPgraphic{name:\overlaywidth:\overlayheight}
      path p ; p := unitsquare
        xscaled OverlayWidth yscaled OverlayHeight ;
      fill p withcolor .625yellow ;
      draw p withcolor .625red ;
   \stopreusableMPgraphic
   \reuseMPgraphic{name:\overlaywidth:\overlayheight}}
\stoptyping

Notice the use of \type {OverlayWidth} and \type {OverlayHeight}. These variables
are set for each call to \METAPOST. After this we can say:

\starttyping
\defineoverlay[my graphic][\MyGraphic]
\button[background=my graphic,frame=off]{Go Home}[firstpage]
\stoptyping

Say that we have a 30pt by 20pt button, then the identifier will be \type
{name:30pt:20pt}. Different dimensions will lead to other identifiers, so this
sort of makes the graphics unique.

We can bypass the ugly looking \type {\def} by using a third class of embedded
graphics, the {\em unique} graphics.

\starttyping
\startuniqueMPgraphic{name}
  path p ; p := unitsquare
    xscaled OverlayWidth yscaled OverlayHeight ;
  fill p withcolor .625yellow ;
  draw p withcolor .625red ;
\stopuniqueMPgraphic
\stoptyping

Now we can say:

\starttyping
\defineoverlay[my graphic][\uniqueMPgraphic{name}]
\button[background=my graphic,frame=off]{Go Home}[firstpage]
\stoptyping

A shorter variant is:

\starttyping
\startuniqueMPgraphic{name}
  fill OverlayBox withcolor .625yellow ;
  draw OverlayBox withcolor .625red ;
\stopuniqueMPgraphic
\stoptyping

You may wonder why unique graphics are needed when a single graphic might be used
multiple times by scaling it to fit the situation. Since a unique graphic is
calculated for each distinctive case, we can be sure that the current
circumstances are taken into account. Also, scaling would result in incomparable
graphics. Consider the following definition:

\startbuffer[a]
\startuseMPgraphic{demo}
  draw unitsquare
    xscaled 5cm yscaled 1cm
    withpen pencircle scaled 2mm
    withcolor .625red ;
\stopuseMPgraphic
\stopbuffer

\typebuffer[a]

Since we reuse the graphic, the dimensions are sort of fixed, and because the
graphic is calculated once, scaling it will result in incompatible line widths.

\startbuffer[b]
\hbox \bgroup
  \scale[width=5cm,height=1cm]{\useMPgraphic{demo}}\quad
  \scale[width=8cm,height=1cm]{\useMPgraphic{demo}}%
\egroup
\stopbuffer

\startlinecorrection[blank]
\getbuffer[a,b]
\stoplinecorrection

These graphics were placed with:

\typebuffer[b]

Imagine what happens when we add some buttons to an interactive document without
taking care of this side effect. All the frames would look different. Consider
the following example.

\startbuffer[a]
\startuniqueMPgraphic{right or wrong}
  pickup pencircle scaled .075 ;
  fill unitsquare withcolor .8white ;
  draw unitsquare withcolor .625red ;
  currentpicture := currentpicture
    xscaled OverlayWidth yscaled OverlayHeight ;
\stopuniqueMPgraphic
\stopbuffer

\typebuffer[a]

Let's define this graphic as a background to some buttons.

\startbuffer[b]
\defineoverlay[button][\uniqueMPgraphic{right or wrong}]
\setupbuttons[background=button,frame=off]
\stopbuffer

\startbuffer[c]
\hbox
  {\button {previous}          [previouspage]\quad
   \button {next}              [nextpage]\quad
   \button {index}             [index]\quad
   \button {table of contents} [content]}
\stopbuffer

\typebuffer[b,c]

The buttons will look like:

\startlinecorrection[blank]
\setupinteraction[state=start,color=,contrastcolor=]
\getbuffer[a,b,c]
\stoplinecorrection

Compare these with:

\startbuffer[a]
\startuniqueMPgraphic{wrong or right}
  pickup pencircle scaled 3pt ;
  path p ; p := unitsquare
    xscaled OverlayWidth yscaled OverlayHeight ;
  fill p withcolor .8white ;
  draw p withcolor .625red ;
\stopuniqueMPgraphic
\stopbuffer

\startlinecorrection[blank]
\getbuffer[a,b]
\defineoverlay[button][\uniqueMPgraphic{wrong or right}]
\setupinteraction[state=start,color=,contrastcolor=]
\getbuffer[c]
\stoplinecorrection

Here the graphic was defined as:

\typebuffer[a]

The last class of embedded graphics are the {\em runtime} graphics. When a
company logo is defined in a separate file \type {mylogos.mp}, you can run this
file by saying:

\starttyping
\startMPrun
  input mylogos ;
\stopMPrun
\stoptyping

The source for the logo is stored in a file named \type {mylogos.mp}.

\startbuffer
beginfig(21) ;
  draw fullsquare            withcolor .625red ;
  draw fullsquare rotated 45 withcolor .625red ;
  picture cp ; cp := currentpicture ;
  def copy = addto currentpicture also cp enddef ;
  copy scaled .9 withcolor .625white  ;
  copy scaled .7 withcolor .625yellow ;
  copy scaled .6 withcolor .625white  ;
  copy scaled .4 withcolor .625red    ;
  copy scaled .3 withcolor .625white  ;
  fill fullcircle scaled .2 withcolor .625yellow ;
  currentpicture := currentpicture scaled 50 ;
endfig ;
end .
\stopbuffer

\typebuffer

In this example the result is available in the virtual file \type {mprun.21}.
This file can be included in the normal way, using:

\starttyping
\externalfigure[mprun.21][width=5cm]
\stoptyping

\startuseMPgraphic{dummy logo}
  draw fullsquare            withcolor .625red ;
  draw fullsquare rotated 45 withcolor .625red ;
  picture cp ; cp := currentpicture ;
  def copy = addto currentpicture also cp enddef ;
  copy scaled .9 withcolor .625white  ;
  copy scaled .7 withcolor .625yellow ;
  copy scaled .6 withcolor .625white  ;
  copy scaled .4 withcolor .625red    ;
  copy scaled .3 withcolor .625white  ;
  fill fullcircle scaled .2 withcolor .625yellow ;
  currentpicture := currentpicture scaled 3cm ;
\stopuseMPgraphic

\placefigure
  {The logo is defined in the file \type {mylogos.mp} as
   figure~21 and processed by means of the \type {mprun}
   method.}
  {\useMPgraphic{dummy logo}}

Optionally you can specify a name and an instance. This has the advantage that
the graphics don't interfere with the regular inline graphics. Here the instance
used is \type {extrafun} and the name where the run is stored is \type {mydemo}.

\startbuffer
\startMPrun{extrafun::mydemo}
    input mfun-mrun-demo.mp ;
\stopMPrun

\placefigure
  {An external file can have multiple graphics. Here we show a few
   images that we used to use on the \PRAGMA\ \CONTEXT\ website.}
  {\startcombination[2*2]
     {\externalfigure[mprun:extrafun::mydemo.1][height=6cm]} {downloads}
     {\externalfigure[mprun:extrafun::mydemo.2][height=6cm]} {links}
     {\externalfigure[mprun:extrafun::mydemo.3][height=6cm]} {mirrors}
     {\externalfigure[mprun:extrafun::mydemo.4][height=6cm]} {team}
   \stopcombination}
\stopbuffer

\typebuffer

Keep in mind that the whole file will be processed (using the built in library)
in order to get one graphic. Normally this is no big deal.

\getbuffer

\stopsection

\startsection[title={Using \METAFUN\ but not \CONTEXT}]

\index{graphics+standalone}

If you don't want to use \CONTEXT\ but still want to use \METAFUN, a rather
convenient method is the following. Create a file that

\starttyping
\startMPpage
    % Your mp code goes here. You can use the textext
    % macro as discussed later to deal with typeset text.
\stopMPpage
\stoptyping

When you process that file with the \type {context} command you will get a \PDF\
file that you can include in any application that can embed a \PDF\ image. In
this case your exposure to \CONTEXT\ is minimal.

\stopsection

\startsection[title={Graphic buffers}]

\index{graphics+buffers}
\index{buffers}

In addition to the macros defined in the previous section, you can use \CONTEXT's
buffers to handle graphics. This can be handy when making documentation, so it
makes sense to spend a few words on them.

A buffer is a container for content that is to be (re|)|used later on. The main
reason for their existence is that they were needed for typesetting manuals and
articles on \TEX. By putting the code snippets in buffers, we don't have to key
in the code twice, since we can either show the code of buffers verbatim, or
process the code as part of the text flow. This means that the risk of mismatch
between the code shown and the typeset text is minimized.

\startbuffer[a]
\startbuffer
You are reading the \METAFUN\ manual.
\stopbuffer
\stopbuffer

\typebuffer[a]

This buffer can be typeset verbatim using \type {\typebuffer} and processed using
\type {\getbuffer}, as we will do now:

\blank \getbuffer[a] \blank

An other advantage of using buffers, is that they help you keeping the document
source clean. In many places in this manual we put table or figure definitions in
a buffer and pass the buffer to another command, like:

\starttyping
\placefigure{A very big table}{\getbuffer}
\stoptyping

Sometimes it makes sense to collect buffers in separate files. In that case we
give them names.

\startbuffer
\startbuffer[mfun]
You are reading the \METAFUN\ manual.
\stopbuffer
\stopbuffer

This time we should say \type {\typebuffer[mfun]} to typeset the code verbatim.
Instead of \TEX\ code, we can put \METAPOST\ definitions in buffers.

\startbuffer
\startbuffer[graphic]
draw fullcircle scaled 2cm ;
\stopbuffer
\stopbuffer

Buffers can be used to stepwise build graphics. By putting code in multiple
buffers, you can selectively process this code.

\startbuffer
\startbuffer[red]
drawoptions(withcolor .625red) ;
\stopbuffer

\startbuffer[yellow]
drawoptions(withcolor .625yellow) ;
\stopbuffer
\stopbuffer

\typebuffer

We can now include the same graphic in two colors by simply using different
buffers. This time we use the special command \type {\processMPbuffer}, since
\type {\getbuffer} will typeset the code fragment, which is not what we want.

\startbuffer
\startlinecorrection[blank]
\processMPbuffer[red,graphic]
\stoplinecorrection
\stopbuffer

\typebuffer

The line correction macros take care of proper spacing around the graphic. The
\type {[blank]} directive tells \CONTEXT\ to add more space before and after the
graphic.

\startbuffer
\startlinecorrection[blank]
\processMPbuffer[yellow,graphic]
\stoplinecorrection
\stopbuffer

\typebuffer

Which mechanism you use, (multiple) buffers or (re|)|usable graphics, depends on
your preferences. Buffers are slower but don't take memory, while (re|)|usable
graphics are stored in memory which means that they are accessed faster.

\stopsection

\startsection[title={Communicating color}]

\index{color}

Now that color has moved to the desktop, even simple documents have become more
colorful, so we need a way to consistently apply color to text as well as
graphics. In \CONTEXT, colors are called by name.

The next definitions demonstrate that we can define a color using different color
models, \RGB\ or \CMYK. Depending on the configuration, \CONTEXT\ will convert
one color system to the other, \RGB\ to \CMYK, or vice versa. The full repertoire
of color components that can be set is as follows.

\starttyping
\definecolor[color one]  [r=.1, g=.2, b=.3]
\definecolor[color two]  [c=.4, m=.5, y=.6, k=.7]
\definecolor[color three][s=.8]
\stoptyping

The numbers are limited to the range $0\dots1$ and represent percentages. Black
is represented by:

\starttyping
\definecolor[black 1] [r=0, g=0, b=0]
\definecolor[black 2] [c=0, m=0, y=0, k=1]
\definecolor[black 3] [s=0]
\stoptyping

Predefined colors are passed to \METAPOST\ graphics via the \type {\MPcolor}.
First we define some colors.

\starttyping
\definecolor[darkyellow][y=.625] % a CMYK color
\definecolor[darkred]   [r=.625] % a RGB color
\definecolor[darkgray]  [s=.625] % a gray scale
\stoptyping

These are the colors we used in this document. The next example uses two of them.

\startbuffer
\startuseMPgraphic{color demo}
  pickup pencircle scaled 1mm ;
  path p ; p := fullcircle xscaled 10cm yscaled 1cm ;
  fill p withcolor \MPcolor{darkgray} ;
  draw p withcolor \MPcolor{darkred} ;
\stopuseMPgraphic

\useMPgraphic{color demo}
\stopbuffer

\typebuffer

The previous example uses a pure \RGB\ red shade, combined with a gray fill.

\startlinecorrection[blank]
\getbuffer
\stoplinecorrection

Originally \METAPOST\ only supported only the \RGB\ and gray color spaces. In \METAFUN\
we also supported \CMYK\ and spot colors, using an extension mechanism that hooked into
the backend. At some point \METAPOST\ got native support for \CMYK. When you use mixed
color models you need to be aware of the fact that their related variables have different
types:

\starttabulate[||T|T|]
\NC gray \NC numeric   \NC s         \NC \NR
\NC rgb  \NC color     \NC (r,g,b)   \NC \NR
\NC cmyk \NC cmykcolor \NC (c,m,y,k) \NC \NR
\stoptabulate

Because in \METAFUN\ (\type {mpiv}) we hook deeply into the \CONTEXT\ color
mechanisms we can use symbolic names instead. and these are just strings.

There is a fundamental difference between a yellow as defined in \CONTEXT\ using
\CMYK\ and a \RGB\ yellow.

\startbuffer
\definecolor[cmyyellow]    [y=1]
\definecolor[rgbyellow]    [r=1,g=1]

\definecolor[cmydarkyellow][y=.625]
\definecolor[rgbdarkyellow][r=.625,g=.625]
\stopbuffer

\typebuffer \getbuffer

\in {Figure} [fig:many yellows:pass] demonstrates what happens when we multiply
colors by a factor. Since we are not dealing with real \CMYK\ colors,
multiplication gives different results for \CMYK\ colors passed as \type
{\MPcolor}. \in {Figure} [fig:many yellows:fetch] we show the same colors but
this time we use a different method, one that avoids the \TEX\ macro. This method
can be used in \MKIV. We will use both methods in examples.

\def\TestColor#1%
  {\startuseMPgraphic{yellow}
     fill unitsquare xscaled (.30*\the\hsize) yscaled 1cm withcolor #1 ;
   \stopuseMPgraphic
   \useMPgraphic{yellow}}

\startbuffer
\setuptype[style=\ttx]%
\startcombination[3*3]
  {\TestColor{(0,0,1,0)}}               {\type{(0,0,1,0)}}
  {\TestColor{(1,1,0)}}                 {\type{(1,1,0)}}
  {\TestColor{(.5,.5,0)}}               {\type{(.5,.5,0)}}
  {\TestColor{\MPcolor{rgbyellow}}}     {\type{\MPcolor{rgbyellow}}}
  {\TestColor{\MPcolor{rgbdarkyellow}}} {\type{\MPcolor{rgbdarkyellow}}}
  {\TestColor{.5\MPcolor{rgbyellow}}}   {\type{.5\MPcolor{rgbyellow}}}
  {\TestColor{\MPcolor{cmyyellow}}}     {\type{\MPcolor{cmyyellow}}}
  {\TestColor{\MPcolor{cmydarkyellow}}} {\type{\MPcolor{cmydarkyellow}}}
  {\TestColor{.5\MPcolor{cmyyellow}}}   {\type{.5\MPcolor{cmyyellow}}}
\stopcombination
\stopbuffer

\placefigure
  [here][fig:many yellows:pass]
  {All kinds of yellow (passing valued from \TEX).}
  {\getbuffer}

\startbuffer
\setuptype[style=\ttx]%
\startcombination[3*3]
  {\TestColor{(0,0,1,0)}}                 {\type{(0,0,1,0)}}
  {\TestColor{(1,1,0)}}                   {\type{(1,1,0)}}
  {\TestColor{(.5,.5,0)}}                 {\type{(.5,.5,0)}}
  {\TestColor{"rgbyellow"}}               {\type{"rgbyellow"}}
  {\TestColor{"rgbdarkyellow"}}           {\type{"rgbdarkyellow"}}
  {\TestColor{.5namedcolor("rgbyellow")}} {\type{.5namedcolor("rgbyellow")}}
  {\TestColor{"cmyyellow"}}               {\type{"cmyyellow"}}
  {\TestColor{"cmydarkyellow"}}           {\type{"cmydarkyellow"}}
  {\TestColor{.5namedcolor("cmyyellow")}} {\type{.5namedcolor("cmyyellow")}}
\stopcombination
\stopbuffer

\placefigure
  [here][fig:many yellows:fetch]
  {All kinds of yellow (fetching values from \TEX).}
  {\getbuffer}

So, \type {.625red} is the same as \type {[r=.5]}, but \type {.625yellow} is not
the same as \type {[y=.5]}, but matches \type {[r=.5,g=.5]}. \in {Figure}
[fig:some reds] shows the pure and half reds.

\def\TestColor#1%
  {\startMPcode
     fill unitsquare xscaled (.30*\the\hsize) yscaled 1cm withcolor #1 ;
   \stopMPcode}

\startbuffer
\setuptype[style=\ttx]\setupcolors[mpcmyk=no]
\startcombination[3*2]
  {\TestColor{red}}                   {\type{red}}
  {\TestColor{(1,0,0)}}               {\type{(1,0,0)}}
  {\TestColor{(.625,0,0)}}            {\type{(.625,0,0)}}
  {\TestColor{"red"}}                 {\type{"red")}}
  {\TestColor{"darkred"}}             {\type{"darkred")}}
  {\TestColor{.625namedcolor("red")}} {\type{.625namedcolor("red")}}
\stopcombination
\stopbuffer

\placefigure
  [here][fig:some reds]
  {Some kinds of red.}
  {\getbuffer}

In order to prevent problems, we advise you to stick to \RGB\ color
when you create documents for screen and \CMYK\ when producing for print.

In the \METAFUN\ macro collection there is a macro \type {cmyk} that takes four
arguments, representing the cyan, magenta, yellow, and black component. Nowadays
you don't need it as we have native \CMYK.

\startbuffer
fill unitsquare xyscaled (10cm, 5mm) withcolor cmyk(1,0,.3,.3) ;
fill unitsquare xyscaled (10cm,-5mm) withcolor     (1,.3,0,.3) ;
\stopbuffer

\typebuffer

\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection

If you take a close look at the numbers, you will notice that the cyan component
results in a 100\% ink contribution. You will also notice that 30\% black ink is
added. This means that we cannot safely convert this color to \RGB\ ($r=1-c-k<0$)
without losing information. Nevertheless the previous blue bar is presented all
right. This is due to the fact that in \METAFUN\ the \CMYK\ colors are handled as
they should, even when \METAPOST\ does not support this color model.

If you use this feature independent of \CONTEXT, you need to enable it by setting
\type {cmykcolors} to \type {true}. You have to convert the resulting graphic to
\PDF\ by using for instance the \type {mptopdf} suite.

In \CONTEXT\ you can influence this conversion by changing parameters related to
color handling:

\starttyping
\setupcolors[cmyk=yes,rgb=no]
\stoptyping

Unless you know what you are doing, you don't have to change the default settings
(both \type {yes}). In the \CONTEXT\ reference manual you can also read how color
reduction can be handled.

Special care should be paid to gray scales. Combining equal quantities of the
three color inks will not lead to a gray scale, but to a muddy brown shade.

\startbuffer
fill fullsquare xyscaled (10cm,  2cm) withcolor .5white ;
fill fullsquare xyscaled ( 6cm,1.5cm) withcolor cmyk(.5,.5,.5,0) ;
fill fullsquare xyscaled ( 2cm,  1cm) withcolor cmyk(0,0,0,.5) ;
\stopbuffer

\typebuffer

\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection

In \in {figure} [fig:cmyk 1] \in {and} [fig:cmyk 2] you can see some more colors
defined in the \CMYK\ color space. When you display the screen version of this
document, you will notice that the way colors are displayed can differ per
viewer. This is typical for \CMYK\ colors and has to do with the fact that some
assumptions are made with respect to the (print) medium.

\startbuffer[mp]
  fill fullcircle xyscaled (3cm,1cm) withcolor \MPcolor{test} ;
\stopbuffer

\startbuffer[cmyk]
\startcombination[4*1]
  {\definecolor[test][c=1,y=.3,k=.3] \processMPbuffer[mp]} {c=1 y=.3 k=.3}
  {\definecolor[test][c=.9,y=.15]    \processMPbuffer[mp]} {c=.9 y=.15}
  {\definecolor[test][c=.25,y=.8]    \processMPbuffer[mp]} {c=.25 y=.8}
  {\definecolor[test][c=.45,y=.1]    \processMPbuffer[mp]} {c=.45 y=.1}
\stopcombination
\stopbuffer

\placefigure
  [here][fig:cmyk 1]
  {\CMYK\ support enabled.}
  {\getbuffer[cmyk]}

\placefigure
  [here][fig:cmyk 2]
  {\CMYK\ support disabled, no support in \METAPOST.}
  {\setupcolors[cmyk=no]\getbuffer[cmyk]\setupcolors[cmyk=yes]}

\stopsection

% \startsection[title={Common definitions}]
%
% \index{inclusions}
% \index{common definitions}
%
% When using many graphics, there is a chance that they share common definitions.
% Such shared components can be defined by:
%
% \starttyping
% \startMPinclusions
%   color mycolor ; mycolor := .625red ;
% \stopMPinclusions
% \stoptyping
%
% {\em The following is only true for \CONTEXT\ \MKII ! Users of \MKIV\ can skip
% this section.}
%
% All \METAPOST\ graphics defined in the document end up in the files \type
% {mpgraph.mp} and \type {mprun.mp}. When processed, they produce (sometimes many)
% graphic files. When you use \CONTEXT\ \MKII\ and \TEXEXEC\ to process documents,
% these two files are processed automatically after a run so that in a next run,
% the right graphics are available.
%
% When you are using the \type {web2c} distribution, \CONTEXT\ can call \METAPOST\
% at runtime and thereby use the right graphics instantaneously. In order to use
% this feature, you have to enable \type {\write18} in the file \type {texmf.cnf}.
% Also, in the file \type {cont-sys.tex}, that holds local preferences, or in the
% document source, you should say:
%
% \starttyping
% \runMPgraphicstrue
% \stoptyping
%
% This enables runtime generation of graphics using the low level \TEX\ command
% \type {\write18}. First make sure that your local brand of \TEX\ supports this
% feature. A simple test is making a \TEX\ file with the following line:
%
% \starttyping
% \immediate\write18{echo It works}
% \stoptyping
%
% If this fails, you should consult the manual that comes with your system, locate
% an expert or ask around on the \CONTEXT\ mailing list. Of course you can also
% decide to let \TEXEXEC\ take care of processing the graphics afterwards. This has
% the advantage of being faster but has the disadvantage that you need additional
% \TEX\ runs.
%
% If you generate the graphics at run time, you should consider to turn on graphic
% slot recycling, which means that you often end up with fewer intermediate files:
%
% \starttyping
% \recycleMPslotstrue
% \stoptyping
%
% There are a few more low level switches and features, but these go beyond the
% purpose of this manual. Some of these features, like the option to add tokens to
% \type {\everyMPgraphic} are for experts only, and fooling around with them can
% interfere with existing features.
%
% \stopsection

\startsection[title={One page graphics}]

An advantage of using \CONTEXT\ to make your \METAPOST\ graphics is you don't
have to bother about specials, font inclusion and all those nasty things that can
spoil a good day. An example of such a graphic is the file \type {mfun-800} that
resides on the computer of the author.

\typefile{mfun-800}

Given that \CONTEXT\ is present on your system, you can process this file with:

\starttyping
context mfun-800
\stoptyping

You can define many graphics in one file. Later you can include individual pages
from the resulting \PDF\ file in your document:

\startbuffer
\placefigure
  {A silly figure, demonstrating that stand||alone||graphics
   can be made.}
  {\typesetfile[mfun-800.tex][page=1]}
\stopbuffer

\typebuffer

In this case the \type {page=1} specification is not really needed. You can scale
and manipulate the figure in any way supported by the macro package that you use.

\getbuffer

\stopsection

\startsection[title={Managing resources}]

A graphic consists of curves, either or not filled with a given color. A graphic
can also include text, which means that fonts are used. Finally a graphic can
have special effects, like a shaded fill. Colors, fonts and special effects go
under the name resources, since they may demand special care or support from the
viewing or printing device.

% When fonts are used, a \METAPOST\ file is not self contained. This means that the
% postprocessing program has to deal with the fonts. In \CONTEXT, the special
% driver |<|and \PDFTEX\ support is considered as such|>| takes care of this.

Special effects, like shading, are supported by dedicated \METAPOST\ modules.
These are included in the \CONTEXT\ distribution and will be discussed later in
\in {chapter} [sec:effects].

Since \METAPOST\ supports color, an embedded graphic can be rather colorful.
However, when color support is disabled or set up to convert colors to gray
scales, \CONTEXT\ will convert the colors in the graphics to gray scales.

\startbuffer[circle]
  colorcircle(4cm,red,green,blue) ;
\stopbuffer

\startbuffer
\startcombination[3*1]
  {\setupcolors[state=start]\processMPbuffer[circle]}          {full color}
  {\setupcolors[state=stop]\processMPbuffer[circle]}           {weighted gray}
  {\setupcolors[state=stop,factor=no]\processMPbuffer[circle]} {linear gray}
\stopcombination
\stopbuffer

You may wonder what the advantage is of weighted gray conversion. \in {Figure}
[fig:color circles] shows the difference between natural colors, weighted gray
scales and straightforward, non||weighted, gray scales.

\placefigure
  [here][fig:color circles]
  {The advantage of weighted gray over linear gray.}
  {\getbuffer\setupcolors[state=start,factor=yes]} % just to be sure

When we convert color to gray, we use the following formula. This kind of
conversion also takes place in black and white televisions.

\placeformula [-]
  \startformula
    G = .30r + .59g + .11b
  \stopformula

\in {Section} [sec:conversion] introduces the \type {grayed} operation that you
can use to convert a colored picture into a gray one. This macro uses the same
conversion method as mentioned here.

\stopsection

\startsection[title={Instances}]

There are a few instances predefined and if you want to isolate your own
graphics from whatever \CONTEXT\ itself cooks up, you can define more as the
extra overhead can be neglected.

\starttabulate[|T|T|T|T|T|]
\BC name        \BC format  \BC extensions \BC initializations \BC method  \NC \NR
\NC metafun     \NC metafun \NC yes        \NC yes             \NC         \NC \NR
\NC extrafun    \NC metafun \NC yes        \NC yes             \NC         \NC \NR
\NC lessfun     \NC metafun \NC            \NC                 \NC         \NC \NR
\NC doublefun   \NC metafun \NC yes        \NC yes             \NC double  \NC \NR
\NC binaryfun   \NC metafun \NC yes        \NC yes             \NC binary  \NC \NR
\NC decimalfun  \NC metafun \NC yes        \NC yes             \NC decimal \NC \NR
\stoptabulate

According to this the \type {doublefun} instance is defined as:

\starttyping
\defineMPinstance
  [doublefun]
  [format=metafun,
   extensions=yes,
   initializations=yes,
   method=double]
\stoptyping

The \type {extensions} key relates to:

\starttyping
\startMPextensions
    % some code
\stopMPextensions
\stoptyping

that are used to pass (common) extensions to the instance. The \type
{initializations} key relates to:

\starttyping
\startMPinitializations
    % some code
\stopMPinitializations
\stoptyping

that are used to communicate \TEX\ properties to the instance (they are
expanded each graphic). Instance bound definitions can be set with:

\starttyping
\startMPdefinitions{doublefun}
    % some code
\stopMPdefinitions
\stoptyping

We do have more instances, for instance for the chemical subsystem. If you load
the \type {graph} module you get a double precision \type {graph} instance. We might
use more private ones in the future.

When you make graphic pages, you can do this:

\starttyping
\startMPpage[instance=doublefun]
    % some code
\stopMPpage
\stoptyping

When you use the other commands you can optionally specify an instance:

\startbuffer[metafun]
\startMPcode{metafun}
    draw textext(decimal pi) scaled 2 withcolor .625red ;
    draw boundingbox currentpicture enlarged 2pt ;
\stopMPcode
\stopbuffer

\startbuffer[extrafun]
\startMPcode{extrafun}
    draw textext(decimal pi) scaled 2 withcolor .625green ;
    draw boundingbox currentpicture enlarged 2pt  ;
\stopMPcode
\stopbuffer

\startbuffer[doublefun]
\startMPcode{doublefun}
    draw textext(decimal pi) scaled 2 withcolor .625blue ;
    draw boundingbox currentpicture enlarged 2pt  ;
\stopMPcode
\stopbuffer

\startbuffer[binaryfun]
\startMPcode{binaryfun}
    draw textext(decimal pi) scaled 2 withcolor .625yellow ;
    draw boundingbox currentpicture enlarged 2pt  ;
\stopMPcode
\stopbuffer

\startbuffer[decimalfun]
\startMPcode{decimalfun}
    draw textext(decimal pi) scaled 2 withcolor .375white ;
    draw boundingbox currentpicture enlarged 2pt  ;
\stopMPcode
\stopbuffer

\typebuffer[metafun,extrafun,doublefun,binaryfun,decimalfun]

The result is shown in \in {figure} [fig:instances] and as expected there
is a different value for $\pi$ reported.

\startplacefigure[reference=fig:instances,title={Instances can use different number systems.}]
    \startcombination[1*5]
        {\getbuffer[metafun]}    {metafun}
        {\getbuffer[extrafun]}   {extrafun}
        {\getbuffer[doublefun]}  {doublefun}
        {\getbuffer[binaryfun]}  {binaryfun}
        {\getbuffer[decimalfun]} {decimalfun}
    \stopcombination
\stopplacefigure

You need to be aware of the fact that the results of a (for instance) double
instance can differ from a scaled (the default) one. As long as graphics can be
processed in both models (which is the case as long as the dimensions stay below
4096 base points) the outcome is probably not that different. However, we've seen
that the accuracy of for instance $\pi$ (just a variable) differs. I like to use
random values and the random generators are definitely different: each of the
number libraries has its own implementation. Let's look at that. We define two
random graphic generators:

\startbuffer
\startuseMPgraphic{normaldeviate}
    randomseed := 100 ;
    draw fullsquare
        withpen pencircle scaled 1/200
        withcolor .5white ;
    for i=1 upto 500 :
        draw (normaldeviate,normaldeviate)
            scaled 1/3
            withpen pencircle scaled 1/30
            withtransparency (1,.5) ;
    endfor ;
    setbounds currentpicture to
        boundingbox fullcircle
        enlarged 1/2 ;
    currentpicture :=
        currentpicture
        xsized (2TextWidth/5) ;
\stopuseMPgraphic
\stopbuffer

\typebuffer \getbuffer

And:

\startbuffer
\startuseMPgraphic{uniformdeviate}
    randomseed := 100 ;
    draw fullsquare
        withpen pencircle scaled 1/200
        withcolor .5white ;
    for i=1 upto 500 :
        draw (-1/2 + uniformdeviate 1,-1/2 + uniformdeviate 1)
            withpen pencircle scaled 1/30
            withtransparency (1,.5) ;
    endfor ;
    setbounds currentpicture to
        boundingbox fullcircle
        enlarged 1/2 ;
    currentpicture :=
        currentpicture
        xsized (2TextWidth/5) ;
\stopuseMPgraphic
\stopbuffer

\typebuffer \getbuffer

We show the results for a normaldeviate in \in {figure} [fig:random:1] \in {upto}
[fig:random:4] you see the randomizers per number system. In \in {figure}
[fig:random:compared] we demonstrate that the scaled version has its own variant.

\unexpanded\def\TestRandomFun#1#2#3%
  {\startMPcode{#1}
      draw image(\includeMPgraphic{#2})
          withcolor #3 ;
   \stopMPcode}

\startplacefigure[reference=fig:random:1,title={The scaled randomizers.}]
    \pushrandomseed
        \startcombination
            {\TestRandomFun{metafun}   {normaldeviate} {darkred}}    {normaldeviate}
            {\TestRandomFun{metafun}   {uniformdeviate}{darkred}}    {uniformdeviate}
        \stopcombination
    \poprandomseed
\stopplacefigure

\startplacefigure[reference=fig:random:2,title={The double randomizers.}]
    \pushrandomseed
        \startcombination
            {\TestRandomFun{doublefun} {normaldeviate} {darkgreen}}  {normaldeviate}
            {\TestRandomFun{doublefun} {uniformdeviate}{darkgreen}}  {uniformdeviate}
        \stopcombination
    \poprandomseed
\stopplacefigure

\startplacefigure[reference=fig:random:3,title={The decimal randomizers.}]
    \pushrandomseed
        \startcombination
            {\TestRandomFun{decimalfun}{normaldeviate} {darkblue}}   {normaldeviate}
            {\TestRandomFun{decimalfun}{uniformdeviate}{darkblue}}   {uniformdeviate}
        \stopcombination
    \poprandomseed
\stopplacefigure

\startplacefigure[reference=fig:random:4,title={The binary randomizers.}]
    \pushrandomseed
        \startcombination
            {\TestRandomFun{binaryfun} {normaldeviate} {darkyellow}} {normaldeviate}
            {\TestRandomFun{binaryfun} {uniformdeviate}{darkyellow}} {uniformdeviate}
        \stopcombination
    \poprandomseed
\stopplacefigure

\startplacefigure[reference=fig:random:compared,title={Normaldeviate and uniformdeviate overlayed.}]
    \pushrandomseed
        \startcombination[2*1]
            \bgroup
                \startoverlay
                    {\TestRandomFun{metafun}   {normaldeviate} {darkred}}
                    {\TestRandomFun{doublefun} {normaldeviate} {darkgreen}}
                    {\TestRandomFun{decimalfun}{normaldeviate} {darkblue}}
                    {\TestRandomFun{binaryfun} {normaldeviate} {darkyellow}}
                \stopoverlay
            \egroup {normaldeviate}
            \bgroup
                \startoverlay
                    {\TestRandomFun{metafun}   {uniformdeviate}{darkred}}
                    {\TestRandomFun{doublefun} {uniformdeviate}{darkgreen}}
                    {\TestRandomFun{decimalfun}{uniformdeviate}{darkblue}}
                    {\TestRandomFun{binaryfun} {uniformdeviate}{darkyellow}}
                \stopoverlay
            \egroup {uniformdeviate}
        \stopcombination
    \poprandomseed
\stopplacefigure

\stopsection

\stopchapter

\stopcomponent