Skip to content

Commit fce5451

Browse files
committed
Change color conversion to AwayFromZero rounding
1 parent c9975b7 commit fce5451

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

src/CommunityToolkit.Maui.Core/Extensions/ColorConversionExtensions.shared.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@ namespace CommunityToolkit.Maui.Core.Extensions;
88
/// </summary>
99
public static class ColorConversionExtensions
1010
{
11-
/// <summary>
12-
/// Converts the value to percentage.
13-
/// Uses <see cref="MidpointRounding.ToEven"/> and "0%" format string to emulate the behavior of "en-US" locale.
14-
/// If only "0%" is used to get rid of the space between the value and the percent sign, 0.625f would become 63% instead of 62%.
15-
/// </summary>
16-
/// <param name="percentage">percentage</param>
17-
/// <returns></returns>
18-
static string ToPercentage(this float percentage)
19-
{
20-
var toEvenRounded = Math.Round(percentage, 2, MidpointRounding.ToEven);
21-
return FormattableString.Invariant($"{toEvenRounded:0%}");
22-
}
23-
2411
/// <summary>
2512
/// Converts this <see cref="Color"/> to a <see cref="string"/> containing the red, green and blue components.
2613
/// </summary>
@@ -78,7 +65,7 @@ public static string ToRgbaString(this Color color)
7865
public static string ToCmykString(this Color color)
7966
{
8067
ArgumentNullException.ThrowIfNull(color);
81-
return FormattableString.Invariant($"CMYK({color.GetPercentCyan().ToPercentage()},{color.GetPercentMagenta().ToPercentage()},{color.GetPercentYellow().ToPercentage()},{color.GetPercentBlackKey().ToPercentage()})");
68+
return FormattableString.Invariant($"CMYK({color.GetPercentCyan():0%},{color.GetPercentMagenta():0%},{color.GetPercentYellow():0%},{color.GetPercentBlackKey():0%})");
8269
}
8370

8471
/// <summary>
@@ -108,7 +95,7 @@ public static string ToCmykString(this Color color)
10895
public static string ToCmykaString(this Color color)
10996
{
11097
ArgumentNullException.ThrowIfNull(color);
111-
return FormattableString.Invariant($"CMYKA({color.GetPercentCyan().ToPercentage()},{color.GetPercentMagenta().ToPercentage()},{color.GetPercentYellow().ToPercentage()},{color.GetPercentBlackKey().ToPercentage()},{color.Alpha})");
98+
return FormattableString.Invariant($"CMYKA({color.GetPercentCyan():0%},{color.GetPercentMagenta():0%},{color.GetPercentYellow():0%},{color.GetPercentBlackKey():0%},{color.Alpha})");
11299
}
113100

114101
/// <summary>
@@ -123,7 +110,7 @@ public static string ToCmykaString(this Color color)
123110
public static string ToHslString(this Color color)
124111
{
125112
ArgumentNullException.ThrowIfNull(color);
126-
return FormattableString.Invariant($"HSL({color.GetDegreeHue():0},{color.GetSaturation().ToPercentage()},{color.GetLuminosity().ToPercentage()})");
113+
return FormattableString.Invariant($"HSL({color.GetDegreeHue():0},{color.GetSaturation():0%},{color.GetLuminosity():0%})");
127114
}
128115

129116
/// <summary>
@@ -152,7 +139,7 @@ public static string ToHslString(this Color color)
152139
public static string ToHslaString(this Color color)
153140
{
154141
ArgumentNullException.ThrowIfNull(color);
155-
return FormattableString.Invariant($"HSLA({color.GetDegreeHue():0},{color.GetSaturation().ToPercentage()},{color.GetLuminosity().ToPercentage()},{color.Alpha})");
142+
return FormattableString.Invariant($"HSLA({color.GetDegreeHue():0},{color.GetSaturation():0%},{color.GetLuminosity():0%},{color.Alpha})");
156143
}
157144

158145
/// <summary>

src/CommunityToolkit.Maui.UnitTests/Converters/ColorToHslStringConverterTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,22 @@ public class ColorToHslStringConverterTests : BaseOneWayConverterTest<ColorToHsl
108108
0.25f, 0.25f, 0.25f, 0, "HSL(0,0%,25%)"
109109
},
110110
{
111-
0.25f, 0.25f, 1, 1, "HSL(240,100%,62%)"
111+
0.25f, 0.25f, 1, 1, "HSL(240,100%,63%)"
112112
},
113113
{
114-
0.25f, 0.25f, 1, 0, "HSL(240,100%,62%)"
114+
0.25f, 0.25f, 1, 0, "HSL(240,100%,63%)"
115115
},
116116
{
117-
0.25f, 1, 0.25f, 1, "HSL(120,100%,62%)"
117+
0.25f, 1, 0.25f, 1, "HSL(120,100%,63%)"
118118
},
119119
{
120-
0.25f, 1, 0.25f, 0, "HSL(120,100%,62%)"
120+
0.25f, 1, 0.25f, 0, "HSL(120,100%,63%)"
121121
},
122122
{
123-
0.75f, 1, 0.25f, 1, "HSL(80,100%,62%)"
123+
0.75f, 1, 0.25f, 1, "HSL(80,100%,63%)"
124124
},
125125
{
126-
0.75f, 1, 0.25f, 0, "HSL(80,100%,62%)"
126+
0.75f, 1, 0.25f, 0, "HSL(80,100%,63%)"
127127
},
128128
{
129129
0.75f, 0, 1, 1, "HSL(285,100%,50%)"

src/CommunityToolkit.Maui.UnitTests/Converters/ColorToHslaStringConverterTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ public class ColorToHslaStringConverterTests : BaseOneWayConverterTest<ColorToHs
107107
0.25f, 0.25f, 0.25f, 0, "HSLA(0,0%,25%,0)"
108108
},
109109
{
110-
0.25f, 0.25f, 1, 1, "HSLA(240,100%,62%,1)"
110+
0.25f, 0.25f, 1, 1, "HSLA(240,100%,63%,1)"
111111
},
112112
{
113-
0.25f, 0.25f, 1, 0, "HSLA(240,100%,62%,0)"
113+
0.25f, 0.25f, 1, 0, "HSLA(240,100%,63%,0)"
114114
},
115115
{
116-
0.25f, 1, 0.25f, 1, "HSLA(120,100%,62%,1)"
116+
0.25f, 1, 0.25f, 1, "HSLA(120,100%,63%,1)"
117117
},
118118
{
119-
0.25f, 1, 0.25f, 0, "HSLA(120,100%,62%,0)"
119+
0.25f, 1, 0.25f, 0, "HSLA(120,100%,63%,0)"
120120
},
121121
{
122-
0.75f, 1, 0.25f, 1, "HSLA(80,100%,62%,1)"
122+
0.75f, 1, 0.25f, 1, "HSLA(80,100%,63%,1)"
123123
},
124124
{
125-
0.75f, 1, 0.25f, 0, "HSLA(80,100%,62%,0)"
125+
0.75f, 1, 0.25f, 0, "HSLA(80,100%,63%,0)"
126126
},
127127
{
128128
0.75f, 0, 1, 1, "HSLA(285,100%,50%,1)"

0 commit comments

Comments
 (0)