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;
		if (v>1) {
			if (v>1) {
				if (v>1) {
					if (v>1) {
					} else oldColor.a=v;
				} else oldColor.b=v;
			} else oldColor.g=v;
		} else oldColor.r=v;

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.

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

“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 :wink:

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 ();


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!