Last active
June 1, 2022 19:32
-
-
Save FaffyWaffles/317331ca096931cfd2d49c5d4442156f to your computer and use it in GitHub Desktop.
Unity script to create n number of evenly distributed random colors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
Author
FaffyWaffles
commented
Apr 1, 2022

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