RicardoTM wrote: ↑March 7th, 2024, 8:48 pm
Alright, it's practically finished, now I need your help to optimize it if needed, as this is practically my first time on Lua for real.
but before I post it I just need help with some final thing, there are 3 functions that generate shades, tints and tones. As I wrote it it only can generate 2 at a time, so I need some form of way to let the user select how many they want. All 3 functions look like this:
Code: Select all
--------------------------------------------------------------------------
--- Returns shades, 2 colors based on 1
--- to separate string see splitComplementary ^^^
--- color r,g,b 0-255
--- return r,g,b,r,g,b 0-255
------------------------------------------------------------------------------
function shades(color,amount)
local amount = amount or 10
local color1 = shiftLight(color, -amount)
local color2 = shiftLight(color, -amount-amount)
return string.format('%s,%s',color1,color2)
end
I want it to be function shades(color,N,amount).
Where N is the number of shades to generate
amount is how much the color dims , if no amount entered, then it should be relative to the number of shades.
how it should work:
https://colors.dopely.top/color-toner/3e97c1-shades-18
Just to clarify, this approach will need the user to separate the colors using substitute on a measure (which would also need optimization):
Code: Select all
[shade2]
measure=String
string=[&test:shades('[#Color]')]
RegExpSubstitute=1
Substitute="(\d+,\d+,\d+),(\d+,\d+,\d+)":"\2"
DynamicVariables=1
Captura de pantalla 2024-03-07 145514.jpg
I don't know how the
shiftLight() functions work - that will be your job to adapt -, but I'd do something along the lines of (
N + 1 is
count below):
Code: Select all
function getshades(color, count, amount)
-- get the r, g, and b from color here
if amount == nil then amount = max(r, g, b) / count end
local shades = {color}
for i = 1, count do
-- subtract proportionally from r, g, and b inside shiftLight()
shades[i + 1] = shiftLight(shades[i], - amount)
end
return table.concat(shades, ';')
end
If adapted correctly, the above should produce all the shades, including the original color (which is used as a base to successively
shiftLight() for
count times in the FOR loop in the script). I'm not that much into
string.format(), and in this case
table.concat() is more suited since you build a
shades table anyway. The result would look like
'0,139,204;0,104,153;...' for better clarity as to where each
'r,g,b' set in the string ends. I suppose that's more or less how the site you linked to computes the
amount, from what I could tell. Didn't use the variable number of parameters above since, after all, there is a single "optional" parameter, aka
amount, and although it will occupy more space than not writing it when you call the function, it can easily be made to "not exist" by passing
nil as the parameter. If you really want to use it, just replace
amount with
... in the function definition and use
arg[3] instead of
amount (or declare a local
amount set to
arg[3]) in the function body (
arg is a table holding all the arguments / parameters to a function). For the record, a quick way to refer to the number of elements in
sometable is to write
#sometable.