diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/colo-ini.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/colo-ini.lua | 202 |
1 files changed, 93 insertions, 109 deletions
diff --git a/Master/texmf-dist/tex/context/base/colo-ini.lua b/Master/texmf-dist/tex/context/base/colo-ini.lua index 747e2116f08..14873393216 100644 --- a/Master/texmf-dist/tex/context/base/colo-ini.lua +++ b/Master/texmf-dist/tex/context/base/colo-ini.lua @@ -11,12 +11,14 @@ 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 report_colors = logs.reporter("colors","defining") -local attributes, context, commands = attributes, context, commands +local attributes, backends, storage = attributes, backends, storage +local context, commands = context, commands local settings_to_hash_strict = utilities.parsers.settings_to_hash_strict @@ -37,6 +39,8 @@ local attributes_list = attributes.list local colorvalues = colors.values local transparencyvalues = transparencies.values +local texattribute = tex.attribute + colors.sets = colors.sets or { } -- sets are mostly used for local colorsets = colors.sets -- showing lists of defined local colorset = { } -- colors @@ -67,12 +71,12 @@ local function definecolor(name, ca, global) if ca and ca > 0 then if global then if trace_define then - report_colors("define global color '%s' with attribute: %s",name,ca) + report_colors("define global color %a with attribute %a",name,ca) end context.colordefagc(name,ca) else if trace_define then - report_colors("define local color '%s' with attribute: %s",name,ca) + report_colors("define local color %a with attribute %a",name,ca) end context.colordefalc(name,ca) end @@ -90,12 +94,12 @@ local function inheritcolor(name, ca, global) if ca and ca ~= "" then if global then if trace_define then - report_colors("inherit global color '%s' with attribute: %s",name,ca) + report_colors("inherit global color %a with attribute %a",name,ca) end context.colordeffgc(name,ca) -- some day we will set the macro directly else if trace_define then - report_colors("inherit local color '%s' with attribute: %s",name,ca) + report_colors("inherit local color %a with attribute %a",name,ca) end context.colordefflc(name,ca) end @@ -113,12 +117,12 @@ local function definetransparent(name, ta, global) if ta and ta > 0 then if global then if trace_define then - report_colors("define global transparency '%s' with attribute: %s",name,ta) + report_colors("define global transparency %a with attribute %a",name,ta) end context.colordefagt(name,ta) else if trace_define then - report_colors("define local transparency '%s' with attribute: %s",name,ta) + report_colors("define local transparency %a with attribute %a",name,ta) end context.colordefalt(name,ta) end @@ -135,12 +139,12 @@ local function inherittransparent(name, ta, global) if ta and ta ~= "" then if global then if trace_define then - report_colors("inherit global transparency '%s' with attribute: %s",name,ta) + report_colors("inherit global transparency %a with attribute %a",name,ta) end context.colordeffgt(name,ta) else if trace_define then - report_colors("inherit local transparency '%s' with attribute: %s",name,ta) + report_colors("inherit local transparency %a with attribute %a",name,ta) end context.colordefflt(name,ta) end @@ -177,8 +181,7 @@ local gray_okay, rgb_okay, cmyk_okay, spot_okay, multichannel_okay, forced = tru function colors.forcesupport(gray,rgb,cmyk,spot,multichannel) -- pdfx driven gray_okay, rgb_okay, cmyk_okay, spot_okay, multichannel_okay, forced = gray, rgb, cmyk, spot, multichannel, true - report_colors("supported models: gray=%s, rgb=%s, cmyk=%s, spot=%s", -- multichannel=%s - tostring(gray), tostring(rgb), tostring(cmyk), tostring(spot)) -- tostring(multichannel) + report_colors("supported models: gray %a, rgb %a, cmyk %a, spot %a",gray,rgb,cmyk,spot) -- multichannel=%l multichannel end local function forcedmodel(model) -- delayed till the backend but mp directly @@ -287,7 +290,8 @@ local right = P(")") local comma = P(",") local mixnumber = lpegpatterns.number / tonumber local mixname = C(P(1-left-right-comma)^1) -local mixcolor = Cc("M") * mixnumber * left * mixname * (comma * mixname)^-1 * right * P(-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 @@ -422,14 +426,11 @@ function colors.definemultitonecolor(name,multispec,colorspec,selfspec) max = max + 1 dd[max] = k pp[max] = v - nn[max] = format("%s_%1.3g",k,tonumber(v) or 0) -- 0 can't happen + 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 max == 2 and (not colorspec or colorspec == "") then ---~ colors.defineduocolor(parent,pp[1],l_color[dd[1]],pp[2],l_color[dd[2]],true,true) ---~ elseif (not colorspec or colorspec == "") then if not colorspec or colorspec == "" then local cc = { } for i=1,max do cc[i] = l_color[dd[i]] end colors.definemixcolor(parent,pp,cc,global,freeze) -- can become local @@ -441,11 +442,8 @@ function colors.definemultitonecolor(name,multispec,colorspec,selfspec) end local cp = attributes_list[a_color][parent] dd, pp = concat(dd,','), concat(pp,',') ---~ print(name,multispec,colorspec,selfspec) ---~ print(parent,max,cp) if cp then do_registerspotcolor(parent, name, cp, "", max, dd, pp) ---~ do_registermultitonecolor(parent, name, cp, "", max, dd, pp) -- done in previous ... check it 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 @@ -472,47 +470,46 @@ local function mpcolor(model,ca,ta,default) model = forcedmodel(model) if tv then if model == 2 then - return format("transparent(%s,%s,(%s,%s,%s))",tv[1],tv[2],cv[3],cv[4],cv[5]) + return formatters["transparent(%s,%s,(%s,%s,%s))"](tv[1],tv[2],cv[3],cv[4],cv[5]) elseif model == 3 then - return format("transparent(%s,%s,(%s,%s,%s))",tv[1],tv[2],cv[3],cv[4],cv[5]) + return formatters["transparent(%s,%s,(%s,%s,%s))"](tv[1],tv[2],cv[3],cv[4],cv[5]) elseif model == 4 then - return format("transparent(%s,%s,cmyk(%s,%s,%s,%s))",tv[1],tv[2],cv[6],cv[7],cv[8],cv[9]) + 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 format('transparent(%s,%s,multitonecolor("%s",%s,"%s","%s"))',tv[1],tv[2],cv[10],cv[11],cv[12],cv[13]) - else - return format("transparent(%s,%s,(%s,%s,%s))",tv[1],tv[2],cv[3],cv[4],cv[5]) --- this will become (see ** in meta-ini.mkiv) --- --- return format("transparent(%s,%s,(%s))",tv[1],tv[2],cv[2]) + return formatters['transparent(%s,%s,multitonecolor("%s",%s,"%s","%s"))'](tv[1],tv[2],cv[10],cv[11],cv[12],cv[13]) + 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 format("(%s,%s,%s)",cv[3],cv[4],cv[5]) + return formatters["(%s,%s,%s)"](cv[3],cv[4],cv[5]) elseif model == 3 then - return format("(%s,%s,%s)",cv[3],cv[4],cv[5]) + return formatters["(%s,%s,%s)"](cv[3],cv[4],cv[5]) elseif model == 4 then - return format("cmyk(%s,%s,%s,%s)",cv[6],cv[7],cv[8],cv[9]) + return formatters["cmyk(%s,%s,%s,%s)"](cv[6],cv[7],cv[8],cv[9]) elseif model == 5 then - return format('multitonecolor("%s",%s,"%s","%s")',cv[10],cv[11],cv[12],cv[13]) - else - return format("(%s,%s,%s)",cv[3],cv[4],cv[5]) --- this will become (see ** in meta-ini.mkiv) --- --- return format("%s",(cv[2])) + return formatters['multitonecolor("%s",%s,"%s","%s")'](cv[10],cv[11],cv[12],cv[13]) + else -- see ** in meta-ini.mkiv: return formatters["%s"]((cv[2])) + return formatters["(%s,%s,%s)"](cv[3],cv[4],cv[5]) end end else default = default or 0 -- rgb ! - return format("(%s,%s,%s)",default,default,default) + return formatters["(%s,%s,%s)"](default,default,default) end end +local function mpnamedcolor(name) + return mpcolor(texattribute[a_colorspace],l_color[name] or l_color.black) +end + local function mpoptions(model,ca,ta,default) -- will move to mlib-col - return format("withcolor %s",mpcolor(model,ca,ta,default)) + return formatters["withcolor %s"](mpcolor(model,ca,ta,default)) end -colors.mpcolor = mpcolor -colors.mpoptions = mpoptions +colors.mpcolor = mpcolor +colors.mpnamedcolor = mpnamedcolor +colors.mpoptions = mpoptions function colors.formatcolor(ca,separator) local cv = colorvalues[ca] @@ -666,83 +663,51 @@ function colors.defineintermediatecolor(name,fraction,c_one,c_two,a_one,a_two,sp end end ---~ local function f(one,two,i,fraction_one,fraction_two) ---~ local otf = fraction_one * one[i] + fraction_two * two[i] ---~ if otf > 1 then ---~ otf = 1 ---~ end ---~ return otf ---~ end - ---~ function colors.defineduocolor(name,fraction_one,c_one,fraction_two,c_two,global,freeze) ---~ local one, two = colorvalues[c_one], colorvalues[c_two] ---~ if one and two then ---~ fraction_one = tonumber(fraction_one) or 1 ---~ fraction_two = tonumber(fraction_two) or 1 ---~ local csone, cstwo = one[1], two[1] ---~ local ca ---~ if csone == 2 then ---~ ca = register_color(name,'gray',f(one,two,2,fraction_one,fraction_two)) ---~ elseif csone == 3 then ---~ ca = register_color(name,'rgb', f(one,two,3,fraction_one,fraction_two), ---~ f(one,two,4,fraction_one,fraction_two), ---~ f(one,two,5,fraction_one,fraction_two)) ---~ elseif csone == 4 then ---~ ca = register_color(name,'cmyk',f(one,two,6,fraction_one,fraction_two), ---~ f(one,two,7,fraction_one,fraction_two), ---~ f(one,two,8,fraction_one,fraction_two), ---~ f(one,two,9,fraction_one,fraction_two)) ---~ else ---~ ca = register_color(name,'gray',f(one,two,2,fraction_one,fraction_two)) ---~ end ---~ definecolor(name,ca,global,freeze) ---~ end ---~ end - - local function f(i,colors,fraction) - local otf = 0 - for c=1,#colors do - otf = otf + (tonumber(fraction[c]) or 1) * colors[c][i] - end - if otf > 1 then - otf = 1 - end - return otf +local function f(i,colors,fraction) + local otf = 0 + for c=1,#colors do + otf = otf + (tonumber(fraction[c]) or 1) * colors[c][i] + end + if otf > 1 then + otf = 1 end + return otf +end - function colors.definemixcolor(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 - local 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)) +function colors.definemixcolor(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 - definecolor(name,ca,global,freeze) + values[i] = v end + local 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) +end -- for the moment downward compatible local patterns = { "colo-imp-%s.mkiv", "colo-imp-%s.tex", "colo-%s.mkiv", "colo-%s.tex" } local function action(name,foundname) + -- could be one command context.startreadingfile() context.startcolorset { name } context.input(foundname) @@ -753,7 +718,7 @@ end local function failure(name) -- context.showmessage("colors",5,name) - report_colors("unknown: library '%s'",name) + report_colors("unknown library %a",name) end function colors.usecolors(name) @@ -877,3 +842,22 @@ end -- context.popcatcodes() -- 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 + +-- inspect(attributes.colors.spec("red")) +-- inspect(attributes.colors.spec("red socks")) |