tui-cs/Terminal.Gui · info · InvalidOperationException

Unable to create string from Color with value {Argb}, using

Error message

Unable to create string from Color with value {Argb}, using format string {formatString}

What it means

Defensive throw on the discarded '_' arm of the pattern switch in Color.ToString(string?, IFormatProvider?). The switch exhaustively covers all (formatString, formatProvider) combinations: null/empty format with any provider, and any non-null format with any provider. This '_' arm is therefore effectively unreachable in normal C# evaluation; it exists as a compile-time-completeness safeguard. If ever hit, it indicates the runtime produced a (formatString, provider) pair the author believed impossible.

Source

Thrown at Terminal.Gui/Drawing/Color/Color.Formatting.cs:137

            // G format string and null formatProvider - Output as 32-bit hex according to invariant culture rules from Argb
            ( ['G'], null) => $"#{A:X2}{R:X2}{G:X2}{B:X2}",

            // d format string and null formatProvider - Output as 24-bit decimal rgb(r,g,b) according to invariant culture rules from R, G, and B
            ( ['d'], null) => $"rgb({R:D},{G:D},{B:D})",

            // D format string and null formatProvider - Output as 32-bit decimal rgba(r,g,b,a) according to invariant culture rules from R, G, B, and A. Non-standard: a is a decimal byte value.
            ( ['D'], null) => $"rgba({R:D},{G:D},{B:D},{A:D})",

            // All other cases (formatString is not null here) - Delegate to formatProvider, first, and otherwise to invariant culture, and try to format the provided string from the channels
            ({ }, _) => string.Format (
                                       formatProvider ?? CultureInfo.InvariantCulture,
                                       CompositeFormat.Parse (formatString),
                                       R,
                                       G,
                                       B,
                                       A
                                      ),
            _ => throw new InvalidOperationException (
                                                      $"Unable to create string from Color with value {Argb}, using format string {formatString}"
                                                     )
        }
               ?? throw new InvalidOperationException (
                                                       $"Unable to create string from Color with value {Argb}, using format string {formatString}"
                                                      );
    }

    /// <inheritdoc/>
    /// <remarks>
    ///     <para>
    ///         This method should be used only when absolutely necessary, because it <b>always</b> has more overhead than
    ///         <see cref="ToString(string?,System.IFormatProvider?)"/>, as this method results in an intermediate allocation
    ///         of one or more instances of <see langword="string"/> and a copy of that string to
    ///         <paramref name="destination"/> if formatting was successful. <br/> When possible, use
    ///         <see cref="ToString(string?,System.IFormatProvider?)"/>, which attempts to avoid intermediate allocations.
    ///     </para>
    ///     <para>

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Treat this as a bug report: capture the Argb value and formatString from the message and file an issue against Terminal.Gui.
  2. Rebuild from a clean checkout to rule out a stale/partially-compiled Color.Formatting.cs.
  3. Avoid passing exotic formatString/provider combinations; use the documented single-letter formats ('g','G','d','D').
Defensive patterns

Strategy: try-catch

Try / catch

try { string s = color.ToString (fmt, provider); }
catch (InvalidOperationException ex) when (ex.Message.Contains ("Unable to create string from Color"))
{ /* report as Terminal.Gui internal bug; fall back to color.ToString() */ }

Prevention

When it happens

Trigger: Effectively unreachable. The only theoretical way is a future refactor that adds a new branch returning before this arm, or a JIT/trimming bug corrupting pattern matching. The message reports the Color's Argb value and the formatString for diagnostics.

Common situations: Not expected in practice; if you see it, suspect a corrupted build, a patched Color.Formatting.cs, or a runtime ABI mismatch.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/fea4eb31cdccf3e7. Report an issue: GitHub.