Skip to content

Instantly share code, notes, and snippets.

@FaffyWaffles
Last active June 1, 2022 19:32
Show Gist options
  • Select an option

  • Save FaffyWaffles/317331ca096931cfd2d49c5d4442156f to your computer and use it in GitHub Desktop.

Select an option

Save FaffyWaffles/317331ca096931cfd2d49c5d4442156f to your computer and use it in GitHub Desktop.
Unity script to create n number of evenly distributed random colors.
using System.Collections.Generic;
using UnityEngine;
public class DistinctRandomColors : MonoBehaviour
{
public Color[] colors;
void RandomColors(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;
}
}
}
@FaffyWaffles
Copy link
Author

Color Example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment