Implement IFormattable on HslColor and HsvColor#20928
Implement IFormattable on HslColor and HsvColor#20928NathanDrake2406 wants to merge 4 commits intoAvaloniaUI:masterfrom
Conversation
Add ToString(string?, IFormatProvider?) to both types: - HslColor: L/l for hsl() with/without alpha - HsvColor: V/v for hsv() with/without alpha Follows same convention as Color: uppercase includes alpha, lowercase excludes it.
C/c outputs the natural CSS format for any color type: - On HslColor: alias for L/l (hsl output) - On HsvColor: alias for V/v (hsv output) Enables type-agnostic formatting when the caller doesn't need to know which color model they're holding.
444de8c to
bb666b0
Compare
|
You can test this PR using the following package version. |
|
|
||
| return format switch | ||
| { | ||
| "V" or "C" => string.Format(CultureInfo.InvariantCulture, "hsv({0}, {1}%, {2}%, {3})", hDeg, sPct, vPct, A), |
There was a problem hiding this comment.
Note: The existing code will output hsva() if there is an alpha channel.
We should double check the CSS spec and see if that was a mistake originally and if that needs to change one way or the other.
There was a problem hiding this comment.
HSV was a natural extension of HSL which is part of the CSS standard: https://www.w3schools.com/css/css_colors_hsl.asp
HSV is how the ColorPicker code in upsteam UWP/WinUI worked and also aligned with the community toolkit's HsvColor so was added first. Then I added HSL to give us the full CSS color models support. Since HSL follows CSS, HSV follows the same formatting rules which does include hsv vs hsva.
src/Avalonia.Base/Media/HslColor.cs
Outdated
There was a problem hiding this comment.
Here is a copy/paste bug in the legacy code. This should be hsla
Since it is going to have to change and break backwards compatibility we can definitely decide if there should be an a or not based on what CSS does.
No one noticed this before anyway so it isn't used for anything I would think.
Pre-existing copy-paste bug: HslColor.ToString() was outputting
"hsva(" instead of "hsla(". Spotted by @robloo in review.
|
You can test this PR using the following package version. |
What does the pull request do?
Adds
IFormattablesupport toHslColorandHsvColor, following the same convention established in #20919 forColor: uppercase includes alpha, lowercase excludes it.Lhsl(h, s%, l%, a)hsl(240, 80%, 20%, 0.5)lhsl(h, s%, l%)hsl(240, 80%, 20%)Vhsv(h, s%, v%, a)hsv(240, 80%, 20%, 0.5)vhsv(h, s%, v%)hsv(240, 80%, 20%)Default
ToString()behavior is unchanged for both types.What is the current behavior?
HslColorandHsvColorhave a singleToString()that outputs a fixed full-precision format (hsla(h, s, l, a)/hsva(h, s, v, a)). There is no way to control alpha inclusion or get CSS-style percentage output.What is the updated/expected behavior with this PR?
How was the solution implemented (if it's not obvious)?
Same pattern as
Color(#20919):IFormattableinterface guarded by#if !BUILDTASK, format specifier selects output,IFormatProvideraccepted but ignored (culture-invariant). Hue is rounded to integer degrees, saturation/lightness/value to integer percentages — standard CSS representation.Checklist
Breaking changes
None.
IFormattableis additive and defaultToString()is unchanged.Fixed issues
Fixes #20927