[NTG-context] colors and contours in luametafun
Jacob Kauffmann
jacobkauffmann at me.com
Mon Jun 20 18:29:24 CEST 2022
Dear list,
> Am 01.09.2021 um 09:54 schrieb Jacob Kauffmann via ntg-context <ntg-context at ntg.nl>:
> - color by value in lmt_surface
> We want to show a surface plot of a function where the color indicates the function value (instead, or in addition to a light source).
> The easiest would be to have variable cf for the brightness factor (as it is), and in addition variables such as zt, minz, maxz, akin to the color formatter for lmt_contour.
> This becomes handy when colors are to be aligned with function values (e.g. negative = blue, zero = white, positive = red, see MWE attached).
I managed to achieve this by tweaking f_color and mp.lmt_surface_do in mlib-cnt.lmt in
ConTeXt ver: 2022.05.11 11:36 LMTX fmt: 2022.6.20
a bit.
Please find the diff and a MWE attached.
Cheers,
Jacob Kauffmann
--- context/tex/texmf-context/tex/context/base/mkxl/mlib-cnt.lmt 2022-06-20 18:11:51.000000000 +0200
+++ mlib-cnt.lmt 2022-06-20 18:26:22.000000000 +0200
@@ -1839,7 +1839,29 @@
local f_color = formatters [ [[
local math = math
- return function(f)
+ local min = math.min
+ local max = math.max
+ local abs = math.abs
+ local minz = %s
+ local maxz = %s
+
+ local color_value = 0
+ local color_step = mp.lmt_color_functions.step
+ local color_shade = mp.lmt_color_functions.shade
+
+ local function step(...)
+ return color_step(color_value,n,...)
+ end
+ local function shade(...)
+ return color_shade(color_value,n,...)
+ end
+ -- local function lin(l)
+ -- return l/n
+ -- end
+ %s
+ return function(z, f)
+ function_value = z
+ brightness_factor = f
return %s
end
]] ]
@@ -1898,7 +1920,7 @@
local dy = (ymax - ymin) / ny
local xt = xmin
--
- local minf, maxf
+ local minf, maxf, minz, maxz
--
-- similar as contours but no data loop here
--
@@ -1908,12 +1930,6 @@
return false -- fatal error
end
--
- local ccode = load(f_color(colorcode))
- local color = type(ccode) == "function" and ccode()
- if type(color) ~= "function" then
- return false -- fatal error
- end
- --
for i=0,nx do
local yt = ymin
for j=0,ny do
@@ -1946,7 +1962,16 @@
maxf = fac
end
--
- data[i][j] = { x, y, fac }
+ if not minz then
+ minz = z
+ maxz = z
+ elseif z < minz then
+ minz = z
+ elseif z > maxz then
+ maxz = z
+ end
+ --
+ data[i][j] = { x, y, fac, z }
--
yt = yt + dy
end
@@ -1957,6 +1982,16 @@
local range = maxf - minf
local cl = linecolor or 1
local enforce = attributes.colors.model == "cmyk"
+ --
+ -- local ccode = load(f_color(colorcode))
+ report("minz, maxz is (%s,%s)",minz,maxz)
+ report("colorcode is %s", colorcode)
+ local ccode = load(f_color(minz, maxz, preamble, colorcode))
+ local color = type(ccode) == "function" and ccode()
+ if type(color) ~= "function" then
+ return false -- fatal error
+ end
+ --
for i=0,nx-1 do
for j=0,ny-1 do
-- points
@@ -1985,12 +2020,13 @@
local z32 = z3[2]
local z41 = z4[1]
local z42 = z4[2]
+ local zout = z1[4]
-- if lines then
-- -- fill first and draw then, previous shapes can be covered
-- else
-- -- fill and draw in one go to prevent artifacts
-- end
- local cr, cg, cb = color(cf)
+ local cr, cg, cb = color(zout, cf)
if not cr then cr = 0 end
if not cg then cg = 0 end
if not cb then cb = 0 end
-------------- next part --------------
A non-text attachment was scrubbed...
Name: colors_and_surface_in_luametafun.pdf
Type: application/pdf
Size: 17337 bytes
Desc: not available
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20220620/1c691065/attachment-0001.pdf>
-------------- next part --------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: colors_and_surface_in_luametafun.tex
Type: application/octet-stream
Size: 795 bytes
Desc: not available
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20220620/1c691065/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20220620/1c691065/attachment-0001.sig>
More information about the ntg-context
mailing list