Better Random Colors - Helpful Debug Script

This simple script will create n number of evenly distributed random colors. This is very helpful for debugging, and much better than using Random.Color, which often leads to dilute colors that are hard to tell apart.

Hopefully this is helpful to someone.

using System.Collections.Generic;
using UnityEngine;

public class DistinctRandomColors : MonoBehaviour
{
public Color[] colors;

private void DistinctRandomColors(int n) ///n == number of colors
    {
        colors = new Color[n];
        int colorCount = colors.Length;
        Color initColor = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 1f, 1f);

        colors[0] = initColor;
        float hue, saturation, value;
        Color.RGBToHSV(initColor, out hue, out saturation, out value);

        float normalizedHue = Mathf.Clamp01(hue);

        for (int i = 1; i < colorCount; i++)
        {
            float angle = i * (360 / colorCount);
           
           
            angle += hue;

            float normalized = (angle / 360)+normalizedHue;

            Color newColor = Color.HSVToRGB(normalized, saturation, value);
            colors[i] = newColor;        
        }
    }
}
3 Likes

in this example, n=3…

2 Likes

Edit – added a % for large initial color hues.

using System.Collections.Generic;
using UnityEngine;

public class DistinctRandomColors : MonoBehaviour
{
public Color[] colors;

private void DistinctRandomColors(int n) ///n == number of colors
    {
        colors = new Color[n];
        int colorCount = colors.Length;
        Color initColor = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 1f, 1f);

        colors[0] = initColor;
        float hue, saturation, value;
        Color.RGBToHSV(initColor, out hue, out saturation, out value);

        float normalizedHue = Mathf.Clamp01(hue);

        for (int i = 1; i < colorCount; i++)
        {
            float angle = i * (360 / colorCount);
         
         
            angle += hue;

            float normalized = (angle / 360)+normalizedHue;

            Color newColor = Color.HSVToRGB(normalized % 1, saturation, value);
            colors[i] = newColor;      
        }
    }
}
3 Likes

Another Bug Fix: Now it can have more than 360 colors in an array.

    void DistinctRandomColors(int colorCount, out Color[] colors)
    {
        colors = new Color[colorCount];
        Color initColor = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 1f, 1f);

        colors[0] = initColor;
        float hue, saturation, value;
        Color.RGBToHSV(initColor, out hue, out saturation, out value);

        float normalizedHue = Mathf.Clamp01(hue);

        for (int i = 1; i < colorCount; i++)
        {
            float angle = (((360 * colorCount) - (360 * i)) / colorCount);
            float normalized = (angle / 360) + normalizedHue;
            Color newColor = Color.HSVToRGB(normalized % 1, saturation, value);
            colors[i] = newColor;
        }
    }
1 Like

I’ve been thinking about writing one of these up, but now you have done it. Thanks.

I was not going to use it for debug, but for nation colors that can be easy to tell apart. I might have to add some code to it for blacks, whites and browns, though, make it three dimensional, so that dark red will opposite light green.

1 Like