📉ConsoleBase - NKS0005

Use SetConsoleColor(Color, true) from KernelColorTools

This analyzer provides the following strings:

ContextString

Error List

Caller uses Console.BackgroundColor instead of SetConsoleColor(Color, true)

Suggestion Box

Use SetConsoleColor(Color, true) instead of Console.BackgroundColor

Description

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

Extended Description

This code analyzer detects the usage of BackgroundColor 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 background 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.BackgroundColor = ConsoleColor.Green;

After the fix

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

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