Strick.PlusCon includes several string
extension methods that can be used to
format information for console output.
These methods work by wrapping the string in escape sequences to enable the specific console formatting.
The Colorize
method wraps a string with foreground and/or background color
escape sequences.
string colorized = "foo".Colorize(Color.Red);
WL(colorized);
WL(colorized.Colorize(null, Color.White)); //add background
WL("Hello World!".Colorize(Color.Red));
WL("Hello World!".Colorize(Color.Red, Color.White));
You can also wrap your colorized string with pre- and post-values. The pre- and post-values can also, optionally, be rendered with their own colors.
string wrapped = "foo".Colorize(Color.Red, null, "**[", "]**");
WL(wrapped);
wrapped = "foo".Colorize(Color.Red, null, "**[", "]**", Color.Lime, Color.White);
WL(wrapped);
wrapped = "cruel".Colorize(Color.Red, null, "*", "*", Color.Lime);
WL($"Hello {wrapped} World!");
Another Colorize
overload takes a sequence of Color
structures, and
returns a string with escape sequences to vary the foreground color of
each character in the value with the colors from the sequence of colors.
If the colors sequence is null or has no elements, the value is returned unchanged.
If the colors sequence has only a single element, the entire value is colorized with that color.
If the length of the value is 1, its color will be set to the first color in the colors sequence.
If the length of the value is > the number of elements in the colors sequence, the colors in the sequence will be repeated.
string stick = "Peppermint-stick";
List<Color> colors = new List<Color>();
colors.Add(Color.Red);
colors.Add(Color.White);
WL(stick.Colorize(colors));
colors = ColorUtilities.GetGradientColors(Color.Red, Color.White, 5).ToList();
WL(stick.Colorize(colors));
colors = ColorUtilities.GetGradientColors(Color.Red, Color.White, stick.Length).ToList();
WL(stick.Colorize(colors));
colors.Clear();
colors.Add(Color.SaddleBrown);
colors.Add(Color.White);
colors.Add(Color.Blue);
WL("Peppermint-Pattie".Colorize(colors).Colorize(null, Color.LightGray));
colors.Clear();
colors.Add(Color.FromArgb(171, 96, 0)); //brown
colors.Add(Color.FromArgb(57, 168, 53)); //green
colors.Add(Color.FromArgb(2, 85, 166)); //blue
WL("Peppermint-Patty".Colorize(colors).Colorize(null, Color.LightGray));
The Underline
method wraps a string with underlining escape sequences.
string underlined = "foo".Underline();
WL(underlined);
WL("Hello World!".Underline());
The Reverse
method wraps a string with reverse text (foreground and background colors are swapped, or “reversed”) escape sequences.
string reversed = "foo".Reverse();
WL(reversed);
WL("Hello World!".Reverse());
WL(reversed, Color.LimeGreen, Color.White);
WL(reversed, Color.White, Color.LimeGreen);
The Gradient
method inserts escape sequences into a string to vary the foreground color of each character in the string.
string grad = "foo-bar".Gradient(Color.White, Color.BlueViolet);
WL(grad);
WL("Hello-World!".Gradient(Color.Red, Color.White));
WL("--=gradients=--".Gradient(Color.White, Color.BlueViolet, Color.White));
WL("***fade-out***".Gradient(Color.White, Color.Black));
WL("***fade-in!***".Gradient(Color.Black, Color.White));
WL("-- ** down on the beach ** --".Gradient(Color.SandyBrown, Color.FromArgb(3, 240, 165), Color.FromArgb(145, 193, 255)));
Use the ColorUtilities.GetGradientColors
method to get a sequence of colors for a gradient,
which can be used in ways limited only by your imagination.
var colors = ColorUtilities.GetGradientColors(Color.SkyBlue, Color.Orange, Console.WindowHeight).ToList();
string spaces = new(' ', Console.WindowWidth);
foreach (var color in colors)
{ W(spaces, Color.White, color); }
Console.SetCursorPosition(0, 0);
W("Sunrise", Color.White, colors[0]);
int top = Console.WindowHeight / 2;
int bottom = Console.WindowHeight - top;
var colors = ColorUtilities.GetGradientColors(Color.FromArgb(145, 193, 255), Color.FromArgb(3, 240, 165), top).ToList();
colors.AddRange(ColorUtilities.GetGradientColors(Color.FromArgb(3, 240, 165), Color.SandyBrown, bottom));
string spaces = new(' ', Console.WindowWidth);
foreach (var color in colors)
{ W(spaces, Color.White, color); }
Console.SetCursorPosition(0, Console.WindowHeight - 2);
W("On the beach", Color.White, colors[^2]);
The various formatting methods can be combined to create additional effects.
WL($"Hello World!".Underline(), Color.Red);
WL($"Hello {"cruel".Underline()} World!", Color.Red);
WL("***fade-out***".Gradient(Color.Black, Color.White).Colorize(null, Color.White));
WL("***fade-in!***".Gradient(Color.White, Color.Black).Colorize(null, Color.White));
WL("-- ** down on the beach ** --".Gradient(Color.SandyBrown, Color.FromArgb(3, 240, 165), Color.FromArgb(145, 193, 255)).Reverse());
WL("-- ** down on the beach ** --".Gradient(Color.SandyBrown, Color.FromArgb(3, 240, 165), Color.FromArgb(145, 193, 255)).Underline());
Note that nesting colors (either foreground or background) is NOT supported.
Console.ForegroundColor = ConsoleColor.Red;
WL($"Hello {"cruel".Colorize(Color.Lime).Underline()} World!");
Console.ResetColor();
WL($"Hello {"cruel".Colorize(Color.Lime).Underline()} World!", Color.Red);
WL($"Hello [cruel] World!".Colorize(Color.Red), Color.Lime);
//You have to use something more like this...
WL($"{"Hello".Colorize(Color.Red)} {"cruel".Colorize(Color.Lime).Underline()} {"World!".Colorize(Color.Red)}");
Brighten
Color
extension method
Darken
Color
extension method
AdjustBrightness
Color
extension method
These methods in the ColorUtilities
static class work with the System.Drawing.Color
structure
to adjust – brighten (toward white) or darken (toward black) a given color.
Color b = Color.FromArgb(255, 0, 0);
Color d = Color.FromArgb(255, 0, 0);
int adjustment = 40;
WL($"{ShowColor(b)} {ShowColor(d)}");
do
{
b = b.Brighten(adjustment);
d = d.Darken(adjustment);
WL($"{ShowColor(b)} {ShowColor(d)}");
} while ((d.R > 0 || d.G > 0 || d.B > 0) || (b.R < 255 || b.G < 255 || b.B < 255));
private static string ShowColor(Color color)
{
string text;
text = $"R:{color.R:D3} G:{color.G:D3} B:{color.B:D3}";
return text.Colorize(null, color);
}