Compare commits

...

2 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
b38c8584ac Fix color picker float format precision from 2 to 4 decimal places
Increase float color format precision to 4 decimal places so all
256 byte values (0-255) can be uniquely represented in the 0-1 float
range. With only 2 decimal places, only 101 unique values existed,
causing round-trip errors (e.g., byte 57 → 0.22 → 56.1 ≠ 57).

Changes:
- ColorFormatHelper.cs: Update 'f' format to "0.####" and 'F' to ".####"
- ColorRepresentationHelper.cs: Update ColorToFloat precision to 4
- Add test for non-trivial color that verifies precision fix

Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/586aab56-475b-476e-b46a-7b18bea5a988

Co-authored-by: niels9001 <9866362+niels9001@users.noreply.github.com>
2026-04-11 23:51:45 +00:00
copilot-swe-agent[bot]
f8bab81abf Initial plan 2026-04-11 23:47:13 +00:00
3 changed files with 14 additions and 4 deletions

View File

@@ -394,8 +394,8 @@ namespace ManagedCommon
{ 'H', "X1" }, // hex uppercase one digit
{ 'x', "x2" }, // hex lowercase two digits
{ 'X', "X2" }, // hex uppercase two digits
{ 'f', "0.##" }, // float with leading zero, 2 digits
{ 'F', ".##" }, // float without leading zero, 2 digits
{ 'f', "0.####" }, // float with leading zero, 4 digits
{ 'F', ".####" }, // float without leading zero, 4 digits
{ 'p', "%" }, // percent value
{ 'i', "i" }, // int value
{ 's', "s" }, // string value

View File

@@ -418,6 +418,16 @@ namespace ColorPicker.UnitTests.Helpers
Assert.AreEqual(expected, result);
}
[TestMethod]
public void GetStringRepresentation_VEC4_NonTrivialColor_HasSufficientPrecision()
{
// Color (237, 41, 57) should produce float values that round-trip back to the original byte values.
// With only 2 decimal places, 57/255 = 0.22 and 0.22*255 = 56.1, which is wrong.
// With 4 decimal places, 57/255 = 0.2235 and 0.2235*255 = 56.9925, which rounds to 57 correctly.
var result = ColorRepresentationHelper.GetStringRepresentation(Color.FromArgb(255, 237, 41, 57), "VEC4", ColorFormatHelper.GetDefaultFormat("VEC4"));
Assert.AreEqual("(0.9294f, 0.1608f, 0.2235f, 1f)", result);
}
[TestMethod]
public void GetStringRepresentation_EmptyFormat_ReturnsHex()
{

View File

@@ -107,8 +107,8 @@ namespace ColorPicker.Helpers
private static string ColorToFloat(Color color)
{
var (red, green, blue) = ColorHelper.ConvertToDouble(color);
const int precision = 2;
const string floatFormat = "0.##";
const int precision = 4;
const string floatFormat = "0.####";
return $"({Math.Round(red, precision).ToString(floatFormat, CultureInfo.InvariantCulture)}f"
+ $", {Math.Round(green, precision).ToString(floatFormat, CultureInfo.InvariantCulture)}f"