C
C#16mo ago
Gamerpost

❔ 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
Pobiega
Pobiega16mo ago
$spectre would be my recommendation
MODiX
MODiX16mo ago
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.
Gamerpost
Gamerpost16mo ago
bet
Cattywampus
Cattywampus16mo ago
if you're insisting on doing it your own
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Green;
Pobiega
Pobiega16mo ago
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
Cattywampus
Cattywampus16mo ago
yeah, and not to mention they're not thread safe kinda funky
Gamerpost
Gamerpost16mo ago
i dont want my whole line in one color
Pobiega
Pobiega16mo ago
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!");
Pixel
Pixel16mo ago
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
Pixel
Pixel16mo ago
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 you can use this guide for more information on ANSI color codes
Gist
ANSI Escape Codes
ANSI Escape Codes. GitHub Gist: instantly share code, notes, and snippets.
Pixel
Pixel16mo ago
.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
Pobiega
Pobiega16mo ago
with modern win11 versions even cmdprompt supports it (via windows terminal, which is default in newer versions of win11)
Pixel
Pixel16mo ago
oh that's nice
Jimmacle
Jimmacle16mo ago
old command prompt can do it too iirc, you just have to make a win32 call to enable it
Jimmacle
Jimmacle16mo ago
SetConsoleMode function - Windows Console
Sets the input mode of a console's input buffer or the output mode of a console screen buffer.
Jimmacle
Jimmacle16mo ago
ENABLE_VIRTUAL_TERMINAL_INPUT
Gamerpost
Gamerpost16mo ago
anyone used Colorful.Console? its showing wrong colors showing blue when i want red
Pobiega
Pobiega16mo ago
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
Gamerpost
Gamerpost16mo ago
how does the spectre thing work? what do you write exactly
MODiX
MODiX16mo ago
Pobiega#2671
AnsiConsole.Markup("[underline red]Hello[/] World!");
React with ❌ to remove this embed.
Gamerpost
Gamerpost16mo ago
i dont really understand im using writelines
Pobiega
Pobiega16mo ago
you'll just swap your Console.WriteLine to AnsiConsole.Markup
Gamerpost
Gamerpost16mo ago
then how do you get the colors after that? AnsiConsole.Markup(logo, Color.Cyan);
Pobiega
Pobiega16mo ago
AnsiConsole.Markup("[underline red]Hello[/] World!");? what part do you not understand
Gamerpost
Gamerpost16mo ago
its a string ^
Pobiega
Pobiega16mo ago
Gamerpost
Gamerpost16mo ago
how do you print a string
Pobiega
Pobiega16mo ago
What do you think the code I just showed you does? it prints... a string.
Gamerpost
Gamerpost16mo ago
im new to c i mean like variable
Pobiega
Pobiega16mo ago
there are many ways
Pobiega
Pobiega16mo ago
Pobiega
Pobiega16mo ago
or this:
AnsiConsole.Foreground = Color.Cyan1;
AnsiConsole.WriteLine("This is cyan");
AnsiConsole.ResetColors();
AnsiConsole.Foreground = Color.Cyan1;
AnsiConsole.WriteLine("This is cyan");
AnsiConsole.ResetColors();
Gamerpost
Gamerpost16mo ago
theres no way to make it more simple? :(( thanks for your help th tho il look more into it
Pobiega
Pobiega16mo ago
I mean, if you just want something like MyConsole.WriteLine(yourStringVariable, Color.Cyan); thats doable
Gamerpost
Gamerpost16mo ago
i like this , Color.Cyan); makes it more simple how do you do that
Pobiega
Pobiega16mo ago
public static class Program
{
public static void Main()
{
MyConsole.WriteLine("Hello", Color.Cyan1);
}
}

public static class MyConsole
{
public static void WriteLine(string str, Color color)
{
AnsiConsole.ResetColors();
AnsiConsole.Foreground = color;
AnsiConsole.WriteLine(str);
AnsiConsole.ResetColors();
}
}
public static class Program
{
public static void Main()
{
MyConsole.WriteLine("Hello", Color.Cyan1);
}
}

public static class MyConsole
{
public static void WriteLine(string str, Color color)
{
AnsiConsole.ResetColors();
AnsiConsole.Foreground = color;
AnsiConsole.WriteLine(str);
AnsiConsole.ResetColors();
}
}
tada?
Gamerpost
Gamerpost16mo ago
yess tytty
Pobiega
Pobiega16mo ago
you dont even need Spectre if you just want that thou
public static class MyConsole
{
public static void WriteLine(string str, Color color)
{
Console.ResetColor();
Console.ForegroundColor = color;
Console.WriteLine(str);
Console.ResetColor();
}
}
public static class MyConsole
{
public static void WriteLine(string str, Color color)
{
Console.ResetColor();
Console.ForegroundColor = color;
Console.WriteLine(str);
Console.ResetColor();
}
}
works just as well you will get more color options with spectre thou, since it handles full 24 bit colors, if not more
Gamerpost
Gamerpost16mo ago
yeah
Accord
Accord16mo ago
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.
Want results from more Discord servers?
Add your server
More Posts