Skip to content

TextStyle.Foreground/Background round trip throws or drops alpha for colors with alpha below 0x10 #142

Description

@matt-edmondson

What's wrong

The Foreground/Background getters in TUI/Models/TextStyle.cs return Color.Name. The XML docs on TryParseHexColor say the bare hex forms exist "so a value read from one can be assigned back", and TextStyleColorTests tests that round trip.

For an unnamed color, System.Drawing.Color.Name is Convert.ToString(argb, 16), which drops leading zeros:

  • Alpha 0x01-0x0F gives a 7-digit name, e.g. a123456. TryParseHexColor accepts only 6 or 8 digits, so assigning it back throws ArgumentException.
  • Alpha 0x00 with non-zero red gives a 6-digit name, e.g. 123456, which parses as RRGGBB and is forced to alpha 0xFF. The copy silently differs and TextStyle equality fails.
  • Shorter values, e.g. Color.FromArgb(0) → "0", also throw.

Failure scenario

var src = new TextStyle { ForegroundColor = Color.FromArgb(0x0A, 0x12, 0x34, 0x56) };
var copy = new TextStyle { Foreground = src.Foreground }; // throws

This is the documented pattern (style with { Foreground = other.Foreground }). It throws ArgumentException: 'a123456' is not a recognized color name or hex value. (Parameter 'Foreground'). With alpha 0x00, the copy's ARGB is FF123456 while the original is 00123456.

How verified

A scratch console app referencing TUI/TUI.csproj:

a=FF name=ff123456 orig=FF123456 copy=FF123456
a=80 name=80123456 orig=80123456 copy=80123456
a=10 name=10123456 orig=10123456 copy=10123456
a=0A name=a123456 THROWS ArgumentException: 'a123456' is not a recognized color name or hex value. (Parameter 'Foreground')
a=00 name=123456 orig=00123456 copy=FF123456

Suggested fix / acceptance criteria

  • For unnamed colors, return a fixed-width string from the getters, e.g. $"#{c.ToArgb():X8}", instead of Color.Name. Known colors keep their names.
  • Extend the existing round-trip test with rows for alpha 0x00, 0x01, 0x0F and Color.FromArgb(0); every row gives back the same ARGB with no exception.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workingreadyFully specified; implement as written

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions