I just want to make a system to mix an array of colors as if it was paint,
ex: yellow+blue=green
but nothing has worked. here is the code I came up with, it only works in cases with rgb
example red+blue=purple but yellow+blue=white
func mixcolors(colors:Array):
var output:Color
var step = 0
for c in colors:
if c is Color:
if step == 0:
output = c
else:
output = (output/2 + c/2).clamp()
step += 1
return output
The colors can not be properly mixed using RGB. That is because the amount of sensors in the eyes are not the same for r g and b.
You should research based on that.
What you want is subtractive color mixing. Mathematically pure subtractive mixing is just multiplication. This is perceptually not satisfying in many cases but you can start by that as itās trivial to implement. Note that your primaries in the pure subtractive model are not blue and red but cyan and magenta, and mixing blue and yellow will result in black instead of green.
Thereās a formula that emulates real world mixing a bit better so it might be acceptable for your use case:
When doing it with Godotās Color object, for best results you might want to convert input colors to linear color space and then convert the result back to srgb color space.
Mixing green from blue and yellow will still be a problem though.
Thereās apparently some new-ish algorithm that emulates paint mixing in perceptually satisfying way.
I havenāt looked into it but hereās the siggraph presentation:
EDIT: Thereās Python source code available under CC BY-NC 4.0 license: