HOWTO: Terminal Color Schemes
Color schemes is one of the features of our terminal emulator control (part of Rebex SSH Shell and Rebex Telnet) that was added few months ago after one of our users requested it. What is it for? If your application communicates with a monochrome terminal, you can define a custom color scheme to assign some colors to different visual styles to make them more readable. Doing this for a colors-capable terminal application usually doesn’t make much sense (although it is possible as well), but for a monochrome application, it can be a nice visual enhancement.
Check out the results below – a monochrome “man” command output is on the left and the output of the same command using a custom color scheme is on the right:
Defining a color scheme is very simple:
C #:
console.Options.ColorScheme = ColorScheme.Custom;
console.Options.SetColorIndex(SchemeColorName.Background, 0);
console.Options.SetColorIndex(SchemeColorName.Foreground, 7); //= normal text
console.Options.SetColorIndex(SchemeColorName.Bold, 11); //= highlighted text
console.Options.SetColorIndex(SchemeColorName.SingleUnderline, 9);
console.Options.SetColorIndex(SchemeColorName.DoubleUnderline, 9);
VB.NET:
console.Options.ColorScheme = ColorScheme.Custom
console.Options.SetColorIndex(SchemeColorName.Background, 0)
console.Options.SetColorIndex(SchemeColorName.Foreground, 7) ' = normal text
console.Options.SetColorIndex(SchemeColorName.Bold, 11) ' = highlighted
console.Options.SetColorIndex(SchemeColorName.SingleUnderline, 9)
console.Options.SetColorIndex(SchemeColorName.DoubleUnderline, 9)
(where console is an instance of TerminalControl)
You might be wondering about the numbers - these are indexes to the current color palette which contains the following colors:
0 | Black | 8 | Dark gray |
1 | Red | 9 | Light red |
2 | Green | 10 | Light green |
3 | Yellow | 11 | Light yellow |
4 | Blue | 12 | Light blue |
5 | Magenta | 13 | Light magenta |
6 | Cyan | 14 | Light cyan |
7 | Light gray | 15 | White |
Fortunately, you don't have to rembeber these numbers - just use the constants defined by TerminalColor static class.
If you prefer monochrome color scheme instead, you can simply turn color-capable terminals into monochrome by setting TerminalOption’s ColorScheme property to ColorScheme.Monochrome.
To ensure color changes are applied immediately to the terminal control, you have to call the console.Refresh() method.