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
0
Question by Tanoshimi2000 · Apr 24, 2014 at 01:33 AM · colorloopcolorpicker

How to loop through colors

I have racing cars and want to customize the color. Steer right and color goes up one. Steer left and color goes down one. In other systems, I can access the color as a Long and just increment that by 1 (or 10, or 100). Using the RGBA individually, I have it so that it increments R until R>1, then resets that to 0 and increments G, and so on.

 //Change color
     if (Input.GetButton("Fire3")){
         var oldColor : Color;
         var v;
         var addV = 0.1; // or .01, or .001 depending upon speed I want.
         oldColor = transform.GetComponent("CarColor").car_color;
         v=oldColor.r+addV;
         if (v>1) {
             oldColor.r=0;
             v=oldColor.g+addV;
             if (v>1) {
                 oldColor.g=0;
                 v=oldColor.b+addV;
                 if (v>1) {
                     oldColor.b=0;
                     v=oldColor.a+addV;
                     if (v>1) {
                         oldColor.a=0;
                     } else oldColor.a=v;
                 } else oldColor.b=v;
             } else oldColor.g=v;
         } else oldColor.r=v;
         transform.GetComponent("CarColor").car_color=oldColor;

Is this the best way to handle this, or is there a better way to gradually shift through colors? I'd rather not use a colorpicker, since we're building for a steering wheel control and not a mouse interface.

Comment
Add comment · Show 6
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 FirePlantGames · Apr 24, 2014 at 01:42 AM 0
Share

Would it be possible to have something like this, when you press on a gui toggle that's the color you're editing? like, have a red,green,and blue toggle then just use your 'steering wheel' interface?

avatar image Tanoshimi2000 · Apr 24, 2014 at 01:51 AM 0
Share

I suppose I could. The wheel has a lot of buttons including the standard X,O,Triangle,Square. I could let each of those be RGBA and then the wheel changes the color. Still, that's not as slick as steering left and right to scroll cars, gas to select, then left and right to change colors, gas to select. I appreciate the comment. Not exactly what I was looking for, but certainly something to consider.

avatar image getyour411 · Apr 24, 2014 at 01:56 AM 0
Share

Have you looked at something like this

https://docs.unity3d.com/Documentation/ScriptReference/Color.Lerp.html

avatar image FirePlantGames · Apr 24, 2014 at 02:06 AM 0
Share

oh! I didn't realize that you're using an actual wheel, thought it was a in-game kind of thing. Umm...so, do you want to have select colors be available for cars? or the whole hue spectrum?

avatar image Tanoshimi2000 · Apr 24, 2014 at 10:51 AM 0
Share

Initially I wanted the whole spectrum, but since it takes a long time to cycle through 16.7 million colors, I'm now thinking about running all the yellows, then reds, then greens, then blues. Or something like that.

Show more comments

2 Replies

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

Answer by Bunny83 · Apr 24, 2014 at 02:33 AM

If you can live with subdivides which are a power of two (2,4,8,16,32,64,...) you can "translate" your color into a linear integer value and only use a certain bit-count for each color:

 // C#
 
 public static int Col2Int(Color aColor, int aBitCount)
 {
     int maxValue = 2 << aBitCount - 1;
     int r = Mathf.FloorToInt(aColor.r * maxValue);
     int g = Mathf.FloorToInt(aColor.g * maxValue);
     int b = Mathf.FloorToInt(aColor.b * maxValue);
     return r | (g << aBitCount) | (b << aBitCount*2);
 }
 
 public static Color Int2Col(int aValue, int aBitCount)
 {
     int maxValue = 2 << aBitCount - 1;
     float r = ((float)(( aValue                ) & maxValue)) / maxValue;
     float g = ((float)(( aValue >> aBitcount   ) & maxValue)) / maxValue;
     float b = ((float)(( aValue >> aBitcount*2 ) & maxValue)) / maxValue;
     return new Color(r,g,b);
 }

With those two methods you can "convert" a color into an integer representation with a certain (bit) precision.

For example:

 int val = Col2Int(someColor, 3);  // 3 bits per color channel
 //  XXXX XXXB BBGG GRRR

"val" represents the color as a 9 bit value so each color channel can have 8 different values (2^3 == 8). So the value can have 512 different values. This int-value you can increment or decrement as you like.

When done you can use "Int2Col" to convert the integer value back to a color:

 Color col = Int2Col(val, 3);

Note: I just wrote those two methods from scratch and don't have the time to test them, but they should work ;)

Comment
Add comment · Show 3 · 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 Tanoshimi2000 · Apr 24, 2014 at 10:55 AM 0
Share

Nice answer. That was my original approach. Or similar. I actually multiplied them out to longs by taking R+(G*256)+(B*65536), then adding one, then converting it back. It kinda worked, and may end up being what I go with, but the current system works more smoothly. I guess I was just trying to see if there was an actual RGBA function, and it doesn't appear so. Still, this is a correct answer, so you get the vote.

avatar image Bunny83 · Apr 24, 2014 at 02:35 PM 0
Share

If you simply want 8bit per channel you can utilize the Color32 struct:

 public static int Col2Int(Color aColor)
 {
     Color32 c = aColor
     return c.r | (c.g << 8) | (c.b << 16);
 }
  
 public static Color Int2Col(int aValue)
 {
     Color32 c = new Color32((byte)(aValue),(byte)(aValue >> 8), (byte)(aValue >> 16));
     return C;
 }

The resulting integer has a 24 bit resolution. You can increment / decrement it by any amount you like, but if you use values that aren't power-of-two the color values won't be distributed evenly and it's possible that you get two times almost the same color.

avatar image Tanoshimi2000 · Apr 24, 2014 at 03:49 PM 0
Share

Byteshifting works, but now I'm thinking of taking advantage of the separate channels. I've seen a similar effect in other games where the car (or other object) will run the spectrum of red, then green, then blue, then yellow/orange/brown, then B&W. I believe it will be easier for a player to find the color they want using that approach. Or simply storing an array of acceptable car colors and looping though that; but that will limit the number of colors I can use.

A quick google search outside of Unity produced this page (http://krazydad.com/tutorials/makecolors.php) that shows something like what I'd like to do.

avatar image
0

Answer by Tanoshimi2000 · Apr 24, 2014 at 07:23 PM

After reading other people's answers, and googling how to do it outside of Unity, I finally implemented the solution I wanted.

[Here's the final effect][1]

And here's the code I used to get that effect.

 void ColorUp ()
 {
     currentIndex += 0.1f;
     //if (currentIndex>1) currentIndex=0f;
     SetColor ();
 }

and

 void SetColor()
 {
     float red = Mathf.Sin (frequency1 * currentIndex + phase1);
     float grn = Mathf.Sin (frequency2 * currentIndex + phase2);
     float blu = Mathf.Sin (frequency3 * currentIndex + phase3);
     car_color.r = red;
     car_color.g = grn;
     car_color.b = blu;
         Debug.Log (car_color);
 }

Let me know what you think of the effect. I like it, and think it's a great way to customize the car.

Thanks to all who pitched in! [1]: http://www.dcjoys.com/Games/Time_Trials/Chamelian/

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

22 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

Related Questions

Changing two different objects renderer colour 1 Answer

Instantiate color change loop 0 Answers

Material Colour Change, Loop 2 Answers

Change Color of Material in Loop 1 Answer

Runtime Equivelent of EditorGUIUtility.HSVToRGB() 3 Answers


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