{"record":{"id":"faa537be0e0709a6","repo":"spectreconsole/spectre.console","slug":"cannot-convert-color-to-console-color","errorCode":null,"errorMessage":"Cannot convert color to console color.","messagePattern":"Cannot convert color to console color\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console.Ansi/Color.cs","lineNumber":207,"sourceCode":"        return color.Number.Value switch\n        {\n            0 => ConsoleColor.Black,\n            1 => ConsoleColor.DarkRed,\n            2 => ConsoleColor.DarkGreen,\n            3 => ConsoleColor.DarkYellow,\n            4 => ConsoleColor.DarkBlue,\n            5 => ConsoleColor.DarkMagenta,\n            6 => ConsoleColor.DarkCyan,\n            7 => ConsoleColor.Gray,\n            8 => ConsoleColor.DarkGray,\n            9 => ConsoleColor.Red,\n            10 => ConsoleColor.Green,\n            11 => ConsoleColor.Yellow,\n            12 => ConsoleColor.Blue,\n            13 => ConsoleColor.Magenta,\n            14 => ConsoleColor.Cyan,\n            15 => ConsoleColor.White,\n            _ => throw new InvalidOperationException(\"Cannot convert color to console color.\"),\n        };\n    }\n\n    /// <summary>\n    /// Converts a color number into a <see cref=\"Color\"/>.\n    /// </summary>\n    /// <param name=\"number\">The color number.</param>\n    /// <returns>The color representing the specified color number.</returns>\n    public static Color FromInt32(int number)\n    {\n        return ColorTable.GetColor(number);\n    }\n\n    /// <summary>\n    /// Creates a color from a hexadecimal string representation.\n    /// </summary>\n    /// <param name=\"hex\">The hexadecimal string representation of the color.</param>\n    /// <returns>The color created from the hexadecimal string.</returns>","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console.Ansi/Color.cs#L189-L225","documentation":"A switch expression maps the 16 standard ANSI colors (indices 0-15) to System.ConsoleColor values; the default arm '_' throws InvalidOperationException for any color number outside 0-15 (i.e. the 256-color palette entries 16-255, which have no ConsoleColor equivalent).","triggerScenarios":"Calling this ConsoleColor conversion on a Color whose number is >= 16 (e.g. a true-color/RGB-derived color or a high palette index), when the runtime/console only supports the 16-color mapping.","commonSituations":"Constructing a Color via rgb()/hex that resolves to a palette index beyond 15 and then asking for its ConsoleColor; downgrading a rich-color Spectre pipeline to a legacy ConsoleColor API; running on a terminal that requested 256-color output.","solutions":["Restrict colors to the 0-15 range when a ConsoleColor is required","Guard the conversion: check the color number is <= 15 before calling, and pick a fallback ConsoleColor otherwise","Avoid mixing true-color Spectre output with System.ConsoleColor-based APIs"],"exampleFix":"// before\nvar cc = color.ToConsoleColor(); // throws if color.Number > 15\n\n// after\nvar cc = color.Number <= 15\n    ? color.ToConsoleColor()\n    : ConsoleColor.Gray; // safe fallback","handlingStrategy":"validation","validationCode":"// Guard the conversion against out-of-range palette indices:\nConsoleColor cc = color.Number is >= 0 and <= 15\n    ? color.ToConsoleColor()\n    : ConsoleColor.Gray; // or another safe fallback","typeGuard":"public static bool HasConsoleColorEquivalent(Color c) => c.Number is >= 0 and <= 15;","tryCatchPattern":"ConsoleColor cc;\ntry { cc = color.ToConsoleColor(); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Cannot convert color to console color.\")\n{ cc = ConsoleColor.Gray; }","preventionTips":["Keep colors in the 0-15 range when interoperating with System.ConsoleColor","Don't derive ConsoleColor from rgb/hex true-color values","Guard the conversion and provide a sensible fallback for rich colors"],"tags":["color","console","conversion","spectre-console"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}