Skip to content

Commit 6409b10

Browse files
committed
Changed HsvColor and HslColor to use float instead of double
1 parent 0d8c297 commit 6409b10

File tree

6 files changed

+59
-60
lines changed

6 files changed

+59
-60
lines changed

components/Helpers/src/ColorHelper/ColorExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static Color AlphaOver(this Color @base, Color overlay, double alpha)
6464
/// <param name="original">The original color.</param>
6565
/// <param name="hue">The new hue.</param>
6666
/// <returns>A <see cref="HsvColor"/> with a new hue and the same saturation and value.</returns>
67-
public static HsvColor WithHue(this Color original, double hue)
67+
public static HsvColor WithHue(this Color original, float hue)
6868
{
6969
var hsv = (HsvColor)original;
7070
hsv.Hue = hue;
@@ -77,7 +77,7 @@ public static HsvColor WithHue(this Color original, double hue)
7777
/// <param name="original">The original color.</param>
7878
/// <param name="saturation">The new saturation.</param>
7979
/// <returns>A <see cref="HsvColor"/> with a new saturation and the same hue and value.</returns>
80-
public static HsvColor WithSaturation(this Color original, double saturation)
80+
public static HsvColor WithSaturation(this Color original, float saturation)
8181
{
8282
var hsv = (HsvColor)original;
8383
hsv.Saturation = saturation;
@@ -90,7 +90,7 @@ public static HsvColor WithSaturation(this Color original, double saturation)
9090
/// <param name="original">The original color.</param>
9191
/// <param name="value">The new value.</param>
9292
/// <returns>A <see cref="HsvColor"/> with a new value and the same hue and saturation.</returns>
93-
public static HsvColor WithValue(this Color original, double value)
93+
public static HsvColor WithValue(this Color original, float value)
9494
{
9595
var hsv = (HsvColor)original;
9696
hsv.Value = value;
@@ -103,7 +103,7 @@ public static HsvColor WithValue(this Color original, double value)
103103
/// <param name="original">The original color.</param>
104104
/// <param name="lightness">The new lightness.</param>
105105
/// <returns>A <see cref="HsvColor"/> with a new lightness and the same hue and saturation.</returns>
106-
public static HslColor WithLightness(this Color original, double lightness)
106+
public static HslColor WithLightness(this Color original, float lightness)
107107
{
108108
var hsl = (HslColor)original;
109109
hsl.Lightness = lightness;
@@ -183,7 +183,7 @@ private static Color MixColors(Color color1, Color color2, double factor)
183183
/// <param name="l">The color's lightness.</param>
184184
/// <param name="a">The color's alpha value.</param>
185185
/// <returns>The color as a <see cref="HslColor"/>.</returns>
186-
public static HslColor FromAhsl(double a, double h, double s, double l) => HslColor.Create(h, s, l, a);
186+
public static HslColor FromAhsl(float a, float h, float s, float l) => HslColor.Create(h, s, l, a);
187187

188188
/// <summary>
189189
/// Gets a <see cref="HsvColor"/> from alpha, hue, saturation, and value channel info.
@@ -197,7 +197,7 @@ private static Color MixColors(Color color1, Color color2, double factor)
197197
/// <param name="v">The color's value.</param>
198198
/// <param name="a">The color's alpha value.</param>
199199
/// <returns>The color as a <see cref="HsvColor"/>.</returns>
200-
public static HsvColor FromAhsv(double a, double h, double s, double v) => HsvColor.Create(h, s, v, a);
200+
public static HsvColor FromAhsv(float a, float h, float s, float v) => HsvColor.Create(h, s, v, a);
201201

202202
/// <summary>
203203
/// Mixes two colors using a factor for deciding how much influence each color has.

components/Helpers/src/ColorHelper/ColorHelper.Obsolete.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static partial class ColorHelper
2727
/// <returns>The created <see cref="Color"/>.</returns>
2828
[Obsolete("This method is marked deprecated, and will be removed in a future version. Please replace with (Color)HslColor.Create().")]
2929
public static Color FromHsl(double hue, double saturation, double lightness, double alpha = 1.0)
30-
=> HslColor.Create(hue, saturation, lightness, alpha);
30+
=> HslColor.Create((float)hue, (float)saturation, (float)lightness, alpha);
3131

3232
/// <summary>
3333
/// Creates a <see cref="Color"/> from the specified hue, saturation, value, and alpha values.
@@ -39,7 +39,7 @@ public static Color FromHsl(double hue, double saturation, double lightness, dou
3939
/// <returns>The created <see cref="Color"/>.</returns>
4040
[Obsolete("This method is marked deprecated, and will be removed in a future version. Please replace with (Color)HsvColor.Create().")]
4141
public static Color FromHsv(double hue, double saturation, double value, double alpha = 1.0)
42-
=> HsvColor.Create(hue, saturation, value, alpha);
42+
=> HsvColor.Create((float)hue, (float)saturation, (float)value, (float)alpha);
4343

4444
/// <summary>
4545
/// Converts a <see cref="Color"/> to a hexadecimal string representation.

components/Helpers/src/ColorHelper/ColorHelper.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,21 +299,21 @@ public static Color ParseColorName(string colorName)
299299
throw new FormatException($"The name '{colorName}' not a valid color");
300300
}
301301

302-
internal static (double h1, double chroma) CalculateHueAndChroma(Color color, out double min, out double max, out double alpha)
302+
internal static (float h1, float chroma) CalculateHueAndChroma(Color color, out float min, out float max, out float alpha)
303303
{
304304
// This code is shared between both the conversion
305305
// to both HSL and HSV from RGB.
306306

307-
var r = (double)color.R / 255;
308-
var g = (double)color.G / 255;
309-
var b = (double)color.B / 255;
310-
alpha = (double)color.A / 255;
307+
var r = (float)color.R / 255;
308+
var g = (float)color.G / 255;
309+
var b = (float)color.B / 255;
310+
alpha = (float)color.A / 255;
311311

312312
max = Math.Max(Math.Max(r, g), b);
313313
min = Math.Min(Math.Min(r, g), b);
314314
var chroma = max - min;
315315

316-
double h1;
316+
float h1;
317317
if (chroma == 0)
318318
{
319319
// No max
@@ -338,20 +338,20 @@ internal static (double h1, double chroma) CalculateHueAndChroma(Color color, ou
338338
return (h1, chroma);
339339
}
340340

341-
internal static Color FromHueChroma(double h1, double chroma, double x, double m, double alpha)
341+
internal static Color FromHueChroma(float h1, float chroma, float x, float m, float alpha)
342342
{
343343
// This code is shared between both the conversion
344344
// from both HSL and HSV to RGB.
345345

346-
double r1, g1, b1;
346+
float r1, g1, b1;
347347
(r1, g1, b1) = h1 switch
348348
{
349-
< 1 => (chroma, x, 0d),
350-
< 2 => (x, chroma, 0d),
351-
< 3 => (0d, chroma, x),
352-
< 4 => (0d, x, chroma),
353-
< 5 => (x, 0d, chroma),
354-
_ => (chroma, 0d, x),
349+
< 1 => (chroma, x, 0f),
350+
< 2 => (x, chroma, 0f),
351+
< 3 => (0f, chroma, x),
352+
< 4 => (0f, x, chroma),
353+
< 5 => (x, 0f, chroma),
354+
_ => (chroma, 0f, x),
355355
};
356356

357357
byte r = (byte)(255 * (r1 + m));
@@ -365,7 +365,7 @@ internal static Color FromHueChroma(double h1, double chroma, double x, double m
365365
/// <summary>
366366
/// Parses a string to match the argument pattern "<paramref name="funcName"/>(args[0], args[1], ...)"
367367
/// </summary>
368-
private static bool MatchArgPattern(string value, string funcName, out double[] args)
368+
private static bool MatchArgPattern(string value, string funcName, out float[] args)
369369
{
370370
args = [];
371371

@@ -388,10 +388,10 @@ private static bool MatchArgPattern(string value, string funcName, out double[]
388388
var argStrings = value.Split(',');
389389

390390
// Parse the args into an array of doubles
391-
args = new double[argStrings.Length];
391+
args = new float[argStrings.Length];
392392
for (var i = 0; i < argStrings.Length; i++)
393393
{
394-
if (!double.TryParse(argStrings[i], null, out args[i]))
394+
if (!float.TryParse(argStrings[i], null, out args[i]))
395395
return false;
396396
}
397397

components/Helpers/src/ColorHelper/HslColor.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public struct HslColor
4747
private HslColor(Color color)
4848
{
4949
// Calculate hue, chroma, and supplementary values
50-
(double h1, double chroma) = ColorHelper.CalculateHueAndChroma(color, out var min, out var max, out var alpha);
50+
(float h1, float chroma) = ColorHelper.CalculateHueAndChroma(color, out var min, out var max, out var alpha);
5151

5252
// Calculate saturation and lightness
53-
double lightness = 0.5 * (max + min);
54-
double saturation = chroma == 0 ? 0 : chroma / (1 - Math.Abs((2 * lightness) - 1));
53+
float lightness = 0.5f * (max + min);
54+
float saturation = chroma == 0 ? 0 : chroma / (1 - Math.Abs((2 * lightness) - 1));
5555

5656
// Set hsl properties
5757
Hue = 60 * h1;
@@ -68,7 +68,7 @@ private HslColor(Color color)
6868
/// <param name="lightness">The color's lightness.</param>
6969
/// <param name="alpha">The alpha/opacity.</param>
7070
/// <returns>The new <see cref="HslColor"/>.</returns>
71-
public static HslColor Create(double hue, double saturation, double lightness, double alpha = 1)
71+
public static HslColor Create(float hue, float saturation, float lightness, double alpha = 1)
7272
{
7373
HslColor color = default;
7474
#pragma warning disable 0618
@@ -90,9 +90,9 @@ public static HslColor Create(double hue, double saturation, double lightness, d
9090
/// <remarks>
9191
/// This value is clamped between 0 and 360.
9292
/// </remarks>
93-
public double Hue
93+
public float Hue
9494
{
95-
readonly get => Math.Clamp(H, 0, 360);
95+
readonly get => (float)H;
9696
set => H = Math.Clamp(value, 0, 360);
9797
}
9898

@@ -102,9 +102,9 @@ public double Hue
102102
/// <remarks>
103103
/// This value is clamped between 0 and 1.
104104
/// </remarks>
105-
public double Saturation
105+
public float Saturation
106106
{
107-
readonly get => Math.Clamp(S, 0, 1);
107+
readonly get => (float)S;
108108
set => S = Math.Clamp(value, 0, 1);
109109
}
110110

@@ -114,9 +114,9 @@ public double Saturation
114114
/// <remarks>
115115
/// This value is clamped between 0 and 1.
116116
/// </remarks>
117-
public double Lightness
117+
public float Lightness
118118
{
119-
readonly get => Math.Clamp(L, 0, 1);
119+
readonly get => (float)L;
120120
set => L = Math.Clamp(value, 0, 1);
121121
}
122122

@@ -126,9 +126,9 @@ public double Lightness
126126
/// <remarks>
127127
/// This value is clamped between 0 and 1.
128128
/// </remarks>
129-
public double Alpha
129+
public float Alpha
130130
{
131-
readonly get => Math.Clamp(A, 0, 1);
131+
readonly get => (float)A;
132132
set => A = Math.Clamp(value, 0, 1);
133133
}
134134

@@ -140,10 +140,10 @@ public double Alpha
140140
/// <returns>The <see cref="HslColor"/> as a <see cref="Color"/>.</returns>
141141
public readonly Color ToColor()
142142
{
143-
double chroma = (1 - Math.Abs((2 * Lightness) - 1)) * Saturation;
144-
double h1 = Hue / 60;
145-
double x = chroma * (1 - Math.Abs((h1 % 2) - 1));
146-
double m = Lightness - (0.5 * chroma);
143+
float chroma = (1 - Math.Abs((2 * Lightness) - 1)) * Saturation;
144+
float h1 = Hue / 60;
145+
float x = chroma * (1 - Math.Abs((h1 % 2) - 1));
146+
float m = Lightness - (0.5f * chroma);
147147

148148
return ColorHelper.FromHueChroma(h1, chroma, x, m, Alpha);
149149
}

components/Helpers/src/ColorHelper/HsvColor.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public struct HsvColor
4747
private HsvColor(Color color)
4848
{
4949
// Calculate hue, chroma, and supplementary values
50-
(double h1, double chroma) = ColorHelper.CalculateHueAndChroma(color, out var _, out var max, out var alpha);
50+
(float h1, float chroma) = ColorHelper.CalculateHueAndChroma(color, out var _, out var max, out var alpha);
5151

5252
// Calculate saturation and value
53-
double saturation = chroma == 0 ? 0 : chroma / max;
54-
double value = max;
53+
float saturation = chroma == 0 ? 0 : chroma / max;
54+
float value = max;
5555

5656
// Set hsv properties
5757
Hue = 60 * h1;
@@ -68,7 +68,7 @@ private HsvColor(Color color)
6868
/// <param name="value">The color's value.</param>
6969
/// <param name="alpha">The alpha/opacity.</param>
7070
/// <returns>The new <see cref="HsvColor"/>.</returns>
71-
public static HsvColor Create(double hue, double saturation, double value, double alpha = 1)
71+
public static HsvColor Create(float hue, float saturation, float value, float alpha = 1)
7272
{
7373
HsvColor color = default;
7474
#pragma warning disable 0618
@@ -80,7 +80,6 @@ public static HsvColor Create(double hue, double saturation, double value, doubl
8080
return color;
8181
}
8282

83-
8483
// This class contains deprecated public backing fields to be removed in a future version.
8584
// Suppress the warnings from using them in their new wrapping properties.
8685
#pragma warning disable 0618
@@ -91,9 +90,9 @@ public static HsvColor Create(double hue, double saturation, double value, doubl
9190
/// <remarks>
9291
/// This value is clamped between 0 and 360.
9392
/// </remarks>
94-
public double Hue
93+
public float Hue
9594
{
96-
readonly get => Math.Clamp(H, 0, 360);
95+
readonly get => (float)H;
9796
set => H = Math.Clamp(value, 0, 360);
9897
}
9998

@@ -103,9 +102,9 @@ public double Hue
103102
/// <remarks>
104103
/// This value is clamped between 0 and 1.
105104
/// </remarks>
106-
public double Saturation
105+
public float Saturation
107106
{
108-
readonly get => Math.Clamp(S, 0, 1);
107+
readonly get => (float)S;
109108
set => S = Math.Clamp(value, 0, 1);
110109
}
111110

@@ -115,9 +114,9 @@ public double Saturation
115114
/// <remarks>
116115
/// This value is clamped between 0 and 1.
117116
/// </remarks>
118-
public double Value
117+
public float Value
119118
{
120-
readonly get => Math.Clamp(V, 0, 1);
119+
readonly get => (float)V;
121120
set => V = Math.Clamp(value, 0, 1);
122121
}
123122

@@ -127,9 +126,9 @@ public double Value
127126
/// <remarks>
128127
/// This value is clamped between 0 and 1.
129128
/// </remarks>
130-
public double Alpha
129+
public float Alpha
131130
{
132-
readonly get => Math.Clamp(A, 0, 1);
131+
readonly get => (float)A;
133132
set => A = Math.Clamp(value, 0, 1);
134133
}
135134

@@ -142,10 +141,10 @@ public double Alpha
142141
/// <returns>The <see cref="HsvColor"/> as a <see cref="Color"/>.</returns>
143142
public readonly Color ToColor()
144143
{
145-
double chroma = Value * Saturation;
146-
double h1 = Hue / 60;
147-
double x = chroma * (1 - Math.Abs((h1 % 2) - 1));
148-
double m = Value - chroma;
144+
float chroma = Value * Saturation;
145+
float h1 = Hue / 60;
146+
float x = chroma * (1 - Math.Abs((h1 % 2) - 1));
147+
float m = Value - chroma;
149148

150149
return ColorHelper.FromHueChroma(h1, chroma, x, m, Alpha);
151150
}

components/Helpers/tests/Test_ColorHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ public void Test_ColorHelper_ToHsv_MaxR()
196196
[TestMethod]
197197
public void Test_ColorHelper_CreateHsl()
198198
{
199-
Assert.AreEqual(HslColor.Create(0.0, 1.0, 0.5), Colors.Red);
199+
Assert.AreEqual(HslColor.Create(0.0f, 1.0f, 0.5f), Colors.Red);
200200
}
201201

202202
[TestCategory("Helpers")]
203203
[TestMethod]
204204
public void Test_ColorHelper_CreateHsv()
205205
{
206-
Assert.AreEqual(HsvColor.Create(0.0, 1.0, 1.0), Colors.Red);
206+
Assert.AreEqual(HsvColor.Create(0.0f, 1.0f, 1.0f), Colors.Red);
207207
}
208208

209209
[TestCategory("Helpers")]

0 commit comments

Comments
 (0)