Skip to content

Commit 7ecf5ce

Browse files
committed
Extension method
1 parent 0eeeabf commit 7ecf5ce

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

TextureMerge/ColorPicker.xaml.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ private void RGBChanged(object sender, TextChangedEventArgs e)
3131
canChange = false;
3232
RGBToHSV(R, G, B, out double H, out double S, out double V);
3333
RGBToHex(R, G, B, out string hex);
34-
HValue.Text = DoubleToString(H);
35-
SValue.Text = DoubleToString(S);
36-
VValue.Text = DoubleToString(V);
34+
HValue.Text = H.ToStringRounded(3);
35+
SValue.Text = S.ToStringRounded(3);
36+
VValue.Text = V.ToStringRounded(3);
3737
HexValue.Text = hex;
3838
canChange = true;
3939
}
@@ -65,9 +65,9 @@ private void HEXChanged(object sender, TextChangedEventArgs e)
6565
RValue.Text = R.ToString();
6666
GValue.Text = G.ToString();
6767
BValue.Text = B.ToString();
68-
HValue.Text = DoubleToString(H);
69-
SValue.Text = DoubleToString(S);
70-
VValue.Text = DoubleToString(V);
68+
HValue.Text = H.ToStringRounded(3);
69+
SValue.Text = S.ToStringRounded(3);
70+
VValue.Text = V.ToStringRounded(3);
7171
canChange = true;
7272
}
7373
}
@@ -243,8 +243,5 @@ private static void HSVToRGB(double H, double S, double V, out byte R, out byte
243243
G = (byte)(g * 255.0);
244244
B = (byte)(b * 255.0);
245245
}
246-
247-
private string DoubleToString(double value) =>
248-
((long)(value * 1000) / 1000.0).ToString();
249246
}
250247
}

TextureMerge/Extensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public static class Extensions
1212
{
1313
public static string Expand(this string path) => Environment.ExpandEnvironmentVariables(path);
1414

15+
public static string ToStringRounded(this double value, int decimalPlaces)
16+
{
17+
double p = Math.Pow(10, decimalPlaces);
18+
return ((long)(value * p) / p).ToString();
19+
}
20+
1521
public static ImageSource ToImageSource(this MagickImage image)
1622
{
1723
var stream = new MemoryStream();

0 commit comments

Comments
 (0)