Struct Color
A console color in 24-bit truecolor, an xterm 256-palette index, or one of the 16 standard SGR slots. Use Rgb(byte, byte, byte) for arbitrary colors, Indexed256(byte) for xterm palette entries, and the named static fields (LightCyan, Brown, …) for the classic DOS palette mapped onto the user's terminal theme.
public readonly record struct Color : IEquatable<Color>
- Implements
- Inherited Members
Fields
Black
public static readonly Color Black
Field Value
Brown
public static readonly Color Brown
Field Value
DarkBlue
public static readonly Color DarkBlue
Field Value
DarkCyan
public static readonly Color DarkCyan
Field Value
DarkGray
public static readonly Color DarkGray
Field Value
DarkGreen
public static readonly Color DarkGreen
Field Value
DarkMagenta
public static readonly Color DarkMagenta
Field Value
DarkRed
public static readonly Color DarkRed
Field Value
LightBlue
public static readonly Color LightBlue
Field Value
LightCyan
public static readonly Color LightCyan
Field Value
LightGray
public static readonly Color LightGray
Field Value
LightGreen
public static readonly Color LightGreen
Field Value
LightMagenta
public static readonly Color LightMagenta
Field Value
LightRed
public static readonly Color LightRed
Field Value
White
public static readonly Color White
Field Value
Yellow
public static readonly Color Yellow
Field Value
Properties
B
public byte B { get; }
Property Value
G
public byte G { get; }
Property Value
Index
Palette index. 0..15 for Standard16, 0..255 for Xterm256; ignored for truecolor.
public byte Index { get; }
Property Value
Mode
public ColorMode Mode { get; }
Property Value
R
public byte R { get; }
Property Value
Methods
For(ColorDepth)
Return a color that depth can render exactly.
Truecolor values get quantized to the closest 256-palette entry
or BIOS-16 anchor; Xterm256 values get folded into Standard16 if
the depth permits only 16 colors. Standard16 values pass through
unchanged — they always render correctly. Returns the
default(Color) sentinel for None;
callers should check capability before emitting.
public Color For(ColorDepth depth)
Parameters
depthColorDepth
Returns
FromHex(string)
Parse a CSS hex string: #RRGGBB or #RGB (case
insensitive, with or without leading #). Returns a truecolor
Color. Throws FormatException on bad input.
public static Color FromHex(string hex)
Parameters
hexstring
Returns
Indexed256(byte)
xterm 256-palette entry. Index 0..15 mirrors the user's terminal theme (same as Standard(byte)); 16..231 is the 6×6×6 RGB cube; 232..255 is the 24-step grayscale ramp.
public static Color Indexed256(byte index)
Parameters
indexbyte
Returns
QuantizeToStandard16(byte, byte, byte)
Public quantizer — find the closest BIOS-16 anchor for an arbitrary RGB triple. Useful when consumers want to pre-render theme colors for terminals that lack truecolor.
public static byte QuantizeToStandard16(byte r, byte g, byte b)
Parameters
Returns
QuantizeToXterm256(byte, byte, byte)
Public quantizer — find the closest xterm 256-palette entry for an arbitrary RGB triple. Returns an index in 16..255 (never the terminal-themed 0..15 slots).
public static byte QuantizeToXterm256(byte r, byte g, byte b)
Parameters
Returns
Rgb(byte, byte, byte)
24-bit truecolor.
public static Color Rgb(byte r, byte g, byte b)
Parameters
Returns
TryFromHex(string?, out Color)
Non-throwing companion to FromHex(string).
public static bool TryFromHex(string? hex, out Color color)
Parameters
Returns
TryFromName(string?, out Color)
Look up one of the named DOS palette entries by name (case
insensitive). Accepts canonical names like "LightCyan" as
well as the lower-cased variant.
public static bool TryFromName(string? name, out Color color)
Parameters
Returns
TryParse(string?, out Color)
Parse either a hex string (#RRGGBB / #RGB) or a DOS
palette name ("LightCyan"). Hex takes priority when input
starts with #.
public static bool TryParse(string? text, out Color color)