перевод hex в hsl C#
- User
перевод hex в hsl C#
- Assistant
Код для перевода цвета из формата HEX в формат HSL на языке C#:
public static HSLColor HexToHSLColor(string hexValue) { hexValue = hexValue.TrimStart('#'); int red = int.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber); int green = int.Parse(hexValue.Substring(2, 2), NumberStyles.HexNumber); int blue = int.Parse(hexValue.Substring(4, 2), NumberStyles.HexNumber); return RGBToHSLColor(red, green, blue); } public static HSLColor RGBToHSLColor(int red, int green, int blue) { double r = red / 255.0; double g = green / 255.0; double b = blue / 255.0; double max = Math.Max(Math.Max(r, g), b); double min = Math.Min(Math.Min(r, g), b); double delta = max - min; double h = 0; if (delta != 0) { if (max == r) { h = 60 * ((g - b) / delta % 6); } else if (max == g) { h = 60 * ((b - r) / delta + 2); } else { h = 60 * ((r - g) / delta + 4); } } if (h < 0) { h += 360; } double l = (max + min) / 2; double s = 0; if (l != 0 && l != 1) { s = delta / (1 - Math.Abs(2 * l - 1)); } return new HSLColor(h, s, l); } - User
перевод hex в hsb
- Assistant
Код для перевода цвета из формата HEX в формат HSB (HSV) на языке C#:
public static HSBColor HexToHSBColor(string hexValue) { hexValue = hexValue.TrimStart('#'); int red = int.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber); int green = int.Parse(hexValue.Substring(2, 2), NumberStyles.HexNumber); int blue = int.Parse(hexValue.Substring(4, 2), NumberStyles.HexNumber); return RGBToHSBColor(red, green, blue); } public static HSBColor RGBToHSBColor(int red, int green, int blue) { double r = red / 255.0; double g = green / 255.0; double b = blue / 255.0; double max = Math.Max(Math.Max(r, g), b); double min = Math.Min(Math.Min(r, g), b); double delta = max - min; double h = 0; double s = 0; double v = max; if (max != 0) { s = delta / max; } if (delta != 0) { if (max == r) { h = 60 * ((g - b) / delta % 6); } else if (max == g) { h = 60 * ((b - r) / delta + 2); } else { h = 60 * ((r - g) / delta + 4); } } if (h < 0) { h += 360; } return new HSBColor(h / 360, s, v); }