luametafun-color.tex /size: 4821 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/luametafun
2
3\environment luametafun-style
4
5\startcomponent luametafun-color
6
7\startchapter[title={Color}]
8
9There are by now plenty of examples made by users that use color and \METAFUN\
10provides all kind of helpers. So do we need more? When I play around with things
11or when users come with questions that then result in a nice looking graphic, the
12result might en dup as example of coding. The following is an example of showing
13of colors. We have a helper that goes from a so called lab specification to rgb
14and it does that via xyz transformations. It makes no real sense to interface
15this beyond this converter. We use this opportunity to demonstrate how to make
16an interface.
17
18\startbuffer
19\startMPdefinitions
20  vardef cielabmatrix(expr l, mina, maxa, minb, maxb, stp) =
21    image (
22      for a = mina step stp until maxa :
23        for b = minb step stp until maxb :
24          draw (a,b) withcolor labtorgb(l,a,b) ;
25        endfor ;
26      endfor ;
27    )
28  enddef ;
29\stopMPdefinitions
30\stopbuffer
31
32\typebuffer[option=TEX]
33
34\getbuffer
35
36Here we define a macro that makes a color matrix. It can be used as follows
37
38\startbuffer
39\startcombination[nx=4,ny=1]
40  {\startMPcode draw cielabmatrix(20, -100, 100, -100, 100, 5) ysized 35mm withpen pencircle scaled 2.5 ; \stopMPcode} {\type {l = 20}}
41  {\startMPcode draw cielabmatrix(40, -100, 100, -100, 100, 5) ysized 35mm withpen pencircle scaled 2.5 ; \stopMPcode} {\type {l = 40}}
42  {\startMPcode draw cielabmatrix(60, -100, 100, -100, 100, 5) ysized 35mm withpen pencircle scaled 2.5 ; \stopMPcode} {\type {l = 60}}
43  {\startMPcode draw cielabmatrix(80, -100, 100, -100, 100, 5) ysized 35mm withpen pencircle scaled 2.5 ; \stopMPcode} {\type {l = 80}}
44\stopcombination
45\stopbuffer
46
47\typebuffer[option=TEX]
48
49\startlinecorrection
50\getbuffer
51\stoplinecorrection
52
53One can of course mess around a bit:
54
55\startbuffer
56\startcombination[nx=4,ny=1]
57  {\startMPcode draw cielabmatrix(20, -100, 100, -100, 100, 10) ysized 35mm randomized 1 withpen pensquare scaled 4 ; \stopMPcode} {\type {l = 20}}
58  {\startMPcode draw cielabmatrix(40, -100, 100, -100, 100, 10) ysized 35mm randomized 1 withpen pensquare scaled 4 ; \stopMPcode} {\type {l = 40}}
59  {\startMPcode draw cielabmatrix(60, -100, 100, -100, 100, 10) ysized 35mm randomized 1 withpen pensquare scaled 4 ; \stopMPcode} {\type {l = 60}}
60  {\startMPcode draw cielabmatrix(80, -100, 100, -100, 100, 10) ysized 35mm randomized 1 withpen pensquare scaled 4 ; \stopMPcode} {\type {l = 80}}
61\stopcombination
62\stopbuffer
63
64\typebuffer[option=TEX]
65
66\startlinecorrection
67\getbuffer
68\stoplinecorrection
69
70Normally, when you don't go beyond this kind of usage, a simple macro like the
71above will do. But when you want to make something that is upward compatible
72(which is one of the principles behind the \CONTEXT\ user interface(s), you can
73do this:
74
75\startbuffer
76\startcombination[nx=4,ny=1]
77    {\startMPcode draw lmt_labtorgb [ l = 20, step = 20 ] ysized 35mm withpen pencircle scaled 8 ; \stopMPcode} {\type {l = 20}}
78    {\startMPcode draw lmt_labtorgb [ l = 40, step = 20 ] ysized 35mm withpen pencircle scaled 8 ; \stopMPcode} {\type {l = 40}}
79    {\startMPcode draw lmt_labtorgb [ l = 60, step = 20 ] ysized 35mm withpen pencircle scaled 8 ; \stopMPcode} {\type {l = 60}}
80    {\startMPcode draw lmt_labtorgb [ l = 80, step = 20 ] ysized 35mm withpen pencircle scaled 8 ; \stopMPcode} {\type {l = 80}}
81\stopcombination
82\stopbuffer
83
84\typebuffer[option=TEX]
85
86\startlinecorrection
87\getbuffer
88\stoplinecorrection
89
90This is a predefined macro in the reserved \type {lmt_} namespace (don't use that
91one yourself, create your own). First we preset the possible parameters:
92
93\starttyping[option=MP]
94presetparameters "labtorgb" [
95  mina = -100,
96  maxa =  100,
97  minb = -100,
98  maxb =  100,
99  step =    5,
100  l    =   50,
101] ;
102\stoptyping
103
104Next we define the main interface macro:
105
106\starttyping[option=MP]
107def lmt_labtorgb = applyparameters "labtorgb" "lmt_do_labtorgb" enddef ;
108\stoptyping
109
110Last we do the actual implementation, which looks a lot like the one we
111started with:
112
113\starttyping[option=MP]
114vardef lmt_do_labtorgb =
115  image (
116    pushparameters "labtorgb" ;
117      save l ; l := getparameter "l" ;
118      for a = getparameter "mina" step getparameter "step"
119            until getparameter "maxa" :
120        for b = getparameter "minb" step getparameter "step"
121            until getparameter "maxb" :
122          draw (a,b) withcolor labtorgb(l,a,b) ;
123        endfor ;
124      endfor ;
125    popparameters ;
126  )
127enddef ;
128\stoptyping
129
130Of course we can now add all kind of extra features but this is what we currently
131have. Maybe this doesn't belong in the \METAFUN\ core but it's not that much code
132and a nice demo. After all, there is much in there that is seldom used.
133
134\stopchapter
135
136\stopcomponent
137
138
139