Hi all,
I have written a lua function, with the help of CheatGPT ChatGPT, since my lua skills are less than zero, which takes two colours and blends them together . This function is part of another function which setups a bytemap and fills each point with the blended colour. I can only do this if I use "hard wired" named colours (see the code below). I would like to be able to select colours from a Palette of colours and pass them to the lua function instead. I have tried ChatGPT again and I've looked though the wiki and some manuals but can't find a solution. I realise that  function MP.MakeByteMap(i) would have to become function MP.MakeByteMap(i,aColour1,aColour2), however I'm guessing that I'll have to separate the \MPcolor{aColour} out to red part, green part, blue part and multiply by 255 and pass that to the function?  Any hints or suggestions on a solution to this would be greatly appreciated.
Best Wishes
Keith

%%%%%%%%
\startluacode
local namedColors = {
red = { 255, 0, 0},
green = { 0, 255, 0},
blue = { 0, 0, 255},
white = { 255, 255, 255},
black = { 0, 0, 0},
yellow = { 255, 255, 0},
cyan = { 0, 255, 255},
magenta = { 255, 0, 255},
gray = { 128, 128, 128}
}
local function blendColors(color1, color2, t)
-- Ensure color1 and color2 are in the format {r, g, b}
-- Perform linear interpolation (lerp) on each color channel
local newColor = {
color1[1] * (1 - t) + color2[1] * t,
color1[2] * (1 - t) + color2[2] * t,
color1[3] * (1 - t) + color2[3] * t
}
return newColor
end
local random = math.random
local setbytemap = mp.setbytemap
function MP.MakeByteMap(i)
mp.newbytemap(i,1000,1000,3)
local c1 = namedColors["white"]
local c2 = namedColors["black"]
local c3 = namedColors["red"]
local c4 = namedColors["yellow"]
local c5 = namedColors["cyan"]
local c6 = namedColors["magenta"]
for x=0,1000 do
for y=0,1000 do
noise = math.random(1, 1000) / 1000 -- Generate a random noise value between 0 and 1
--calculates the color based on the noise value
--local mixedColor = blendColors("cyan","magenta", noise) -- Should give a blended color
local mixedColor = blendColors(c5, c6, noise) -- Should give a blended color
setbytemap(i,
x, y,
(mixedColor[1]), (mixedColor[2]), (mixedColor[3])
)
end
end
end
\stopluacode
\usecolors[crayola]
\definepalet[MyColors][1=SkyBlue,2=Orange,3=PigPink,4=BananaMania,5=BrilliantRose]
\setuppalet[MyColors]
\starttext
\startMPpage%[Page 7]
numeric p;
p := 4;
lua.MP.MakeByteMap(p);
fill
unitsquare xyscaled (999,999)
withbytemap p;
\stopMPpage
\stoptext
%%%%%%%%%%%%
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to