- Home /
Best way to mix color values?
right now i am using the halfway point of lerp to mix two colors:
startColor = Color.Lerp(Color.blue, Color.red, 0.5);
but say i wanted to mix 3 colors at once, like red + blue + yellow
i have a feeling i am missing something very simple
But the result will be an ugly medium grey. $$anonymous$$ath-mixing even two colors often gives ugly results.
I only do it in better-than-nothing situations, like when I have to turn from blue to red, a "snap" happens to look bad in actual gameplay, so make very quick lerp.
Sorry for not responding after 3 people were nice enough to answer, Unity answers did not email me like it usually does. . .
Thanks everyone for the help, I get it now. I am now finding that what I think I want is to simulate mixing subtractive colors
Answer by Bunny83 · Jun 11, 2014 at 11:39 PM
The way you use lerp, all you're actually doing is calculating the arithmetic mean. That means you can simply add all colors together and divide the result be the number of colors.
So your example is the same as:
startColor = (Color.blue + Color.red) / 2;
Note: With Color32 it doesn't work that way, since each component is just a byte, so you can't exceed the max value which Color can since each component is a float. However since Color can be implicit converted into Color32 and the other way round, you can do this calculation with Color.
Here's a method which would combine multiple colors:
// C#
public static Color CombineColors(params Color[] aColors)
{
Color result = new Color(0,0,0,0);
foreach(Color c in aColors)
{
result += c;
}
result /= aColors.Length;
return result;
}
This method can be used like this:
startColor = CombineColors(Color.blue, Color.red, Color.yellow);
can you give complete program ... thanks in advance
Answer by Eric5h5 · Jun 11, 2014 at 11:29 PM
startColor = (Color.blue + Color.red + Color.yellow) / 3.0;
Hi Eric, yellow + blue is not working using this functionality, it giving another colour ins$$anonymous$$d of green. What could be the cause.
Yellow + blue does not equal green. Yellow is RGB(1, 1, 0), blue is RGB(0, 0, 1), so the result is white.
And in-between yellow and blue is grey. To see this, take any color gradient tool (Photoshop, Gimp, PowerPoint ... ) and make a yellow-to-blue gradient. The middle will be grey.
For the math, same #'s as Eric5h5 lists:( (110)+(001))/2=(all 0.5's)=grey.
That's not relevant at all. You're using an RGB screen (additive), not mixing paint (subtractive).
Answer by christoph_r · Jun 11, 2014 at 11:28 PM
float r = (color1.r + color2.r + color3.r) / (3f);
float g = (color1.g + color2.g + color3.g) / (3f);
float b = (color1.b + color2.b + color3.b) / (3f);
startColor = new Color(r, g, b, 1.0f);
It works the same for the alpha, should you need that, as well.
Why do you use (3 * 1.0f)? it would be easier to use 3f
Answer by Cherno · Feb 09, 2016 at 09:08 PM
I can provide these two functions that I found on the web. The first one is probably the relevant one.
public Color CombineColors(params Color[] aColors)
{
Color result = new Color(0,0,0,0);
foreach(Color c in aColors)
{
result += c;
}
result /= aColors.Length;
return result;
}
public Color TransformHSV(
Color color, // color to transform
float H, // hue shift (in degrees)
float S, // saturation multiplier (scalar)
float V // value multiplier (scalar)
)
{
float VSU = V*S*Mathf.Cos(H*Mathf.PI/180);
float VSW = V*S*Mathf.Sin(H*Mathf.PI/180);
Color ret = new Color();
ret.r = (.299f*V+.701f*VSU+.168f*VSW)*color.r
+ (.587f*V-.587f*VSU+.330f*VSW)*color.g
+ (.114f*V-.114f*VSU-.497f*VSW)*color.b;
ret.g = (.299f*V-.299f*VSU-.328f*VSW)*color.r
+ (.587f*V+.413f*VSU+.035f*VSW)*color.g
+ (.114f*V-.114f*VSU+.292f*VSW)*color.b;
ret.b = (.299f*V-.3f*VSU+1.25f*VSW)*color.r
+ (.587f*V-.588f*VSU-1.05f*VSW)*color.g
+ (.114f*V+.886f*VSU-.203f*VSW)*color.b;
ret.a = 1f;
if(ret.r < 0) {ret.r = 0;}
if(ret.g < 0) {ret.g = 0;}
if(ret.b < 0) {ret.b = 0;}
return ret;
}
Your answer
Follow this Question
Related Questions
how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers
Vertex colors as diffuse in lightmapping. 0 Answers
My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers
Color of some instantiated objects differs from the normal ones. 1 Answer