A simple and easy-to-use color space conversion tool that supports mutual conversion between HSL/HSV and RGB color spaces.
Install-Package KlxPiao.ColorTool
// Method 1: Implicit conversion via HslColor
var hsl = HslColor.FromHsl(0, 1, 1); // Using float parameters (H: 0-360°, S/L: 0-1)
Color color1 = hsl; // Implicit conversion to Color
// Method 2: Direct creation from string
var hsl2 = HslColor.FromString("0, 100%, 100%");
Color color2 = hsl2; // Implicit conversion to Color
// Method 3: Quick methods (no explicit instantiation)
Color color3 = HslColor.FromHsl(0, 1, 1);
Color color4 = HslColor.FromString("0, 100%, 100%");
// Get HSL values via implicit conversion
HslColor hsl = Colors.Red; // Implicit conversion to HslColor
float h = hsl.Hue; // Output: 0f (0-360 range)
float s = hsl.Saturation; // Output: 1f (0-1 range)
float l = hsl.Lightness; // Output: 1f (0-1 range)
// Get string representation
string hslString1 = hsl.ToString(); // Output: "0, 100%, 100%"
// Get string via extension method
string hslString2 = Colors.Red.ToHslString(); // Output: "0, 100%, 100%"
- Parameter Ranges:
FromHsl()
acceptsHue: 0-360
,Saturation/Lightness: 0-1
FromString()
requires format:"H, S%, L%"
- Compatibility:
- Supports both spaced (e.g.,
"0, 100%, 50%"
) and unspaced (e.g.,"0,100%,50%"
) formats - Parameters are automatically clamped to valid ranges (e.g., 1.5 → 1.0, 721 → 1)
- When S/L values include '%', the symbol is removed and divided by 100. Without '%', values remain unchanged.
- Supports both spaced (e.g.,
- Implicit Conversion Equivalence:
- Implicit conversion is equivalent to
color.ToHsl()
orhslColor.ToColor()
- Implicit conversion is equivalent to
- Exception Handling:
- Invalid strings throw
FormatException
- Invalid strings throw