summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/colo-ini.lua
blob: c074d34077bed35ddb95f0fbeae8a3b0713f04e8 (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
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
if not modules then modules = { } end modules ['colo-ini'] = {
    version   = 1.000,
    comment   = "companion to colo-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local type, tonumber = type, tonumber
local concat, insert, remove = table.concat, table.insert, table.remove
local format, gmatch, gsub, lower, match, find = string.format, string.gmatch, string.gsub, string.lower, string.match, string.find
local P, R, C, Cc = lpeg.P, lpeg.R, lpeg.C, lpeg.Cc
local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
local formatters = string.formatters

local trace_define = false  trackers.register("colors.define",function(v) trace_define = v end)
local trace_pgf    = false  trackers.register("colors.pgf",   function(v) trace_pgf    = v end)

local report_colors = logs.reporter("colors","defining")
local report_pgf    = logs.reporter("colors","pgf")

local attributes          = attributes
local backends            = backends
local storage             = storage
local context             = context
local commands            = commands

local implement           = interfaces.implement
local getnamespace        = interfaces.getnamespace

local mark                = utilities.storage.mark

local settings_to_hash_strict = utilities.parsers.settings_to_hash_strict

local colors              = attributes.colors
local transparencies      = attributes.transparencies
local colorintents        = attributes.colorintents
local registrations       = backends.registrations

local v_reset             = interfaces.variables.reset

local texsetattribute     = tex.setattribute
local texgetattribute     = tex.getattribute
local texgetcount         = tex.getcount
local texgettoks          = tex.gettoks

local a_color             = attributes.private('color')
local a_transparency      = attributes.private('transparency')
local a_colormodel        = attributes.private('colormodel')

local register_color      = colors.register
local attributes_list     = attributes.list

local colorvalues         = colors.values
local transparencyvalues  = transparencies.values

colors.sets               = mark(colors.sets or { }) -- sets are mostly used for
local colorsets           = colors.sets        -- showing lists of defined
local colorset            = { }                -- colors
colorsets.default         = colorset
local valid               = mark(colors.valid or { })
colors.valid              = valid
local counts              = mark(colors.counts or { })
colors.counts             = counts

storage.register("attributes/colors/sets",   colorsets, "attributes.colors.sets")
storage.register("attributes/colors/valid",  valid,     "attributes.colors.valid")
storage.register("attributes/colors/counts", counts,    "attributes.colors.counts")

local function currentmodel()
    return texgetattribute(a_colormodel)
end

colors.currentmodel = currentmodel

local function synccolor(name)
    valid[name] = true
end

local function synccolorclone(name,clone)
    valid[name] = clone
end

local function synccolorcount(name,n)
    counts[name] = n
end

local stack = { }

local function pushset(name)
    insert(stack,colorset)
    colorset = colorsets[name]
    if not colorset then
        colorset = { }
        colorsets[name] = colorset
    end
end

local function popset()
    colorset = remove(stack)
end

local function setlist(name)
    return table.sortedkeys(name and name ~= "" and colorsets[name] or colorsets.default or {})
end

colors.pushset = pushset
colors.popset  = popset
colors.setlist = setlist

local ctx_colordefagc = context.colordefagc
local ctx_colordefagt = context.colordefagt
local ctx_colordefalc = context.colordefalc
local ctx_colordefalt = context.colordefalt
local ctx_colordeffgc = context.colordeffgc
local ctx_colordeffgt = context.colordeffgt
local ctx_colordefflc = context.colordefflc
local ctx_colordefflt = context.colordefflt
local ctx_colordefrgc = context.colordefrgc
local ctx_colordefrgt = context.colordefrgt
local ctx_colordefrlc = context.colordefrlc
local ctx_colordefrlt = context.colordefrlt

local function definecolor(name, ca, global)
    if ca and ca > 0 then
        if global then
            if trace_define then
                report_colors("define global color %a with attribute %a",name,ca)
            end
            ctx_colordefagc(name,ca)
        else
            if trace_define then
                report_colors("define local color %a with attribute %a",name,ca)
            end
            ctx_colordefalc(name,ca)
        end
    else
        if global then
            ctx_colordefrgc(name)
        else
            ctx_colordefrlc(name)
        end
    end
    colorset[name] = true-- maybe we can store more
end

local function inheritcolor(name, ca, global)
    if ca and ca ~= "" then
        if global then
            if trace_define then
                report_colors("inherit global color %a with attribute %a",name,ca)
            end
            ctx_colordeffgc(name,ca) -- some day we will set the macro directly
        else
            if trace_define then
                report_colors("inherit local color %a with attribute %a",name,ca)
            end
            ctx_colordefflc(name,ca)
        end
    else
        if global then
            ctx_colordefrgc(name)
        else
            ctx_colordefrlc(name)
        end
    end
    colorset[name] = true-- maybe we can store more
end

local function definetransparent(name, ta, global)
    if ta and ta > 0 then
        if global then
            if trace_define then
                report_colors("define global transparency %a with attribute %a",name,ta)
            end
            ctx_colordefagt(name,ta)
        else
            if trace_define then
                report_colors("define local transparency %a with attribute %a",name,ta)
            end
            ctx_colordefalt(name,ta)
        end
    else
        if global then
            ctx_colordefrgt(name)
        else
            ctx_colordefrlt(name)
        end
    end
end

local function inherittransparent(name, ta, global)
    if ta and ta ~= "" then
        if global then
            if trace_define then
                report_colors("inherit global transparency %a with attribute %a",name,ta)
            end
            ctx_colordeffgt(name,ta)
        else
            if trace_define then
                report_colors("inherit local transparency %a with attribute %a",name,ta)
            end
            ctx_colordefflt(name,ta)
        end
    else
        if global then
            ctx_colordefrgt(name)
        else
            ctx_colordefrlt(name)
        end
    end
end

local transparent = {
    none       =  0,
    normal     =  1,
    multiply   =  2,
    screen     =  3,
    overlay    =  4,
    softlight  =  5,
    hardlight  =  6,
    colordodge =  7,
    colorburn  =  8,
    darken     =  9,
    lighten    = 10,
    difference = 11,
    exclusion  = 12,
    hue        = 13,
    saturation = 14,
    color      = 15,
    luminosity = 16,
}

transparencies.names = transparent

local gray_okay   = true
local rgb_okay    = true
local cmyk_okay   = true
local spot_okay   = true
local multi_okay  = true
local forced      = false

function colors.forcesupport(gray,rgb,cmyk,spot,multi) -- pdfx driven
    gray_okay, rgb_okay, cmyk_okay, spot_okay, multi_okay, forced = gray, rgb, cmyk, spot, multi, true
    report_colors("supported models: gray %a, rgb %a, cmyk %a, spot %a",gray,rgb,cmyk,spot)
end

local function forcedmodel(model) -- delayed till the backend but mp directly
    if not forced then
        return model
    elseif model == 2 then -- gray
        if gray_okay then
            -- okay
        elseif cmyk_okay then
            return 4
        elseif rgb_okay then
            return 3
        end
    elseif model == 3 then -- rgb
        if rgb_okay then
            -- okay
        elseif cmyk_okay then
            return 4
        elseif gray_okay then
            return 2
        end
    elseif model == 4 then -- cmyk
        if cmyk_okay then
            -- okay
        elseif rgb_okay then
            return 3
        elseif gray_okay then
            return 2
        end
    elseif model == 5 then -- spot
        if cmyk_okay then
            return 4
        elseif rgb_okay then
            return 3
        elseif gray_okay then
            return 2
        end
    end
    return model
end

colors.forcedmodel = forcedmodel

-- By coupling we are downward compatible. When we decouple we need to do more tricky
-- housekeeping (e.g. persist color independent transparencies when color bound ones
-- are nil.)

colors.couple = true

local function definetransparency(name,n,global)
    if n == v_reset then
        definetransparent(name, 0, global) -- or attributes.unsetvalue
        return
    end
    local a = tonumber(n)
    if a then
        transparent[name] = a -- 0 .. 16
        return
    end
    local a = transparent[name]
    if a then
        transparent[name] = a
        return
    end
    local settings = settings_to_hash_strict(n)
    if settings then
        local a, t = settings.a, settings.t
        if a and t then
            definetransparent(name, transparencies.register(name,transparent[a] or tonumber(a) or 1,tonumber(t) or 1), global)
        else
            definetransparent(name, 0, global)
        end
    else
        inherittransparent(name, n, global)
    end
end

colors.definetransparency = definetransparency

local registered = { }

local function do_registerspotcolor(parent,parentnumber,e,f,d,p)
    if not registered[parent] then
        local v = colorvalues[parentnumber]
        if v then
            local model = currentmodel()
            if model == 1 then
                model = v[1]
            end
            if e and e ~= "" then
                registrations.spotcolorname(parent,e) -- before registration of the color
            end
            if     model == 2 then -- name noffractions names p's r g b
                registrations.grayspotcolor(parent,f,d,p,v[2])
            elseif model == 3 then
                registrations.rgbspotcolor (parent,f,d,p,v[3],v[4],v[5])
            elseif model == 4 then
                registrations.cmykspotcolor(parent,f,d,p,v[6],v[7],v[8],v[9])
            end
        end
        registered[parent] = true
    end
end

-- local function do_registermultitonecolor(parent,name,parentnumber,e,f,d,p) -- same as spot but different template
--     if not registered[parent] then
--         local v = colorvalues[parentnumber]
--         if v then
--             local model = currentmodel()
--             if model == 1 then
--                 model = v[1]
--             end
--             if     model == 2 then
--                 registrations.grayindexcolor(parent,f,d,p,v[2])
--             elseif model == 3 then
--                 registrations.rgbindexcolor (parent,f,d,p,v[3],v[4],v[5])
--             elseif model == 4 then
--                 registrations.cmykindexcolor(parent,f,d,p,v[6],v[7],v[8],v[9])
--             end
--         end
--         registered[parent] = true
--     end
-- end

function colors.definesimplegray(name,s)
    return register_color(name,'gray',s) -- we still need to get rid of 'color'
end

local hexdigit    = R("09","AF","af")
local hexnumber   = hexdigit * hexdigit / function(s) return tonumber(s,16)/255 end
local hexpattern  = hexnumber * (P(-1) + hexnumber * hexnumber * P(-1))
local hexcolor    = Cc("H") * P("#") * hexpattern

local left        = P("(")
local right       = P(")")
local comma       = P(",")
local mixnumber   = lpegpatterns.number / tonumber
                  + P("-") / function() return -1 end
local mixname     = C(P(1-left-right-comma)^1)
----- mixcolor    = Cc("M") * mixnumber * left * mixname * (comma * mixname)^-1 * right * P(-1)
local mixcolor    = Cc("M") * mixnumber * left * mixname * (comma * mixname)^0 * right * P(-1) -- one is also ok

local exclamation = P("!")
local pgfnumber   = lpegpatterns.digit^0 / function(s) return tonumber(s)/100 end
local pgfname     = C(P(1-exclamation)^1)
local pgfcolor    = Cc("P") * pgfname * exclamation * pgfnumber * (exclamation * pgfname)^-1 * P(-1)

local specialcolor = hexcolor + mixcolor

local l_color        = attributes.list[a_color]
local l_transparency = attributes.list[a_transparency]

directives.register("colors.pgf",function(v)
    if v then
        specialcolor = hexcolor + mixcolor + pgfcolor
    else
        specialcolor = hexcolor + mixcolor
    end
end)

local defineintermediatecolor

local function resolvedname(name)
    local color
    if valid[name] then
        color = counts[name]
        if color then
            color = texgetcount(color)
        else
            color = l_color[name] -- fall back on old method
        end
    else
        color = l_color[name] -- fall back on old method
    end
    return color, l_transparency[name]
end

local function defineprocesscolor(name,str,global,freeze) -- still inconsistent color vs transparent
    local what, one, two, three = lpegmatch(specialcolor,str)
    if what == "H" then
        -- for old times sake (if we need to feed from xml or so)
        definecolor(name, register_color(name,'rgb',one,two,three),global)
    elseif what == "M" then
        -- intermediate
     -- return defineintermediatecolor(name,one,l_color[two],l_color[three],l_transparency[two],l_transparency[three],"",global,freeze)
        local c1, t1 = resolvedname(two)
        local c2, t2 = resolvedname(three)
        return defineintermediatecolor(name,one,c1,c2,t1,t2,"",global,freeze)
    elseif what == "P" then
        -- pgf for tikz
     -- return defineintermediatecolor(name,two,l_color[one],l_color[three],l_transparency[one],l_transparency[three],"",global,freeze)
        local c1, t1 = resolvedname(one)
        local c2, t2 = resolvedname(three)
        return defineintermediatecolor(name,two,c1,c2,t1,t2,"",global,freeze)
    else
        local settings = settings_to_hash_strict(str)
        if settings then
            local r, g, b = settings.r, settings.g, settings.b
            if r or g or b then
                -- we can consider a combined rgb cmyk s definition
                definecolor(name, register_color(name,'rgb', tonumber(r) or 0, tonumber(g) or 0, tonumber(b) or 0), global)
            else
                local c, m, y, k = settings.c, settings.m, settings.y, settings.k
                if c or m or y or k then
                    definecolor(name, register_color(name,'cmyk',tonumber(c) or 0, tonumber(m) or 0, tonumber(y) or 0, tonumber(k) or 0), global)
                else
                    local h, s, v = settings.h, settings.s, settings.v
                    if v then
                        r, g, b = colors.hsvtorgb(tonumber(h) or 0, tonumber(s) or 1, tonumber(v) or 1) -- maybe later native
                        definecolor(name, register_color(name,'rgb',r,g,b), global)
                    else
                        local x = settings.x or h
                        if x then
                            r, g, b = lpegmatch(hexpattern,x) -- can be inlined
                            if r and g and b then
                                definecolor(name, register_color(name,'rgb',r,g,b), global)
                            else
                                definecolor(name, register_color(name,'gray',r or 0), global)
                            end
                        else
                            definecolor(name, register_color(name,'gray',tonumber(s) or 0), global)
                        end
                    end
                end
            end
            local a, t = settings.a, settings.t
            if a and t then
                definetransparent(name, transparencies.register(name,transparent[a] or tonumber(a) or 1,tonumber(t) or 1), global)
            elseif colors.couple then
            --  definetransparent(name, transparencies.register(nil, 1, 1), global) -- can be sped up
                definetransparent(name, 0, global) -- can be sped up
            end
        elseif freeze then
            local ca = attributes_list[a_color]       [str]
            local ta = attributes_list[a_transparency][str]
            if ca then
                definecolor(name, ca, global)
            end
            if ta then
                definetransparent(name, ta, global)
            end
        else
            inheritcolor(name, str, global)
            inherittransparent(name, str, global)
        --  if global and str ~= "" then -- For Peter Rolf who wants access to the numbers in Lua. (Currently only global is supported.)
        --      attributes_list[a_color]       [name] = attributes_list[a_color]       [str] or attributes.unsetvalue  -- reset
        --      attributes_list[a_transparency][name] = attributes_list[a_transparency][str] or attributes.unsetvalue
        --  end
        end
    end
    colorset[name] = true-- maybe we can store more
end

local function isblack(ca) -- maybe commands
    local cv = ca > 0 and colorvalues[ca]
    return (cv and cv[2] == 0) or false
end

colors.isblack = isblack

-- local m, c, t = attributes.colors.namedcolorattributes(parent)
-- if c and c > 1 then -- 1 is black
-- local v = attributes.colors.values[c]


local function definespotcolor(name,parent,str,global)
    if parent == "" or find(parent,"=",1,true) then
        colors.registerspotcolor(name, parent) -- does that work? no attr
    elseif name ~= parent then
        local cp = attributes_list[a_color][parent]
        if cp then
            local t = settings_to_hash_strict(str)
            if t then
                local tp = tonumber(t.p) or 1
                do_registerspotcolor(parent,cp,t.e,1,"",tp) -- p not really needed, only diagnostics
                if name and name ~= "" then
                    definecolor(name,register_color(name,'spot',parent,1,"",tp),true)
                    local ta, tt = t.a, t.t
                    if ta and tt then
                        definetransparent(name, transparencies.register(name,transparent[ta] or tonumber(ta) or 1,tonumber(tt) or 1), global)
                    elseif colors.couple then
                     -- definetransparent(name, transparencies.register(nil, 1, 1), global) -- can be sped up
                        definetransparent(name, 0, global) -- can be sped up
                    end
                end
            end
        end
    end
    colorset[name] = true-- maybe we can store more
end

function colors.registerspotcolor(parent, str)
    local cp = attributes_list[a_color][parent]
    if cp then
        local e = ""
        if str then
            local t = settings_to_hash_strict(str)
            e = (t and t.e) or ""
        end
        do_registerspotcolor(parent, cp, e, 1, "", 1) -- p not really needed, only diagnostics
    end
end

local function f(i,colors,fraction)
    local otf = 0
    if type(fraction) == "table" then
        for c=1,#colors do
            otf = otf + (tonumber(fraction[c]) or 1) * colors[c][i]
        end
    else
        fraction = tonumber(fraction)
        for c=1,#colors do
            otf = otf + fraction * colors[c]
        end
    end
    if otf > 1 then
        otf = 1
    end
    return otf
end

local function definemixcolor(makename,name,fractions,cs,global,freeze)
    local values = { }
    for i=1,#cs do -- do fraction in here
        local v = colorvalues[cs[i]]
        if not v then
            return
        end
        values[i] = v
    end
    if #values > 0 then
        csone = values[1][1]
        local ca
        if csone == 2 then
            ca = register_color(name,'gray',f(2,values,fractions))
        elseif csone == 3 then
            ca = register_color(name,'rgb', f(3,values,fractions),
                                            f(4,values,fractions),
                                            f(5,values,fractions))
        elseif csone == 4 then
            ca = register_color(name,'cmyk',f(6,values,fractions),
                                            f(7,values,fractions),
                                            f(8,values,fractions),
                                            f(9,values,fractions))
        else
            ca = register_color(name,'gray',f(2,values,fractions))
        end
        definecolor(name,ca,global,freeze)
    else
        report_colors("invalid specification of components for color %a",makename)
    end
end

local function definemultitonecolor(name,multispec,colorspec,selfspec)
    local dd, pp, nn, max = { }, { }, { }, 0
    for k,v in gmatch(multispec,"([^=,]+)=([^%,]*)") do -- use settings_to_array
        max = max + 1
        dd[max] = k
        pp[max] = v
        nn[max] = formatters["%s_%1.3g"](k,tonumber(v) or 0) -- 0 can't happen
    end
    if max > 0 then
        nn = concat(nn,'_')
        local parent = gsub(lower(nn),"[^%d%a%.]+","_")
        if not colorspec or colorspec == "" then
            local cc = { }
            for i=1,max do
--                 cc[i] = l_color[dd[i]]
                cc[i] = resolvedname(dd[i])
            end
            definemixcolor(name,parent,pp,cc,global,freeze) -- can become local
        else
            if selfspec ~= "" then
                colorspec = colorspec .. "," .. selfspec
            end
            defineprocesscolor(parent,colorspec,true,true)
        end
        local cp = attributes_list[a_color][parent]
        dd, pp = concat(dd,','), concat(pp,',')
        if cp then
            do_registerspotcolor(parent, cp, "", max, dd, pp)
            definecolor(name, register_color(name, 'spot', parent, max, dd, pp), true)
            local t = settings_to_hash_strict(selfspec)
            if t and t.a and t.t then
                definetransparent(name, transparencies.register(name,transparent[t.a] or tonumber(t.a) or 1,tonumber(t.t) or 1), global)
            elseif colors.couple then
            --  definetransparent(name, transparencies.register(nil, 1, 1), global) -- can be sped up
                definetransparent(name, 0, global) -- can be sped up
            end
        end
    end
    colorset[name] = true-- maybe we can store more
end

colors.defineprocesscolor   = defineprocesscolor
colors.definespotcolor      = definespotcolor
colors.definemultitonecolor = definemultitonecolor

-- will move to mlib-col as colors in mp are somewhat messy due to the fact
-- that we cannot cast .. so we really need to use (s,s,s) for gray in order
-- to be able to map onto 'color'

local function mpcolor(model,ca,ta,default)
    local cv = colorvalues[ca]
    if cv then
        local tv = transparencyvalues[ta]
        if model == 1 then
            model = cv[1]
        end
        model = forcedmodel(model)
        if tv then
            if model == 2 then
                return formatters["transparent(%s,%s,(%s,%s,%s))"](tv[1],tv[2],cv[3],cv[4],cv[5])
            elseif model == 3 then
                return formatters["transparent(%s,%s,(%s,%s,%s))"](tv[1],tv[2],cv[3],cv[4],cv[5])
            elseif model == 4 then
                return formatters["transparent(%s,%s,cmyk(%s,%s,%s,%s))"](tv[1],tv[2],cv[6],cv[7],cv[8],cv[9])
            elseif model == 5 then
             -- return formatters['transparent(%s,%s,multitonecolor("%s",%s,"%s","%s"))'](tv[1],tv[2],cv[10],cv[11],cv[12],cv[13])
                return formatters['transparent(%s,%s,namedcolor("%s"))'](tv[1],tv[2],cv[10])
            else -- see ** in meta-ini.mkiv: return formatters["transparent(%s,%s,(%s))"](tv[1],tv[2],cv[2])
                return formatters["transparent(%s,%s,(%s,%s,%s))"](tv[1],tv[2],cv[3],cv[4],cv[5])
            end
        else
            if model == 2 then
                return formatters["(%s,%s,%s)"](cv[3],cv[4],cv[5])
            elseif model == 3 then
                return formatters["(%s,%s,%s)"](cv[3],cv[4],cv[5])
            elseif model == 4 then
                return formatters["cmyk(%s,%s,%s,%s)"](cv[6],cv[7],cv[8],cv[9])
            elseif model == 5 then
             -- return formatters['multitonecolor("%s",%s,"%s","%s")'](cv[10],cv[11],cv[12],cv[13])
                return formatters['namedcolor("%s")'](cv[10])
            else -- see ** in meta-ini.mkiv: return formatters["%s"]((cv[2]))
                return formatters["(%s,%s,%s)"](cv[3],cv[4],cv[5])
            end
        end
    end
    local tv = transparencyvalues[ta]
    if tv then
        return formatters["(%s,%s)"](tv[1],tv[2])
    end
    default = default or 0 -- rgb !
    return formatters["(%s,%s,%s)"](default,default,default)
end

-- local function mpnamedcolor(name)
--     return mpcolor(texgetattribute(a_colormodel),l_color[name] or l_color.black,l_transparency[name] or false)
-- end

local colornamespace  = getnamespace("colornumber")
local paletnamespace  = getnamespace("colorpalet")

local function namedcolorattributes(name)
    local space  = texgetattribute(a_colormodel)
    local prefix = texgettoks("t_colo_prefix")
    local color
    if prefix ~= "" then
        color = valid[prefix..name]
        if not color then
            local n = paletnamespace .. prefix .. name
            color = valid[n]
            if not color then
                color = name
            elseif color == true then
                color = n
            end
        elseif color == true then
            color = paletnamespace .. prefix .. name
        end
    elseif valid[name] then
        color = name
    else
        return space, l_color.black
    end
    color = counts[color]
    if color then
        color = texgetcount(color)
    else
        color = l_color[name] -- fall back on old method
    end
    if color then
        return space, color, l_transparency[name]
    else
        return space, l_color.black
    end
end

colors.namedcolorattributes = namedcolorattributes -- can be used local

local function mpnamedcolor(name)
    return mpcolor(namedcolorattributes(name))
end

local function mpoptions(model,ca,ta,default) -- will move to mlib-col .. not really needed
    return formatters["withcolor %s"](mpcolor(model,ca,ta,default))
end

colors.mpcolor      = mpcolor
colors.mpnamedcolor = mpnamedcolor
colors.mpoptions    = mpoptions

-- elsewhere:
--
-- mp.NamedColor = function(str)
--     mpprint(mpnamedcolor(str))
-- end


-- local function formatcolor(ca,separator)
--     local cv = colorvalues[ca]
--     if cv then
--         local c, cn, f, t, model = { }, 0, 13, 13, cv[1]
--         if model == 2 then
--             return c[2]
--         elseif model == 3 then
--             return concat(c,separator,3,5)
--         elseif model == 4 then
--             return concat(c,separator,6,9)
--         end
--     else
--         return 0
--     end
-- end

local function formatcolor(ca,separator)
    local cv = colorvalues[ca]
    if cv then
        local c, cn, f, t, model = { }, 0, 13, 13, cv[1]
        if model == 2 then
            f, t = 2, 2
        elseif model == 3 then
            f, t = 3, 5
        elseif model == 4 then
            f, t = 6, 9
        end
        for i=f,t do
            cn = cn + 1
            c[cn] = format("%0.3f",cv[i])
        end
        return concat(c,separator)
    else
        return format("%0.3f",0)
    end
end

local function formatgray(ca,separator)
    local cv = colorvalues[ca]
    return format("%0.3f",(cv and cv[2]) or 0)
end

colors.formatcolor = formatcolor
colors.formatgray  = formatgray

local f_gray         = formatters["s=%1.3f"]
local f_rgb          = formatters["r=%1.3f%sg=%1.3f%sb=%1.3f"]
local f_cmyk         = formatters["c=%1.3f%sm=%1.3f%sy=%1.3f%sk=%1.3f"]
local f_spot_name    = formatters["p=%s"]
local f_spot_value   = formatters["p=%1.3f"]
local f_transparency = formatters["a=%1.3f%st=%1.3f"]
local f_both         = formatters["%s%s%s"]

local function colorcomponents(ca,separator) -- return list
    local cv = colorvalues[ca]
    if cv then
        local model = cv[1]
        if model == 2 then
            return f_gray(cv[2])
        elseif model == 3 then
            return f_rgb(cv[3],separator or " ",cv[4],separator or " ",cv[5])
        elseif model == 4 then
            return f_cmyk(cv[6],separator or " ",cv[7],separator or " ",cv[8],separator or " ",cv[9])
        elseif type(cv[13]) == "string" then
            return f_spot_name(cv[13])
        else
            return f_spot_value(cv[13])
        end
    else
        return ""
    end
end

local function transparencycomponents(ta,separator)
    local tv = transparencyvalues[ta]
    if tv then
        return f_transparency(tv[1],separator or " ",tv[2])
    else
        return ""
    end
end

local function processcolorcomponents(ca,separator)
    local cs = colorcomponents(ca,separator)
    local ts = transparencycomponents(ca,separator)
    if cs == "" then
        return ts
    elseif ts == "" then
        return cs
    else
        return f_both(cs,separator or " ",ts)
    end
end

local function spotcolorname(ca,default)
    local cv, v = colorvalues[ca], "unknown"
    if not cv and type(ca) == "string" then
        ca = resolvedname(ca) -- we could metatable colorvalues
        cv = colorvalues[ca]
    end
    if cv and cv[1] == 5 then
        v = cv[10]
    end
    return tostring(v)
end

local function spotcolorparent(ca,default)
    local cv, v = colorvalues[ca], "unknown"
    if not cv and type(ca) == "string" then
        ca = resolvedname(ca) -- we could metatable colorvalues
        cv = colorvalues[ca]
    end
    if cv and cv[1] == 5 then
        v = cv[12]
        if v == "" then
            v = cv[10]
        end
    end
    return tostring(v)
end

local function spotcolorvalue(ca,default)
    local cv, v = colorvalues[ca], 0
    if not cv and type(ca) == "string" then
        ca = resolvedname(ca) -- we could metatable colorvalues
        cv = colorvalues[ca]
    end
    if cv and cv[1] == 5 then
       v = cv[13]
    end
    return tostring(v)
end

colors.colorcomponents        = colorcomponents
colors.transparencycomponents = transparencycomponents
colors.processcolorcomponents = processcolorcomponents
colors.spotcolorname          = spotcolorname
colors.spotcolorparent        = spotcolorparent
colors.spotcolorvalue         = spotcolorvalue

-- experiment  (a bit of a hack, as we need to get the attribute number)

local min = math.min

-- a[b,c] -> b+a*(c-b)

local function inbetween(one,two,i,fraction)
    local o, t = one[i], two[i]
    local c = fraction < 0
    if c then
        fraction = - fraction
    end
    local otf = o + fraction * (t - o)
    if otf > 1 then
        otf = 1
    end
    if c then
        return 1 - otf
    else
        return otf
    end
end

local function justone(one,fraction,i)
    local otf = fraction * one[i]
    if otf > 1 then
        otf = 1
    end
    return otf
end

local function complement(one,fraction,i)
    local otf = - fraction * (1 - one[i])
    if otf > 1 then
        otf = 1
    end
    return otf
end

defineintermediatecolor = function(name,fraction,c_one,c_two,a_one,a_two,specs,global,freeze)
    fraction = tonumber(fraction) or 1
    local one, two = colorvalues[c_one], colorvalues[c_two] -- beware, it uses the globals
    if one then
        if two then
            local csone, cstwo = one[1], two[1]
         -- if csone == cstwo then
                -- actually we can set all 8 values at once here but this is cleaner as we avoid
                -- problems with weighted gray conversions and work with original values
                local ca
                if csone == 2 then
                    ca = register_color(name,'gray',inbetween(one,two,2,fraction))
                elseif csone == 3 then
                    ca = register_color(name,'rgb', inbetween(one,two,3,fraction),
                                                    inbetween(one,two,4,fraction),
                                                    inbetween(one,two,5,fraction))
                elseif csone == 4 then
                    ca = register_color(name,'cmyk',inbetween(one,two,6,fraction),
                                                    inbetween(one,two,7,fraction),
                                                    inbetween(one,two,8,fraction),
                                                    inbetween(one,two,9,fraction))
                else
                    ca = register_color(name,'gray',inbetween(one,two,2,fraction))
                end
                definecolor(name,ca,global,freeze)
         -- end
        else
            local inbetween = fraction < 0 and complement or justone
            local csone = one[1]
            local ca
            if csone == 2 then
                ca = register_color(name,'gray',inbetween(one,fraction,2))
            elseif csone == 3 then
                ca = register_color(name,'rgb', inbetween(one,fraction,3),
                                                inbetween(one,fraction,4),
                                                inbetween(one,fraction,5))
            elseif csone == 4 then
                ca = register_color(name,'cmyk',inbetween(one,fraction,6),
                                                inbetween(one,fraction,7),
                                                inbetween(one,fraction,8),
                                                inbetween(one,fraction,9))
            else
                ca = register_color(name,'gray',inbetween(one,fraction,2))
            end
            definecolor(name,ca,global,freeze)
        end
    end
    local one, two = transparencyvalues[a_one], transparencyvalues[a_two]
    local t = settings_to_hash_strict(specs)
    local ta = tonumber((t and t.a) or (one and one[1]) or (two and two[1]))
    local tt = tonumber((t and t.t) or (one and two and f(one,two,2,fraction)))
    if ta and tt then
        definetransparent(name,transparencies.register(name,ta,tt),global)
    end
end

colors.defineintermediatecolor = defineintermediatecolor

-- for the moment downward compatible

local patterns = {
    "colo-imp-%s.mkiv",
    "colo-imp-%s.tex",
    -- obsolete:
    "colo-%s.mkiv",
    "colo-%s.tex"
}

local function action(name,foundname)
    -- could be one command
    context.startreadingfile()
    context.startcolorset { name }
    context.input(foundname)
    context.showmessage("colors",4,name)
    context.stopcolorset()
    context.stopreadingfile()
end

local function failure(name)
 -- context.showmessage("colors",5,name)
    report_colors("unknown library %a",name)
end

local function usecolors(name)
    commands.uselibrary {
        category = "color definition",
        name     = name,
        patterns = patterns,
        action   = action,
        failure  = failure,
        onlyonce = true,
    }
end

colors.usecolors = usecolors

-- backend magic

local currentpagecolormodel

function colors.setpagecolormodel(model)
    currentpagecolormodel = model
end

function colors.getpagecolormodel()
    return currentpagecolormodel
end

-- interface

local setcolormodel = colors.setmodel

implement {
    name      = "synccolorcount",
    actions   = synccolorcount,
    arguments = { "string", "integer" }
}

implement {
    name      = "synccolor",
    actions   = synccolor,
    arguments = "string",
}

implement {
    name      = "synccolorclone",
    actions   = synccolorclone,
    arguments = { "string", "string" },
}

implement {
    name      = "setcolormodel",
    arguments = { "string", "string" },
    actions   = function(model,weight)
        texsetattribute(a_colormodel,setcolormodel(model,weight))
    end
}

implement {
    name      = "setpagecolormodel",
    actions   = colors.setpagecolormodel,
    arguments = { "string" },
}

implement {
    name      = "defineprocesscolorlocal",
    actions   = defineprocesscolor,
    arguments = { "string", "string", false, "boolean" }
}

implement {
    name      = "defineprocesscolorglobal",
    actions   = defineprocesscolor,
    arguments = { "string", "string", true, "boolean" }
}

implement {
    name      = "defineprocesscolordummy",
    actions   = defineprocesscolor,
    arguments = { "'d_u_m_m_y'", "string", false, false }
}

implement {
    name      = "definespotcolorglobal",
    actions   = definespotcolor,
    arguments = { "string", "string", "string", true }
}

implement {
    name      = "definemultitonecolorglobal",
    actions   = definemultitonecolor,
    arguments = { "string", "string", "string", "string", true }
}

implement {
    name      = "registermaintextcolor",
    actions   = function(main)
        colors.main = main
    end,
    arguments = { "integer" }
}

implement {
    name      = "definetransparency",
    actions   = definetransparency,
    arguments = { "string", "string" }
}

implement {
    name      = "definetransparencyglobal",
    actions   = definetransparency,
    arguments = { "string", "string", true }
}

implement {
    name      = "defineintermediatecolor",
    actions   = defineintermediatecolor,
    arguments = { "string", "string", "integer", "integer", "integer", "integer", "string", false, "boolean" }
}

implement { name = "spotcolorname",          actions = { spotcolorname,          context }, arguments = "integer" }
implement { name = "spotcolorparent",        actions = { spotcolorparent,        context }, arguments = "integer" }
implement { name = "spotcolorvalue",         actions = { spotcolorvalue,         context }, arguments = "integer" }
implement { name = "colorcomponents",        actions = { colorcomponents,        context }, arguments = { "integer", tokens.constant(",") } }
implement { name = "transparencycomponents", actions = { transparencycomponents, context }, arguments = { "integer", tokens.constant(",") } }
implement { name = "processcolorcomponents", actions = { processcolorcomponents, context }, arguments = { "integer", tokens.constant(",") } }
implement { name = "formatcolor",            actions = { formatcolor,            context }, arguments = { "integer", "string" } }
implement { name = "formatgray",             actions = { formatgray,             context }, arguments = { "integer", "string" } }

implement {
    name      = "mpcolor",
    actions   = { mpcolor, context },
    arguments = { "integer", "integer", "integer" }
}

implement {
    name      = "mpoptions",
    actions   = { mpoptions, context },
    arguments = { "integer", "integer", "integer" }
}

local ctx_doifelse = commands.doifelse

implement {
    name      = "doifelsedrawingblack",
    actions   = function() ctx_doifelse(isblack(texgetattribute(a_color))) end
}

implement {
    name      = "doifelseblack",
    actions   = { isblack, ctx_doifelse },
    arguments = "integer"
}

-- function commands.withcolorsinset(name,command)
--     local set
--     if name and name ~= "" then
--         set = colorsets[name]
--     else
--         set = colorsets.default
--     end
--     if set then
--         if command then
--             for name in table.sortedhash(set) do
--                 context[command](name)
--             end
--         else
--             context(concat(table.sortedkeys(set),","))
--         end
--     end
-- end

implement { name = "startcolorset", actions = pushset,   arguments = "string" }
implement { name = "stopcolorset",  actions = popset }
implement { name = "usecolors",     actions = usecolors, arguments = "string" }

-- bonus

do

    local function pgfxcolorspec(model,ca) -- {}{}{colorspace}{list}
     -- local cv = attributes.colors.values[ca]
        local cv = colorvalues[ca]
        local str
        if cv then
            if model and model ~= 0 then
                model = model
            else
                model = forcedmodel(texgetattribute(a_colormodel))
                if model == 1 then
                    model = cv[1]
                end
            end
            if model == 3 then
                str = formatters["{rgb}{%1.3f,%1.3f,%1.3f}"](cv[3],cv[4],cv[5])
            elseif model == 4 then
                str = formatters["{cmyk}{%1.3f,%1.3f,%1.3f,%1.3f}"](cv[6],cv[7],cv[8],cv[9])
            else -- there is no real gray
                str = formatters["{rgb}{%1.3f,%1.3f,%1.3f}"](cv[2],cv[2],cv[2])
            end
        else
            str = "{rgb}{0,0,0}"
        end
        if trace_pgf then
            report_pgf("model %a, string %a",model,str)
        end
        return str
    end

    implement {
        name      = "pgfxcolorspec",
        actions   = { pgfxcolorspec, context },
        arguments = { "integer", "integer" }
    }

end

-- handy

local models = storage.allocate { "all", "gray", "rgb", "cmyk", "spot" }

colors.models = models -- check for usage elsewhere

function colors.spec(name)
    local l = attributes_list[a_color]
    local t = colorvalues[l[name]] or colorvalues[l.black]
    return {
        model = models[t[1]] or models[1],
        s = t[2],
        r = t[3], g = t[4], b = t[5],
        c = t[6], m = t[7], y = t[8], k = t[9],
    }
end

function colors.currentnamedmodel()
    return models[texgetattribute(a_colormodel)] or "gray"
end

-- inspect(attributes.colors.spec("red"))
-- inspect(attributes.colors.spec("red socks"))

implement {
    name      = "negatedcolorcomponent",
    arguments = "string",
    actions   = function(s)
        s = 1 - (tonumber(s) or 0)
        context((s < 0 and 0) or (s > 1 and 1) or s)
    end
}