❔ C# Console Application - Need Help With Colors!
Anyone able to recommend me what I can use for colors because the nutget package I installed shows the wrong colors..
40 Replies
$spectre would be my recommendation
Spectre.Console is a .NET library that allows for easy creation of console UIs, text formatting in the console, and command-line argument parsing.
https://spectreconsole.net/
Spectre.Console - Welcome!
Spectre.Console is a .NET library that makes it easier to create beautiful console applications.
bet
if you're insisting on doing it your own
By far the simplest solution, and works quite well. You are limited to 16 colors thou, iirc.
The benefit of Spectre is full 24(?) bit color spectrum
yeah, and not to mention they're not thread safe
kinda funky
i dont want my whole line in one color
yeah thats doable, but annoying, since you need to split your
WriteLine
into several Write
spectre handles that better
AnsiConsole.Markup("[underline red]Hello[/] World!");
you can use ANSI color code if you don't want to use a library, that's what i did in my Oaklog project
\x1b[48;2;{0};{1};{2}m will write a color to the console, 0,1,2 being R,G,B values, to reset the color use the \x1b[0m escape code to reset colors
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 you can use this guide for more information on ANSI color codes
.NET C# uses the /x1b escape code
do note for ANSI colors to work you need an ANSI-capable output, the built-in command prompt will no do it on windows, powershell is able however and so is most terminal emulators on linux
with modern win11 versions even cmdprompt supports it (via windows terminal, which is default in newer versions of win11)
oh that's nice
old command prompt can do it too iirc, you just have to make a win32 call to enable it
SetConsoleMode function - Windows Console
Sets the input mode of a console's input buffer or the output mode of a console screen buffer.
ENABLE_VIRTUAL_TERMINAL_INPUT
anyone used Colorful.Console?
its showing wrong colors
showing blue when i want red
Yeah idk, I've never gotten that to work properly.
throw it in the garbage bin where it belongs and use spectre, or roll your own with ANSI color codes
how does the spectre thing work?
what do you write exactly
Pobiega#2671
AnsiConsole.Markup("[underline red]Hello[/] World!");
Quoted by
<@!105026391237480448> from #C# Console Application - Need Help With Colors! (click here)
React with ❌ to remove this embed.
i dont really understand im using writelines
you'll just swap your
Console.WriteLine
to AnsiConsole.Markup
then how do you get the colors after that?
AnsiConsole.Markup(logo, Color.Cyan);
AnsiConsole.Markup("[underline red]Hello[/] World!");
?
what part do you not understandits a string
^
how do you print a string
What do you think the code I just showed you does?
it prints... a string.
im new to c
i mean
like
variable
there are many ways
or this:
theres no way to make it more simple? :((
thanks for your help th
tho
il look more into it
I mean, if you just want something like
MyConsole.WriteLine(yourStringVariable, Color.Cyan);
thats doablei like this , Color.Cyan);
makes it more simple
how do you do that
tada?
yess
tytty
you dont even need Spectre if you just want that thou
works just as well
you will get more color options with spectre thou, since it handles full 24 bit colors, if not more
yeah
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.