C
C#2y 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
Pobiega2y ago
$spectre would be my recommendation
MODiX
MODiX2y 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
GamerpostOP2y ago
bet
Cattywampus
Cattywampus2y 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
Pobiega2y 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
Cattywampus2y ago
yeah, and not to mention they're not thread safe kinda funky
Gamerpost
GamerpostOP2y ago
i dont want my whole line in one color
Pobiega
Pobiega2y 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
Pixel2y 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
Pixel2y 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
Pixel2y 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
Pobiega2y ago
with modern win11 versions even cmdprompt supports it (via windows terminal, which is default in newer versions of win11)
Pixel
Pixel2y ago
oh that's nice
Jimmacle
Jimmacle2y ago
old command prompt can do it too iirc, you just have to make a win32 call to enable it
Jimmacle
Jimmacle2y 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
Jimmacle2y ago
ENABLE_VIRTUAL_TERMINAL_INPUT
Gamerpost
GamerpostOP2y ago
anyone used Colorful.Console? its showing wrong colors showing blue when i want red
Pobiega
Pobiega2y 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
GamerpostOP2y ago
how does the spectre thing work? what do you write exactly
MODiX
MODiX2y ago
Pobiega#2671
AnsiConsole.Markup("[underline red]Hello[/] World!");
React with ❌ to remove this embed.
Gamerpost
GamerpostOP2y ago
i dont really understand im using writelines
Pobiega
Pobiega2y ago
you'll just swap your Console.WriteLine to AnsiConsole.Markup
Gamerpost
GamerpostOP2y ago
then how do you get the colors after that? AnsiConsole.Markup(logo, Color.Cyan);
Pobiega
Pobiega2y ago
AnsiConsole.Markup("[underline red]Hello[/] World!");? what part do you not understand
Gamerpost
GamerpostOP2y ago
its a string ^
Pobiega
Pobiega2y ago
Gamerpost
GamerpostOP2y ago
how do you print a string
Pobiega
Pobiega2y ago
What do you think the code I just showed you does? it prints... a string.
Gamerpost
GamerpostOP2y ago
im new to c i mean like variable
Pobiega
Pobiega2y ago
there are many ways
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y 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
GamerpostOP2y ago
theres no way to make it more simple? :(( thanks for your help th tho il look more into it
Pobiega
Pobiega2y ago
I mean, if you just want something like MyConsole.WriteLine(yourStringVariable, Color.Cyan); thats doable
Gamerpost
GamerpostOP2y ago
i like this , Color.Cyan); makes it more simple how do you do that
Pobiega
Pobiega2y 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
GamerpostOP2y ago
yess tytty
Pobiega
Pobiega2y 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
GamerpostOP2y ago
yeah
Accord
Accord2y 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