Skip to content

Commit b4c7be0

Browse files
committed
WIP New ColorHelper sample docs
1 parent 5247372 commit b4c7be0

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

components/Helpers/samples/ColorHelper.md

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,48 @@ issue-id: 0
1212
icon: Assets/ColorHelper.png
1313
---
1414

15-
The `ColorHelper` can convert various formats (text names, HTML hex, HSV and HSL) to `Windows.UI.Colors` and back. Some examples:
15+
The `ColorHelper` contains various methods for color parsing and color manipulation for `Windows.UI.Colors`
16+
17+
## Color parsing
18+
19+
The `ColorHelper` contains various methods for parsing colors in specific formats, or simply `ParseColor` can be used to auto-detect the format.
20+
21+
Each parsing method has both a "Parse" and "TryParse" pattern version .
22+
23+
```csharp
24+
// Parse by hex string
25+
Color color = ColorHelper.ParseHexColor("#FFFF0000");
26+
27+
// Parse by hsl string
28+
ColorHelper.ParseHslColor("hsl(0, 1, 0)");
29+
30+
// Try parse by name
31+
ColorHelper.TryParseColorName("Red", out Color color);
32+
```
33+
34+
## HSL/HSV Manipulation
35+
36+
The `ColorHelper` package includes methods for manipulating colors in the HSL or HSV color space.
1637

1738
```csharp
18-
Color color = "#FFFF0000".ToColor();
19-
Color color = "Red".ToColor();
20-
string hex = Colors.Red.ToHex();
21-
HslColor hsl = Colors.Red.ToHsl();
22-
HsvColor hsv = Colors.Red.ToHsv();
23-
int i = Colors.Red.ToInt();
39+
// Adjust a color's hue (to blue)
40+
color = color.WithHue(240);
41+
42+
// Adjust a color's saturation (to fully saturated)
43+
color = color.WithSaturation(1);
44+
```
45+
46+
The package also includes models to store the color as either a HSV or HSL color.
47+
48+
A `Windows.UI.Color` can be converted to an a `HsvColor` or `HslColor` either by using the `.ToHsv()` and `.ToHsl()` methods, or by using an explicit cast on a color.
49+
50+
`HslColor` and `HsvColor` will implicity cast back to a `Windows.UI.Color` when needed.
51+
52+
```csharp
53+
// Convert to hsl
54+
Color color;
55+
var hslColor = (HslColor)color;
56+
57+
// new SolidColorBrush takes a Color, but the HslColor can be implicitly cast to match
58+
var brush = new SolidColorBrush(hslColor);
2459
```

0 commit comments

Comments
 (0)