Skip to content

XAML: Show color swatches for named colors (e.g. Red, CornflowerBlue) #209

Closed

Description

Summary

The XAML language server currently shows color swatches only for hex color literals (e.g. #FF0000). Named WinUI colors like Red, CornflowerBlue, DarkSlateGray should also display inline color swatches in the editor.

Current state

  • XamlColor.cs parses hex literals and returns ColorInformation to the LSP client
  • XamlTypeSystem.GetNamedColors() already discovers named color constants via Roslyn (e.g. Microsoft.UI.Colors.Red)
  • However, Roslyn cannot evaluate property getters at design time, so the actual RGBA values for named colors are not available from the type system alone

Proposed approach

Import a build-time lookup table of named color → RGBA mappings from the WinAppSDK source (or a generated resource). This table would be used by XamlColor to resolve named colors to their swatch values without requiring runtime evaluation.

Related

Activity

  1. chiaramooney commented on Sep 8, 2026

    @chiaramooney
    ContributorAuthor

    Worth doing, and no hard-coded hex table is needed. The proposed approach here (a build-time import of named color to RGBA from the WinAppSDK source) can be dropped.

    The premise in this issue is correct as far as it goes: GetNamedColors() returns only IPropertySymbol.Name values, and Roslyn genuinely cannot evaluate Microsoft.UI.Colors.Red, because those getters are implemented natively in the WinRT projection. But the conclusion that we must therefore import a value table does not follow. The names WinUI exposes are the W3C/X11 set, and .NET already ships that exact table in System.Drawing.Primitives.

    Verified empirically rather than assumed, on net10.0:

    Assembly: System.Drawing.Primitives
    Location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\10.0.11\System.Drawing.Primitives.dll
    names probed: 141
    not resolved: 0
    Red              expected FFFF0000  actual FFFF0000  OK
    CornflowerBlue   expected FF6495ED  actual FF6495ED  OK
    DarkSlateGray    expected FF2F4F4F  actual FF2F4F4F  OK
    Transparent      expected 00FFFFFF  actual 00FFFFFF  OK
    bogus name resolves: False
    

    All 141 names (the 140 X11 colors plus Transparent) resolve, ARGB matches W3C exactly, and Transparent correctly comes back as 00FFFFFF rather than 00000000. Three properties that matter for us:

    • It lives in the shared framework, not a NuGet package, so there is no new dependency and nothing extra to carry in the self-contained publish.
    • Color.FromName is a pure managed lookup with no native System.Drawing dependency, so it is safe on any host.
    • It is data we do not own or maintain, which is the whole point of not hard-coding.

    One design constraint worth writing down. Color.FromName also resolves .NET system colors such as Control, Desktop, and ActiveBorder, which Microsoft.UI.Colors does not have. So FromName must not be the gate on what counts as a valid color name. GetNamedColors() stays authoritative for which names exist, since it reads the real SDK metadata for the project's WinAppSDK version, and System.Drawing is used only to resolve a name we have already accepted into RGBA. That keeps the accepted set correct even if a future SDK adds or removes a name.

    Second question worth deciding before implementing: what ColorPresentation should offer when a user opens the picker on a named color. Right now Present only emits hex, so picking a new color over Red would rewrite it as #FFFF00FF and silently lose the symbolic name. That matches how CSS tooling behaves and is probably acceptable, but offering the nearest named color alongside the hex would be friendlier. Worth an explicit call rather than falling out of the implementation by accident.

    Estimated cost is small: a lookup gated on GetNamedColors(), a branch in XamlColor.Collect next to TryParseHex, and tests. No new dependency, no generated file, no table to keep in sync with the SDK.

  2. added a commit that references this issue on Sep 8, 2026
    a75151b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions