ConsoleBase - NKS0004

Use SetConsoleColor(Color) from KernelColorTools

This analyzer provides the following strings:

Context
String

Error List

Caller uses Console.ForegroundColor instead of SetConsoleColor(Color)

Suggestion Box

Use SetConsoleColor(Color) instead of Console.ForegroundColor

Description

SetConsoleColor(Color) not only brings better color support provided by the appropriate VT sequences, but it can also use true color. Console.ForegroundColor only handles 16 colors.

Extended Description

This code analyzer detects the usage of ForegroundColor from the standard Console class found in the System namespace. This property only supports up to 16 colors, and, in order to upgrade to 255 colors or true color, you need to have a console that supports it and a VT escape sequence to be able to set the foreground color of the console in any color you want.

As a result, SetConsoleColor simplifies things up by using the Color class provided by Terminaux to be able to set the color without being restricted to the domain of the 16 colors.

Analysis Comparison

To get a brief insight about how this analyzer works, compare the two code blocks shown to you below:

Before the fix

Somewhere in your mod code...
public static void MyFunction() =>
    Console.ForegroundColor = ConsoleColor.Green;

After the fix

Somewhere in your mod code...
public static void MyFunction() =>
    ColorTools.SetConsoleColor(ConsoleColors.Green);

Suppression

You can suppress this suggestion by including it in the appropriate place, whichever is convenient.

For more information about how to suppress any warning issued by the Nitrocid analyzer, visit the below page:

Recommendation

We recommend that every caller which use this property use the recommended abovementioned method.

Last updated