So, as of now, I am attempting to make a brush that does 2 things, the first is that it jitters to create some sort of “texture”, and the second is that it draws a random colour that has the same amount of luminance as the foreground/input colour. For the most part, I’ve managed to successfully implement it, but for some reason, it has a bias for blue in low luminance level, and a bias for green in high luminance level, any help please? (Firealpaca uses Lua, just a reminder) (also, I started learning coding yesterday, so, apologies for my sloppy coding work)
Firstdraw = true
function param1()
-- minimum, maximum, default value
return "random" , 0 , 100 , 2
end
function main(x, y, p)
if Firstdraw then
r, g, b = bs_fore()
luminance = (r * 0.3) + (g * 0.59) + (b * 0.11)
luminanceStart = luminance
if luminance <= 28 then
blue = math.random(0, luminance/0.11)
elseif luminance >= 227 then
blue = math.random((luminance-227)/0.11, 255)
else
blue = math.random(0, 255)
end
luminance = luminance - blue*0.11
if luminance <= 76 then
red = math.random(0, luminance/0.3)
elseif luminance >= 179 then
red = math.random((luminanceStart-179)/0.3, 255)
else
red = math.random(0, 255)
end
luminance = luminance - red*0.3
green = luminance/0.59
if green > 255 then
green = 255
end
luminanceEnd = (red * 0.3) + (green * 0.59) + (blue * 0.11)
local sx = tostring(luminanceStart)
local sv = tostring(luminanceEnd)
local sy = tostring(red)
local sp = tostring( green)
local sd = tostring( blue)
bs_debug_log( "sx: " .. sx .. " sv: " .. sv .. " sy: " .. sy .. " sp: " .. sp.. " sd: " .. sd )
bs_debug_log("")
end
local width = bs_width()
local x, y = x+math.random(bs_param1()*-1, bs_param1())*p*width/10, y+math.random(bs_param1()*-1, bs_param1())*p*width/10
local opacity = bs_opaque() * 255
bs_ellipse(x, y, width, width, 0, red, green, blue, opacity)
Firstdraw = false
return 1
end
I honestly don’t know how to fix this, I expected it to output a random colour with the same luminance, but it instead has a bias for green or blue.
resulting colours, green bias on high luminance, blue bias on low luminance
another problem that rose from this, is that on high luminance value, sometimes the green just goes over 255, making it return back to small numbers from 0, so I capped it to 255, but as a result, it reduces the luminance value of the resulting colour. I figured that the reason this happened is because of the green bias
Indonesiaball is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
After you randomly select the blue value, the red component implies a minimum and maximum value, so you can’t use 0 and 255 as random ranges:
-- 0.3r + 0.59g = luminance
-- r = (luminance - 0.59g) / 0.3
local red_min = (luminance - 255 * 0.59) / 0.3
local red_max = luminance / 0.3
if red_min < 0 then red_min = 0 end
if luminance <= 76 then
red = math.random(red_min, luminance/0.3)
elseif luminance >= 179 then
red = math.random(math.max(red_min, (luminanceStart-179)/0.3), red_max)
else
red = math.random(red_min, red_max)
end