spectre.console
spectre.console copied to clipboard
Figlet background color not respected?
Information
- OS: [eg Windows/Linux/MacOS]
- Version: [e.g. 0.33.0]
- Terminal: [e.g Windows Terminal]
Describe the bug When using the figlet it doesn't respect the background color for the console. i.e if the background color is blue, the figlet will print on black
To Reproduce Use the Examples/Console/Figlet, add the following to Program.cs
Console.OutputEncoding = Encoding.UTF8;
Console.Clear();
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Expected behavior Background color should be blue, not black
Screenshots

Additional context Add any other context about the problem here.
Please upvote :+1: this issue if you are interested in it.
This isn't unique to figlets, any set style seems to trigger this. It looks like SGR(0) doesn't reset the console colors to what was last set by BackgroundColor/ForegroundColor but resets to the default colors of the console (grey/black) instead.
There probably isn't a sensible way to make this work with Console.BackgroundColor/ForegroundColor but there are a few places in Spectre.Console that could benefit from knowing what the contextual background style is, and if that was possible then you could use it to solve this as well.
I'm playing with this right now in C#, and found this:
Console.OutputEncoding = Encoding.UTF8;
Console.Clear();
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("\x1b[38;5;15m A \x1b[0m");
var a = Console.BackgroundColor;
Console.WriteLine("\x1b[38;5;15m B \x1b[0m");
You can observe same behaviour
- before
Console.WriteLine("\x1b[38;5;15m A \x1b[0m");value ofConsole.BackgroundColorisDarkRed - after
Console.WriteLine("\x1b[38;5;15m A \x1b[0m");value isBlack(default terminal background)
My guess is its because \x1b[0m (or CSI0m in ansi code) will reset all changes in foreground and background.
In Spectre.console sources, you can find it in AnsiBuilder.Build -> $"{SGR(result)}{text}{SGR(0)}"
So maybe the question is how to work around this? Maybe spectre can backup value of Console.BackgroundColor before rendering anything into console and each time CSI0m is generated append "restore of background color" with CSI48... command?
I will try this on cloned sources and see what comes.
I made these changes:
var restoreBgColorCode = AnsiColorBuilder.GetAnsiCodes(profile.Capabilities.ColorSystem,
Color.FromConsoleColor(System.Console.BackgroundColor),false).ToArray();
var ansi = result.Length > 0 ? $"{SGR(result)}{text}{SGR(0)}{SGR(restoreBgColorCode)}" : text;
in AnsiBuilder.Build method.
And result is this:
But im worried in such simple fix? that this could break something other?
As im about to say, my fix does not work well, in case when you dont set Background color. eg:
My shell is powershell with "dark-dark blue" and with my fix, c# Console.BackgroundColor return Black which is not what we want :-(