summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/colors/colors-basics.tex
blob: 3832501c5d2c36660ab071dc27ab554b8542abdf (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
% language=uk

\startcomponent colors-basics

\environment colors-environment

\usemodule[setups-basics] \loadsetups[i-color]

\startchapter[title=Basics][color=darkred]

\startsection[title=Color models]

When you work with displays, and most of us do, the dominant color model is \RGB.
As far as I know \CMYK\ electrowetting displays are still not in production and
even there the \CMYK\ seems to have made place for \RGB\ (at least in promotion
movies). This is strange since where \RGB\ is used in cases where colors are
radiated, \CMYK\ shows up in reflective situations (and epub readers are just
that). But \RGB\ and \CMYK\ being complementary is not the only difference:
\CMYK\ has an explicit black channel, and as a consequence you cannot go from one
to the other color space without loss.

In print \CMYK\ is dominant but in order to get real good colors you can go with
spot colors. The ink is not mixed with others but applied in more or less
quantity. A mixture of spot colors and \CMYK\ is used too. You can combine spot
colors into a so called multitone color. Often spot colors have names (for
instance refering to Pantone) but they always have a specification in another
color space in order to be shown on screen. Think of \quotation {gold} being a
valid ink, but hard to render on screen, so some yellowish replacement is used
there when documents get prepared on screen.

In \CONTEXT\ all these models are supported, either or not at the same time. In \MKII\
you had to turn on color support explicitly, if only because of the impact of the
overhead on performance, but in \MKIV\ color is on by default. You can disable it
with:

\starttyping
\setupcolors
  [state=stop]
\stoptyping

The three mentioned models are controlled by keys, and by default we have set:

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

Spot colors and their combinations in multitone colors are controlled by the same
parameter. You can define colors in the \HSV\ color space but in the end these
become and behave like \RGB.

\stopsection

\startsection[title=Using color]

Normally you will use colors grouped. Most environments accept a \type {color}
parameter (some have \type {textcolor} or similar longer names too). In a running
text you can use:

\starttyping
\color[red]{This will show up red.}
\stoptyping

or:

\starttyping
\startcolor[red]
    This will show up red.
\stopcolor
\stoptyping

In case you don't want the grouping you can use:

\starttyping
\directcolor[red]
\stoptyping

You can even use:

\starttyping
\colored[r=0.5]{also red}
\stoptyping

In which case an anonymous color is used. An ungrouped variant of this is:

\starttyping
\directcolored[r=0.5]
\stoptyping

You will seldom use these direct variants, but they might come in handy when you
write macros yourself where extra grouping starts interfering. In fact, it often
makes sense to use a bit more abstraction:

\startbuffer
\definehighlight
  [important]
  [color=red]

First \highlight[important]{or} second \important {or} third.
\stopbuffer

\typebuffer

This gives: \inlinebuffer .

\stopsection

\startsection[title=Using \CMYK\ or \RGB]

When you compare colors in different color spaces, you need to be aware of the
fact that when a black component is used in \CMYK, conversion to \RGB\ might give
the same results but going back from that to \CMYK\ will look different from the
original. Also, \CMYK\ colors are often tuned for specific paper.

\startbuffer
\definecolor[demo:rgb:1][r=1.0,g=1.0]
\definecolor[demo:rgb:2][r=1.0,g=1.0,b=0.5]
\definecolor[demo:rgb:3][r=1.0,g=1.0,b=0.6]
\definecolor[demo:cmy:1][y=1.0]
\definecolor[demo:cmy:2][y=0.5]
\definecolor[demo:cmy:3][y=0.4]
\stopbuffer

\typebuffer \getbuffer

In these definitions we have no black component. In \in {figure}
[color:rgb:cmyk:1] we see how these colors translate to the other color spaces.

\startbuffer[compare]
\startbuffer
\definemeasure[demo][(\textwidth-2em)/3]
\startcombination[3*2]
    {\blackrule[width=\measure{demo},height=1cm,depth=0cm,color=demo:rgb:1]}{\infofont\colorcomponents{demo:rgb:1}}
    {\blackrule[width=\measure{demo},height=1cm,depth=0cm,color=demo:rgb:2]}{\infofont\colorcomponents{demo:rgb:2}}
    {\blackrule[width=\measure{demo},height=1cm,depth=0cm,color=demo:rgb:3]}{\infofont\colorcomponents{demo:rgb:3}}
    {\blackrule[width=\measure{demo},height=1cm,depth=0cm,color=demo:cmy:1]}{\infofont\colorcomponents{demo:cmy:1}}
    {\blackrule[width=\measure{demo},height=1cm,depth=0cm,color=demo:cmy:2]}{\infofont\colorcomponents{demo:cmy:2}}
    {\blackrule[width=\measure{demo},height=1cm,depth=0cm,color=demo:cmy:3]}{\infofont\colorcomponents{demo:cmy:3}}
\stopcombination
\stopbuffer

\startcombination[1*4]
    {\setupcolors[rgb=yes,cmyk=yes]\getbuffer} {Both \RGB\ and \CMYK\ enabled}
    {\setupcolors [rgb=no,cmyk=yes]\getbuffer} {Only \CMYK\ enabled.}
    {\setupcolors [rgb=yes,cmyk=no]\getbuffer} {Only \RGB\ enabled.}
    {\setupcolors  [rgb=no,cmyk=no]\getbuffer} {Both \RGB\ and \CMYK\ disabled.}
\stopcombination
\stopbuffer

\startplacefigure[location=page,reference=color:rgb:cmyk:1,title={What happens when we disable color spaces.}]
    \getbuffer[compare]
\stopplacefigure

\startbuffer
\definecolor[demo:rgb:1][r=0.5,g=0.6,b=0.7]
\definecolor[demo:rgb:2][r=0.5,g=0.6,b=0.7]
\definecolor[demo:rgb:3][r=0.5,g=0.6,b=0.7]
\definecolor[demo:cmy:1][c=0.5,m=0.4,y=0.3]
\definecolor[demo:cmy:2][c=0.4,m=0.3,y=0.2,k=0.1]
\definecolor[demo:cmy:3][c=0.3,m=0.2,y=0.1,k=0.2]
\stopbuffer

\typebuffer \getbuffer

When we define the colors as above, you can see a difference between the \RGB\
and \CMYK\ values, but also between a black component versus black distributed
over the colorants. This is seen best in \in {figure} [color:rgb:cmyk:2] when we
compare the first and third colors alongside. In \in {figure} [color:rgb:cmyk:3]
you see the whole repertoire.

\startplacefigure[reference=color:rgb:cmyk:2,title={The impact of black on a \CMYK\ color.}]
\startMPcode
    fill fullsquare xyscaled (TextWidth,25mm) withcolor \MPcolor{demo:cmy:3} ;
    fill fullsquare xyscaled (TextWidth,20mm) withcolor \MPcolor{demo:cmy:1} ;
    fill fullsquare xyscaled (TextWidth,10mm) withcolor \MPcolor{demo:cmy:3} ;
    fill fullsquare xyscaled (TextWidth, 5mm) withcolor \MPcolor{demo:cmy:1} ;
\stopMPcode
\stopplacefigure

\startplacefigure[reference=color:rgb:cmyk:3,title={What happens when we disable color spaces (black component).}]
    \getbuffer[compare]
\stopplacefigure

\stopsection

\startsection[title=Conversion]

A conversion to gray happens when \RGB\ and \CMYK\ are both disabled. The following
setting forces conversion. It disables both \RGB\ and \CMYK:

\starttyping
\setupcolors
  [conversion=always]
\stoptyping

The default setting is \type {yes} which means that colors will be reduced to gray in
the backend. This is an optimization which can result in slightly smaller output:

\starttabulate[|l|c|l|]
\NC          \NC \bf \# \NC \bf \PDF\ sequence                  \NC \NR
\NC \bf cmyk \NC 23     \NC \tttf 0 0 0 0.5 k 0 0 0 0.5 K       \NC \NR
\NC \bf rgb  \NC 29     \NC \tttf 0.5 0.5 0.5 rg 0.5 0.5 0.5 RG \NC \NR
\NC \bf gray \NC 11     \NC \tttf 0.5 g 0.5 G                   \NC \NR
\stoptabulate

The conversion to gray is controlled by:

\starttyping
\setupcolors
  [factor=yes]
\stoptyping

Like \type {conversion} the \type {factor} is a global setting. You can play with the factor values.
The default (\type {yes}) uses the factors used by color television:

\startformula
    s = 0.30r + 0.59g + 0.11b
\stopformula

In \in {figure} [color:demo:gray] we demonstrate what happens when you use different
values. Normally you won't change the defaults but for experimenting we do provide
the option:

\starttyping
\setupcolors
  [factor=0.20:0.40:0.40]
\stoptyping

There is one pitfall. Colors are finalized per page and as this is a backend
feature the value current when a page is shipped out is used. An exception are
\METAPOST\ graphics, as they have local resources and are finalized immediately.
This is hardly a limitation because one will never set these numbers in the
middle of a document.

\starttexdefinition color:demo:gray:do
    \definecolor[demo:rgb:0][r=0.5,g=0.6,b=0.7]
  % \blackrule[width=4cm,height=1cm,color=demo:rgb:0]
    \lower.4\strutdp\hbox\bgroup
        \startMPcode
            fill unitsquare xyscaled (.5TextWidth,.9LineHeight)
                withcolor "demo:rgb:0";
        \stopMPcode
    \egroup
\stoptexdefinition

\starttexdefinition color:demo:gray #1#2#3
    \NC \tttf #1 \NC \tttf #2  \NC \tttf #3 \NC
        \setupcolors[factor={#1:#2:#3}]
        \setupcolors[rgb=no,cmyk=no]
        \texdefinition{color:demo:gray:do}
        \setupcolors[factor=yes]
    \NC \NR
\stoptexdefinition

\startplacefigure[reference=color:demo:gray,title={Color to gray conversion using factors.}]
\starttabulate[|c|c|c|c|]
\NC \bf r \NC \bf g \NC \bf b \NC \bf gray \NC \NR
\NC       \NC       \NC       \NC \texdefinition{color:demo:gray:do} \NC \NR
\texdefinition{color:demo:gray}{0.30}{0.59}{0.11}
\texdefinition{color:demo:gray}{0.30}{0.11}{0.59}
\texdefinition{color:demo:gray}{0.59}{0.30}{0.11}
\texdefinition{color:demo:gray}{0.11}{0.30}{0.59}
\texdefinition{color:demo:gray}{0.11}{0.59}{0.30}
\texdefinition{color:demo:gray}{0.59}{0.11}{0.30}
\texdefinition{color:demo:gray}{0.20}{0.40}{0.40}
\texdefinition{color:demo:gray}{0.30}{0.60}{0.10}
\texdefinition{color:demo:gray}{0.20}{0.30}{0.20}
\texdefinition{color:demo:gray}{0.40}{0.40}{0.40}
\stoptabulate
\stopplacefigure

\stopsection

% let's take a break and watch: https://www.youtube.com/watch?v=baLrrW3El-M

\startsection[title=Definitions]

The mostly used color definition command is \type {\definecolor}. Here we define the
primary colors:

\startbuffer
\definecolor [red]     [r=1]
\definecolor [green]   [g=1]
\definecolor [blue]    [b=1]
\definecolor [yellow]  [y=1]
\definecolor [magenta] [m=1]
\definecolor [cyan]    [c=1]
\stopbuffer

\typebuffer \getbuffer

These can be visualized as follows:

\startbuffer
\showcolorcomponents[red,green,blue,yellow,magenta,cyan,black]
\stopbuffer

\typebuffer \getbuffer

Transparency is included in these tables but is, as already noted, in fact
independent. It can be defined with a color:

\startbuffer
\definecolor [t:red]   [r=1,a=1,t=.5]
\definecolor [t:green] [g=1,a=1,t=.5]
\definecolor [t:blue]  [b=1,a=1,t=.5]
\stopbuffer

\typebuffer \getbuffer

This time the transparency values show up too:

\showcolorcomponents[t:red,t:green,t:blue]

Because transparency is separated from color, we can define transparent behaviour
as follows:

\startbuffer
\definecolor[t:only] [a=1,t=.5]
\stopbuffer

\typebuffer \getbuffer

\startbuffer
    \dontleavehmode
    \blackrule[width=4cm,height=1cm,color=darkgreen]%
    \hskip-2cm
    \color[t:only]{\blackrule[width=4cm,height=1cm,color=darkred]}%
    \hskip-2cm
    \color[t:only]{\blackrule[width=4cm,height=1cm]}
\stopbuffer

\typebuffer

We skip back to create an overlay, so we get:

\startlinecorrection
\getbuffer
\stoplinecorrection

In the section about transparency a bit more will be said about the relation between
color and transparencies and how to cheat.

As soon as you need to typeset something for professional printing, spot colors
will show up so they are supported too. A spot color is not really a color but
related to the substance that gets put on the paper. This can be ink but also
something metallic, like silver, gold or some texture. In these cases we need
something to represent it when not printed on a suitable device so again we end
up with a color. This is reflected in the way spot colors are set up.

\startbuffer
\definecolor     [parentspot]               [r=.5,g=.2,b=.8]
\definespotcolor [childspot-1] [parentspot] [p=.7,e=fancy]
\definespotcolor [childspot-2] [parentspot] [p=.4]
\stopbuffer

\typebuffer \getbuffer

The three colors, two of them are spot colors, show up as follows:

\setupcolors[spot=yes]

\showcolorcomponents[parentspot,childspot-1,childspot-2]

The \type {p} is comparable to the \type {s} in gray scales. The \type {e}
parameter can be used to specify a name for the color. In the \PDF\ file that
name will become the separation name (a popular commercial naming scheme is
Pantone).

A combination of spotcolor is called a multitone color. These are defined as
follows (we also define a few spotcolors and use transparency):

\startbuffer
\definespotcolor [spotone]   [darkred]   [p=1]
\definespotcolor [spottwo]   [darkgreen] [p=1]

\definespotcolor [spotone-t] [darkred]   [a=1,t=.5]
\definespotcolor [spottwo-t] [darkgreen] [a=1,t=.5]

\definemultitonecolor
    [whatever]
    [spotone=.5,spottwo=.5]
    [b=.5]

\definemultitonecolor
    [whatever-t]
    [spotone=.5,spottwo=.5]
    [b=.5]
    [a=1,t=.5]
\stopbuffer

\typebuffer \getbuffer

\showcolorcomponents[spotone,spottwo,spotone-t,spottwo-t,whatever,whatever-t]

Transparencies combine as follows:

\startbuffer
\blackrule[width=3cm,height=1cm,color=spotone-t]\hskip-1.5cm
\blackrule[width=3cm,height=1cm,color=spotone-t]
\stopbuffer

\typebuffer

\startlinecorrection
\dontleavehmode\getbuffer
\stoplinecorrection

In case you want to specify colors in the \HSV\ color space, you can do that too.
The hue parameter (\type {h}) is in degrees and runs from 0 upto 360 (larger
values get divided). The saturation (\type {s}) and value (\type {v}) parameters
run from 0 to 1. The \type {v} parameter is mandate. In \in {figure} [fig:hsv] we
show what the last two variables do.

\starttyping
\definecolor[somecolor][h=125,s=0.5,v=0.8]
\stoptyping

\startMPextensions
    vardef hsvcircle(expr s, v) =
        image ( save p ; path p ; p := fullcircle ;
            save l ; numeric l ; l := length(p)/360 ;
            for i=0 upto 360 :
                fill origin -- subpath (i*l,(i+1)*l) of p -- cycle withcolor hsvtorgb(i,s,v) ;
            endfor ;
        )
    enddef;
\stopMPextensions

\startplacefigure[reference=fig:hsv,title={Four hsv color circle running from 0 to 360 degrees, with zero at the right.}]
    \startcombination[4*1]
        {\startMPcode draw hsvcircle(0.6,0.6) scaled 3cm ; \stopMPcode} {\tttf s=0.6 v=0.6}
        {\startMPcode draw hsvcircle(0.4,0.6) scaled 3cm ; \stopMPcode} {\tttf s=0.4 v=0.6}
        {\startMPcode draw hsvcircle(0.6,0.4) scaled 3cm ; \stopMPcode} {\tttf s=0.6 v=0.4}
        {\startMPcode draw hsvcircle(0.4,0.4) scaled 3cm ; \stopMPcode} {\tttf s=0.4 v=0.4}
    \stopcombination
\stopplacefigure

% \scratchdimen\dimexpr(\textwidth-118pt)/120\relax
% \dontleavehmode \dostepwiserecurse{0}{359}{1}{%
%     \definecolor[tempcolor][h=#1,s=0.6,v=0.6]%
%     \blackrule[color=tempcolor,width=\scratchdimen,height=1ex,depth=1ex]%
%     \hskip1pt
% }

If you need to use hexadecimal color specifications you can use these
definitions:

\starttyping
\definecolor[mycolor][x=4477AA]
\definecolor[mycolor][h=4477AA]
\definecolor[mycolor][x=66]
\definecolor[mycolor][#4477AA]
\stoptyping

The \type {#} is normally not accepted in \TEX\ source code but when you get the
specification from elsewhere (e.g.\ \XML) it can be convenient.

\stopsection

\startsection[title=Freezing colors]

We can clone colors and thereby overload color dynamically. You can however
freeze colors via the setup option \type {expansion}.

\startbuffer
\definecolor[green]   [r=.5]{({\green green ->   red})}
\definecolor[green]   [g=.5]{({\green green -> green})}
\definecolor[green]   [blue]{({\green green ->  blue})}
\definecolor[blue]     [red]{({\green green ->   red})}
\setupcolors[expansion=yes]%
\definecolor[blue]     [red]%
\definecolor[green]   [blue]%
\definecolor[blue]    [r=.5]{({\green green ->  blue})}
\stopbuffer

\typebuffer

\start
\definecolor[red]  [r=.5]%
\definecolor[green][g=.5]%
\definecolor[blue] [b=.5]%
\getbuffer
\stop

\stopsection

\startsection[title=Color groups]

Nowadays we seldom use colorgroups but they are still supported. Groups are
collections of distinctive colors, something we needed in projects where many
graphics had to be made and consistency between text and image colors was
important. The groups can be translated into similar collections in drawing
programs used at that time.

\startbuffer
\definecolorgroup
  [redish]
  [1.00:0.90:0.90, % 1
   1.00:0.80:0.80, % 2
   1.00:0.70:0.70, % 3
   1.00:0.55:0.55, % 4
   1.00:0.40:0.40, % 5
   1.00:0.25:0.25, % 6
   1.00:0.15:0.15, % 7
   0.90:0.00:0.00] % 8
\stopbuffer

\typebuffer \getbuffer

The redish color is called by number:

\startbuffer
\blackrule[width=3cm,height=1cm,depth=0pt,color=redish:1]\quad
\blackrule[width=3cm,height=1cm,depth=0pt,color=redish:2]\quad
\blackrule[width=3cm,height=1cm,depth=0pt,color=redish:3]
\stopbuffer

\typebuffer

The number of elements is normally limited and eight is about what is useful and
still distinguishes good enough when printed in black and white.

\startlinecorrection
\dontleavehmode\getbuffer
\stoplinecorrection

\startlinecorrection
\setupcolors[rgb=no,cmyk=no]\dontleavehmode\getbuffer
\stoplinecorrection

\stopsection

\startsection[title=Palets]

Color palets are handy when you want to use a set of (named) colors but
also want to switch efficiently between different definitions:

\startbuffer
\definepalet
  [standard]
  [darkred=darkcyan,
   darkgreen=darkmagenta,
   darkblue=darkyellow]
\stopbuffer

\typebuffer \getbuffer

The \type {\setuppalet} commands switches to a palet. When a requested color is
not part of a palet, a regular lookup happens. This is used as:

\startbuffer
\blackrule[width=15mm,height=10mm,depth=0mm,color=darkred]\quad
\blackrule[width=15mm,height=10mm,depth=0mm,color=darkgreen]\quad
\blackrule[width=15mm,height=10mm,depth=0mm,color=darkblue]\quad
\setuppalet[standard]%
\blackrule[width=15mm,height=10mm,depth=0mm,color=darkred]\quad
\blackrule[width=15mm,height=10mm,depth=0mm,color=darkgreen]\quad
\blackrule[width=15mm,height=10mm,depth=0mm,color=darkblue]
\stopbuffer

\typebuffer

\startlinecorrection
\dontleavehmode \getbuffer
\stoplinecorrection

Here we use color names but often you end up with more symbolic names:

\starttyping
\definepalet
  [standard]
  [important=darkred,
   notabene=darkgreen,
   warning=darkyellow]
\stoptyping

As with the regular color commands, the palet mechanism is an old one but it is
well integrated. Instead of inheriting you can also use definitions:

\starttyping
\definepalet
  [standard]
  [important={r=.5},
   notabene={g=.5},
   warning={r=.5,g=.5}]
\stoptyping

\stopsection

\startsection[title=Transparency]

We already discussed transparency as part of colors. In most cases we will choose
type {normal} (or \type {1}) as transparency type, but there are more:

\startluacode
    local codes = table.swapped(attributes.transparencies.names)

    context.startcolumns { n = 4 }
    context.starttabulate ( { "|B||" }, { keeptogether = "no" } )
    for i=0,#codes do
        context.NC()
        context(i)
        context.NC()
        context(codes[i])
        context.NC()
        context.NR()
    end
    context.stoptabulate()
    context.stopcolumns()
\stopluacode

In \in {figure} [fig:transparency] we compare these variants. Not all are as
effective as their effect depends on several factors. You can read more about it
in the \PDF\ specification.

\startMPextensions
    vardef transdemo(expr b)  =
        image ( save p ; path p ; p := fullcircle ;
            save l ; numeric l ; l := length(p)/16 ;
            if (b = 1) or (b = 3) :
                fill fullcircle scaled 1.00 withcolor yellow ;
                fill fullcircle scaled 0.80 withcolor red ;
                fill fullcircle scaled 0.60 withcolor black ;
                fill fullcircle scaled 0.40 withcolor white ;
                fill fullcircle scaled 0.20 withcolor .5white ;
            fi ;
            if (b = 2) or (b = 3) :
                for i=0 upto 15 :
                    fill origin -- subpath (i*l,(i+1)*l) of p -- cycle
                        withcolor red
                        withtransparency (i,0.5) ;
                    draw textext("\ttbf " & decimal i) ysized .04 shifted (.45,0) rotated (i*360/16 + 360/32) ;
                endfor ;
            fi ;
        )
    enddef;
\stopMPextensions

\startplacefigure[reference=fig:transparency,title={The differences between the transparency options. The center is explicitly filled with white.}]
    \startcombination[3*1]
        {\startMPcode draw transdemo(1) scaled 4cm ; \stopMPcode} {\tttf backgrounds used}
        {\startMPcode draw transdemo(2) scaled 4cm ; \stopMPcode} {\tttf transparencies used}
        {\startMPcode draw transdemo(3) scaled 4cm ; \stopMPcode} {\tttf transparency overlayed}
    \stopcombination
\stopplacefigure

Colors and transparencies are coupled by definitions. We will explain this by
some examples. When we say:

\startbuffer
\definecolor[clr1][r=.5]
\stopbuffer

\typebuffer \getbuffer

A non|-|transparent color is defined and when we say:

\startbuffer
\definecolor[clr2][g=.5,a=1,t=.5]
\stopbuffer

\typebuffer \getbuffer

We defined a color with a transparency. However, color and transparency get separated
attributes. So when we nest them as in:

\startbuffer
\color[clr1]{\bf RED   \color[clr2] {GREEN}}
\color[clr2]{\bf GREEN \color[clr1] {RED}  }
\stopbuffer

\typebuffer

we get:

\startlines \getbuffer \stoplines

The transparency of the outer color is also applied to the inner color. If you
don't want that, you explicitly need to set them:

\startbuffer
\definecolor[clr3][b=.5,a=1,t=1]
\stopbuffer

\typebuffer \getbuffer

\startbuffer
\color[clr1]{\bf RED   \color[clr2] {GREEN} \color[clr3]{BLUE} }
\color[clr2]{\bf GREEN \color[clr1] {RED}   \color[clr2]{GREEN}}
\color[clr3]{\bf BLUE  \color[clr1] {RED}   \color[clr2]{GREEN}}
\stopbuffer

\typebuffer

we get:

\startlines \getbuffer \stoplines

If you define a transparent|-|only color, you get transparent black:

\startbuffer
\definecolor[clr4][a=1,t=.5]
\stopbuffer

\typebuffer \getbuffer

So:

\startbuffer
\color[clr1]{\bf RED    \color[clr4] {RED}}
\color[clr4]{\bf BLACK  \color[clr1] {RED}}
\stopbuffer

\typebuffer

gives:

\startlines \getbuffer \stoplines

In addition to the already discussed definers and setters we also have a few
special ones. Personally I never needed them but they are the for completeness.

\startbuffer
\definetransparency[tsp1][a=1,t=.5]
\stopbuffer

\typebuffer \getbuffer

We apply this to some text:

\startbuffer
\color      [clr1]{\bf RED   \transparent[tsp1] {RED}   }
\transparent[tsp1]{\bf BLACK \color      [clr1] {RED}   }
\transparent[tsp1]{\bf BLACK \transparent[reset]{BLACK} }
\stopbuffer

\typebuffer

and get:

\startlines \getbuffer \stoplines

We can also only switch color:

\startbuffer
\color[clr1]{\bf RED \color    [clr2] {GREEN}}
\color[clr1]{\bf RED \coloronly[clr2] {GREEN}}
\stopbuffer

\typebuffer

So the second line has no transparency:

\startlines \getbuffer \stoplines

The \typ {\starttransparent} and \type {\startcoloronly} commands are the
complements of \typ {\transparent} and \typ {\coloronly}.

\stopsection

\startsection[title=Interpolation]

You can define intermediate colors in a way comparable with \METAPOST\ \type
{.5[red,green]} kind of specifications. Here are some examples:

\startbuffer
\definecolor [mycolor1] [.5(red,green)]
\definecolor [mycolor2] [.8(red,green)]
\definecolor [mycolor3] [.4(red,white)]
\definecolor [mycolor4] [.4(white,red)]

\showcolorcomponents[red,green,mycolor1,mycolor2,mycolor3,mycolor4]
\stopbuffer

\typebuffer \getbuffer

An older method, still available is:

\starttyping
\defineintermediatecolor[mycolor5][0.5,red,green]
\stoptyping

A variation on this are complementary colors:

\startbuffer
\definecolor[mycolor1][.5(blue,red)]
\definecolor[mycolor2][-.5(blue,red)]
\definecolor[mycolor3][-(blue)]
\definecolor[mycolor4][-(red)]

\showcolorcomponents[blue,red,mycolor1,mycolor2,mycolor3,mycolor4]
\stopbuffer

\typebuffer \getbuffer

\stopsection

\startsection[title=PDF]

Although it is not perfect, \PDF\ evolved in such a way that it will stay around
for a while. One reason is that it has become a standard, or more precisely a set
of standards. Depending on what variant you choose color support is limited.

\startluacode

local function state(v)
    context.NC()
    if v then
        context("\\star")
    end
end

context.starttabulate { "|l|c|c|c|c|c|" }
context.NC() context.bold("format")
context.NC() context.bold("gray")
context.NC() context.bold("rgb")
context.NC() context.bold("cmyk")
context.NC() context.bold("spot")
-------.NC() context.bold("multi")
context.NC() context.bold("transparency")
context.NC() context.NR()
for name, data in table.sortedhash(lpdf.formats.data) do
    context.NC() context.type(name)
    state(data.gray_scale)
    state(data.rgb_colors)
    state(data.cmyk_colors)
    state(data.spot_colors)
 -- state(data.nchannel_colorspace)
 -- state(data.calibrated_rgb_colors)
 -- state(data.cielab_colors)
    state(data.transparency)
 -- state(data.include_intents)
 -- state(data.internal_icc_profiles)
 -- state(data.external_icc_profiles)
    context.NR()
end
context.stoptabulate()

\stopluacode

When you have set the \type{format} with \type {\setupbackend} to one of the
known formats mentioned in the previous table, the color conversions will
automatically kick in.

\stopsection

\startsection[title=Unboxing]

This paragraph is somewhat complex, so skip it when you don't feel comfortable
with the subject or when you've never seen low level \CONTEXT\ code.

Colors are implemented using attributes. Attributes behave like fonts. This means
that they are kind of frozen once material is boxed. Consider that we define a
box as follows:

\startbuffer
\setbox0\hbox{\bf default {\darkred red \darkgreen green} default}
\stopbuffer

\typebuffer \getbuffer

What do you expect to come out the next code? In \MKII\ the \quote {default}
inside the box will be colored yellow but the internal red and and green words
will keep their color.

\startbuffer
\bf default {\darkyellow yellow {\box0} yellow} default
\stopbuffer

\typebuffer

This is what we get in \MKIV: \start \inlinebuffer \stop

When we use fonts switches we don't expect the content of the box to change. So,
in the following the \quote {default} texts will {\em not} become bold.

\startbuffer
\setbox0\hbox{default {\sl slanted \bi bold italic} default}
\stopbuffer

\typebuffer \getbuffer

\startbuffer
default {\bf bold {\box0} bold} default
\stopbuffer

\typebuffer

Now we get: \inlinebuffer .

Redoing a box with a new font is sort of tricky as by then all kind of
manipulations have been applied and the original inputs is long gone. But colors
are easier to deal with and therefore in \MKIV\ we have a trick to make sure the
outer color gets applied to the box:

\startbuffer[enable]
\enabledirectives
  [attributes.inheritance]
\stopbuffer

\startbuffer
default {\bf \darkyellow yellow {\attributedbox0} yellow} default
\stopbuffer

\typebuffer

So, we get: \start\inlinebuffer[box]\inlinebuffer[enable]\space\inlinebuffer
\stop, but only after we have enabled this mechanisms explicitly:

\typebuffer[enable]

There is also an \type {\attributedcopy} macro. These macros signal the attribute
resolver that this box is to be treated special.

In \MKII\ we have a similar situation which is why we had the option (only used
deep down in \CONTEXT) to encapsulate a bunch of code with

\starttyping
\startregistercolor[foregroundcolor]
some macro code ... here foregroundcolor is applied ... more code
\stopregistercolor
\stoptyping

This is for instance used in the \type {\framed} macro. First we package the
content, foregroundcolor is not yet applied because the injected specials of
literals can interfere badly, but by registering the colors the nested color
calls are tricked into thinking that preceding and following content is colored.
When packaged, we apply backgrounds, frames, and foregroundcolor to the whole
result. Because nested colors were aware of the foregroundcolor they have
properly reverted to this color when needed.

In \MKIV\ the situation is reversed. Here we definitely need to set the
foregroundcolor because otherwise attributes are not set. This is no problem
because contrary to \MKII\ colors don't interfere (no extra nodes). We could have
flushed the framed content using \type {\attributedbox}, but we don't want to
enable inheritance by default because it comes with some overhead.

\stopsection

\startsection[title=Color intents]

If we do this:

\startbuffer
\startoverlay
    {\blackrule[color=darkred, height=2cm,width=4cm]}
    {\blackrule[color=darkblue,height=1cm,width=3cm]}
\stopoverlay
\stopbuffer

\typebuffer

we get:

\startlinecorrection
\getbuffer
\stoplinecorrection

The blue rectangle is drawn on top of the red one. In print, normally the
printing engine will make sure that there is no red below the blue. In case of
transparent colors this is somewhat tricky because then we definitely want to see
part of what lays below.

You can control this process with the following commands:

\starttyping
\setupcolors
  [intent=...]
\stoptyping

The default setting is \type {none} but you can set the intent to \type {overprint} or
\type {knockout} as well. In a running text you can use the following commands:

\starttyping
\startcolorintent[overprint|knockout]
    ...
\stopcolorintent

\startoverprint
    ...
\stopoverprint

\startknockout
    ...
\stopknockout
\stoptyping

In practice you will probably seldom need to deal with this so best leave the defaults
as they are.

\stopsection

\startsection[title=Collections]

Collections are predefined sets of colors. You find them in the files \type
{colo-imp-*.mkiv} and you can make such files yourself. When you define a color a
command is generated by default. When you load a collection, there is a danger
that you redefine commands unintended. For that reason most collections are
wrapped in:

\starttyping
\startprotectedcolors
    % definitions
\stopprotectedcolors
\stoptyping

This prevents commands being defined and assumes that colors are accessed by
using the \type {color} parameter of setup commands or in the text with:

\starttyping
\color[somecolor]{this gets colored}

\startcolor[somecolor]
    this gets colored
\stopcolorintent
\stoptyping

The default set (\type {rgb}) is already preloaded with:

\starttyping
\usecolors[rgb] % preloaded
\stoptyping

You can get a list of colors with

\startbuffer
\showcolor[rgb]
\stopbuffer

\typebuffer

This generates the list:

\getbuffer

These are the collections shipped with \CONTEXT. Some names are

\starttabulate[|T||]
\NC crayola \NC crayon colors \NC \NR
\NC dem     \NC a demo set of groups and palets \NC \NR
\NC ema     \NC an old coming from an Emacs user \NC \NR
%NC pan     \NC a bunch of Pantone colors \NC \NR % can't be distributed
%NC pra     \NC colors used at \PRAGMA \NC \NR
\NC rainbow \NC a series of color groups by Alan \NC \NR
\NC ral     \NC a set often used in industry (from Germany) \NC \NR
\NC rgb     \NC a basic set of colors defined in the \RGB\ color space \NC \NR
%NC ros     \NC the roscolux color set used in theater and cinema lighting \NC \NR % probably can't be distributed
\NC x11     \NC (most of the) standard X11 \RGB\ colors \NC \NR
\stoptabulate

You can look in these files to see what gets defined. Even if you don't use them
they might be illustrative,

\stopsection

\startsection[title=Text color]

Setting the color of the running text is done with:

\starttyping
\setupcolors
  [textcolor=darkgray]
\stoptyping

If needed you can also set the \type {pagecolormodel} there but its default value
is \type {none} which means that it will obey the global settings.

\stopsection

\startsection[title=Tikz]

In case you use the TikZ graphical subsystem you need to be aware of the the fact
that its color support is more geared towards \LATEX. There is glue code that
binds the \CONTEXT\ color system to its internal representation but there can
still be problems. For instance, not all color systems are supported so \CONTEXT\
will try to remap, but only when it knows that it has to do so. You can best not
mix colorspaces when you use TikZ. If you really want (and there is no real
reason to do so) you can say:

\starttyping
\enabledirectives[colors.pgf]
\stoptyping

and then (at the cost of some extra overhead) define colors as:

\starttyping
\definecolor[pgfcolora][blue!50!green]
\definecolor[pgfcolorb][red!50!blue]
\stoptyping

\stopsection

\startsection[title=Implementation details]

The low level implementation of colors in \MKIV\ is fundamentally different from
\MKII. In \MKIV\ something like this happens:

\starttyping
one \color[red]{two} three
\stoptyping

becomes (with grouping):

\starttyping
one {<start color: red>two<stop color>} three
\stoptyping

the start and stop points are in fact injections in the input: a special (for
\DVI) or literals (for \PDF) is inserted that turns the color on and off, but
also information is carried along about the state of color, so that we can
properly nest as well as pick up the current color after a page break. We never
had real problems with this mechanism but one had to keep in mind that injections
like this could interfere with typesetting. This mechanism didn't rely on the
engine for housekeeping, all was done at the \TEX\ end using so called marks.

In \MKIV\ we use attributes. This means that the sequence now looks like:

\starttyping
one {<set color attribute to red>two} three
\stoptyping

The actual handling of color happens when a page is shipped out and there is no
interference with typesetting. The work is mostly done in \LUA.

Colorspaces (\RGB, \CMYK, spot) were already supported in \MKII\ and of course
still are in \MKIV. However, the colorspace is now a more independent property.
At some point in \MKII\ we also implemented transparency as a property of a
color. In \MKIV\ transparency is still defined with a color but handled
independently. This means that where in \MKII\ color is just one axis, in \MKIV\
we have three: colorspace (model), color and transparency. This of course has a
bit of a performance and memory hit, but in practice a user won't notice it.

\stopsection

\startsection[title=Grouping]

The \type {\color} and \type {\startcolor} command group their arguments. There might
be cases where this interferes with your intentions, for instance when you want to
set some variable and use its value later on.

\startbuffer
1 \scratchcounter=1
plus
1 \advance \scratchcounter by 1
equals
\the\scratchcounter
\stopbuffer

\typebuffer

The summation works out okay: {\bf \inlinebuffer}.

\startbuffer
\color[darkblue]{1 \scratchcounter=1}
plus
\color[darkblue]{1 \advance \scratchcounter by 1}
equals
\color[darkgreen]{\the\scratchcounter}
\stopbuffer

\typebuffer

Here the final result depends on the value of \type {\scratchcounter}:
{\scratchcounter 3 \bf \inlinebuffer}.

\startbuffer
\start
    \pushcolor[darkblue]1 \scratchcounter=1 \popcolor
    plus
    \pushcolor[darkblue]1 \advance \scratchcounter by 1 \popcolor
    equals
    \pushcolor[darkgreen]\the\scratchcounter \popcolor
\stop
\stopbuffer

\typebuffer

Here we get: {\bf \inlinebuffer}. The \typ {\pushcolor} and \typ {\popcolor}
commands can be used nested which give a bot of overhead. The \typ {\savecolor}
and \typ {\restorecolor} commands are variants that don't stack. They are a bit
more efficient but if you use them nested you probably also will use some
grouping. Where the push||pop pair needs to be matched, the save||restore pair
doesn't impose that restriction.

\stopsection

\startsection[title=Commands]

There are quite some commands that relate to colors but you probably only need
\typ {\definecolor}, \type {\color} and \typ {\startcolor} \unknown \typ
{\stopcolor}. Here we show the complete list. Some commands are redundant, for
instance \typ {\definenamedcolor} is the same as \typ {\definecolor}.

\placeallsetups

\stopsection

\stopchapter

\stopcomponent