public static Color RandomHueColor(float saturation, float lightness) { var hue = Random.Range(0, 360f); var hsl = new RGBHSL(hue, saturation, lightness); return(ColorConverter.ConvertRGBHSL(hsl)); }
public static Color ConvertRGBHSL(RGBHSL c) { float R = c.R / 255f; float G = c.G / 255f; float B = c.B / 255f; float A = c.A / 255f; return(new Color(R, G, B, A)); }
public RGBHSL(float H, float S, float L, byte A = 255) { this.H = H; this.S = S; this.L = L; this.A = A; RGBHSL c = ColorConverter.ConvertHSL(H, S, L); R = c.R; G = c.G; B = c.B; }
/// <summary> /// /// </summary> /// <param name="hue">0..359</param> /// <param name="saturation">0..1</param> /// <param name="lightness">0..1</param> /// <returns></returns> public static RGBHSL ConvertHSL(float hue, float saturation, float lightness) { float C = lightness * saturation; float X = C * (1 - Mathf.Abs((hue / 60) % 2 - 1)); float M = lightness - C; Vector3 rgbTemp = new Vector3(0, 0, 0); hue = ClampExclusive(0, 360, hue); if (0 <= hue && hue < 60) { rgbTemp = new Vector3(C, X, 0); } else if (60 <= hue && hue < 120) { rgbTemp = new Vector3(X, C, 0); } else if (120 <= hue && hue < 180) { rgbTemp = new Vector3(0, C, X); } else if (180 <= hue && hue < 240) { rgbTemp = new Vector3(0, X, C); } else if (240 <= hue && hue < 300) { rgbTemp = new Vector3(X, 0, C); } else if (300 <= hue && hue < 360) { rgbTemp = new Vector3(C, 0, X); } byte r = (byte)Mathf.RoundToInt((rgbTemp.x + M) * 255f); byte g = (byte)Mathf.RoundToInt((rgbTemp.y + M) * 255f); byte b = (byte)Mathf.RoundToInt((rgbTemp.z + M) * 255f); RGBHSL c = new RGBHSL(r, g, b, hue, saturation, lightness); return(c); }