Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by TimBorquez · Jun 11, 2014 at 11:17 PM · colorcolorscolor32

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jun 13, 2014 at 03:16 PM 0
Share

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.

avatar image TimBorquez · Jun 15, 2014 at 12:13 PM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yogeshkainthola · Jul 14, 2016 at 09:39 AM 0
Share

can you give complete program ... thanks in advance

avatar image
2

Answer by Eric5h5 · Jun 11, 2014 at 11:29 PM

 startColor = (Color.blue + Color.red + Color.yellow) / 3.0;
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ben.aten · Jun 03, 2015 at 09:22 AM 0
Share

Hi Eric, yellow + blue is not working using this functionality, it giving another colour ins$$anonymous$$d of green. What could be the cause.

avatar image Eric5h5 · Jun 03, 2015 at 05:32 PM 0
Share

Yellow + blue does not equal green. Yellow is RGB(1, 1, 0), blue is RGB(0, 0, 1), so the result is white.

avatar image Owen-Reynolds · Jun 03, 2015 at 06:47 PM 0
Share

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.

avatar image ben.aten · Jun 04, 2015 at 06:38 AM 0
Share

http://trycolors.com

avatar image Eric5h5 · Jun 04, 2015 at 06:44 AM 1
Share

That's not relevant at all. You're using an RGB screen (additive), not mixing paint (subtractive).

Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wightwhale · Feb 09, 2016 at 08:59 PM 1
Share

Why do you use (3 * 1.0f)? it would be easier to use 3f

avatar image
0

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;
 
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

[Resolved] Game view displays the wrong colors? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges