public static HslColor FromFloatColor(ref FloatColor c) { float r = c.r; float g = c.g; float b = c.b; float min = Math.Min(Math.Min(r, g), b); float max = Math.Max(Math.Max(r, g), b); float delta = max - min; float h = 0; float s = 0; float l = (max + min) * 0.5f; if (delta > float.Epsilon) { if (l < 0.5f) { s = (delta / (max + min)); } else { s = (delta / (2.0f - max - min)); } if (r == max) { h = (g - b) / delta; } else if (g == max) { h = 2f + (b - r) / delta; } else if (b == max) { h = 4f + (r - g) / delta; } } return(new HslColor(h, s, l)); }
public static HsvColor FromFloatColor(ref FloatColor c) { HsvColor hsv = new HsvColor(); int r = (int)(c.r * 255); int g = (int)(c.g * 255); int b = (int)(c.b * 255); int max = Math.Max(r, Math.Max(g, b)); int min = Math.Min(r, Math.Min(g, b)); float delta = max - min; float h = 0; if (delta > float.Epsilon) { if (r == max) { h = (g - b) / delta; } else if (g == max) { h = 2f + (b - r) / delta; } else if (b == max) { h = 4f + (r - g) / delta; } } hsv.H = h * 360; hsv.S = (max == 0) ? 0 : 1.0f - (1.0f * (float)min / (float)max); hsv.V = max / 255.0f; return(hsv); }