- Home /
Question by
siblount · Jan 11, 2021 at 10:12 PM ·
gameobjectcolorgameobjects
How do I mix multiple colors into one color?
Let's say I want one object to be the color of three mixed colors: red, yellow, pink. How would I do so?
Comment
Best Answer
Answer by jackmw94 · Jan 11, 2021 at 11:18 PM
This is a slightly bigger question than it sounds like as there are different methods of mixing colours; additive like light and subtractive like paint.
To get an additively mixed colour you could use an averaging function like so:
private Color AverageColour(Color[] colours)
{
float totalRed = 0f;
float totalGreen = 0f;
float totalBlue = 0f;
foreach (Color colour in colours)
{
totalRed += colour.r;
totalGreen += colour.g;
totalBlue += colour.b;
}
float numColours = colours.Length;
return new Color(totalRed / numColours, totalGreen / numColours, totalBlue / numColours);
}
It works fine but with lots of exceptions. For example, blue + yellow outputs a grayish color. There are other colors that give unexpected results. Is there another way to mix these colors?