Parcourir la source

Fixes #4284 (#4285)

Color alpha channel bug and add StandardColor tests

Updated the `Color` constructor for `StandardColor` to use `StandardColors.GetArgb` instead of casting to an integer, ensuring the alpha channel (`A`) is correctly set to `0xFF` (opaque). This resolves issues with name resolution and ARGB formatting.

Added a new test class, `ColorStandardColorTests`, to verify the behavior of `Color` when initialized with a `StandardColor`. Tests include:
- Verifying `ToString` returns the correct standard color name.
- Ensuring `ToString("G")` outputs the correct opaque ARGB value.

These changes address a bug where the alpha channel was incorrectly set to `0x00` (transparent).
Tig il y a 1 mois
Parent
commit
dea6736e82

+ 1 - 1
Terminal.Gui/Drawing/Color/Color.cs

@@ -113,7 +113,7 @@ public readonly partial record struct Color : ISpanParsable<Color>, IUtf8SpanPar
 
     /// <summary>Initializes a new instance of the <see cref="Color"/> color from a value in the <see cref="StandardColor"/> enum.</summary>
     /// <param name="colorName">The 16-color value.</param>
-    public Color (in StandardColor colorName) : this ((int)colorName) { }
+    public Color (in StandardColor colorName) : this (StandardColors.GetArgb (colorName)) { }
 
     /// <summary>
     ///     Initializes a new instance of the <see cref="Color"/> color from string. See

+ 27 - 0
Tests/UnitTestsParallelizable/Drawing/Color/ColorStandardColorTests.cs

@@ -0,0 +1,27 @@
+using Xunit;
+
+namespace Terminal.Gui.DrawingTests;
+
+public class ColorStandardColorTests
+{
+    [Fact]
+    public void ToString_Returns_Standard_Name_For_StandardColor_CadetBlue()
+    {
+        // Without the fix, this uses Color(in StandardColor) -> this((int)colorName),
+        // which sets A=0x00 and prevents name resolution (expects A=0xFF).
+        var c = new Terminal.Gui.Drawing.Color(Terminal.Gui.Drawing.StandardColor.CadetBlue);
+
+        // Expected: named color
+        Assert.Equal("CadetBlue", c.ToString());
+    }
+
+    [Fact]
+    public void ToString_G_Prints_Opaque_ARGB_For_StandardColor_CadetBlue()
+    {
+        // Without the fix, A=0x00, so "G" prints "#005F9EA0" instead of "#FF5F9EA0".
+        var c = new Terminal.Gui.Drawing.Color(Terminal.Gui.Drawing.StandardColor.CadetBlue);
+
+        // Expected: #AARRGGBB with A=FF (opaque)
+        Assert.Equal("#FF5F9EA0", c.ToString("G", null));
+    }
+}