Skip to content

Commit dea6736

Browse files
authored
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).
1 parent 3a8cbf3 commit dea6736

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Terminal.Gui/Drawing/Color/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public Color (int red = 0, int green = 0, int blue = 0, int alpha = byte.MaxValu
113113

114114
/// <summary>Initializes a new instance of the <see cref="Color"/> color from a value in the <see cref="StandardColor"/> enum.</summary>
115115
/// <param name="colorName">The 16-color value.</param>
116-
public Color (in StandardColor colorName) : this ((int)colorName) { }
116+
public Color (in StandardColor colorName) : this (StandardColors.GetArgb (colorName)) { }
117117

118118
/// <summary>
119119
/// Initializes a new instance of the <see cref="Color"/> color from string. See
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Xunit;
2+
3+
namespace Terminal.Gui.DrawingTests;
4+
5+
public class ColorStandardColorTests
6+
{
7+
[Fact]
8+
public void ToString_Returns_Standard_Name_For_StandardColor_CadetBlue()
9+
{
10+
// Without the fix, this uses Color(in StandardColor) -> this((int)colorName),
11+
// which sets A=0x00 and prevents name resolution (expects A=0xFF).
12+
var c = new Terminal.Gui.Drawing.Color(Terminal.Gui.Drawing.StandardColor.CadetBlue);
13+
14+
// Expected: named color
15+
Assert.Equal("CadetBlue", c.ToString());
16+
}
17+
18+
[Fact]
19+
public void ToString_G_Prints_Opaque_ARGB_For_StandardColor_CadetBlue()
20+
{
21+
// Without the fix, A=0x00, so "G" prints "#005F9EA0" instead of "#FF5F9EA0".
22+
var c = new Terminal.Gui.Drawing.Color(Terminal.Gui.Drawing.StandardColor.CadetBlue);
23+
24+
// Expected: #AARRGGBB with A=FF (opaque)
25+
Assert.Equal("#FF5F9EA0", c.ToString("G", null));
26+
}
27+
}

0 commit comments

Comments
 (0)