- Home /
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.
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?
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.
Have you looked at something like this
https://docs.unity3d.com/Documentation/ScriptReference/Color.Lerp.html
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?
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.
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 ;)
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.
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.
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.
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/
Your answer
Follow this Question
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