Class TerminalColor
Supports three color modes:
- Basic 8/16 colors using
Colorenum - 256 colors using palette indices (0-255)
- True color (24-bit RGB) using RGB values
Also provides theme-aware factory methods that automatically select appropriate colors based on detected terminal capabilities.
- Author:
- Ståle W. Pedersen
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a default TerminalColor with no formatting.TerminalColor(int text, int background) Create a TerminalColor using 256-color palette indices.TerminalColor(int text, Color background) Create a TerminalColor with 256-color foreground and basic background.TerminalColor(int text, Color background, Color.Intensity intensity) Create a TerminalColor with 256-color foreground, basic background, and intensity.TerminalColor(Color text, int background) Create a TerminalColor with basic foreground and 256-color background.TerminalColor(Color text, int background, Color.Intensity intensity) Create a TerminalColor with basic foreground, 256-color background, and intensity.TerminalColor(Color text, Color background) Create a TerminalColor with the specified foreground and background colors.TerminalColor(Color textColor, Color background, Color.Intensity intensity) Create a TerminalColor with the specified colors and intensity. -
Method Summary
Modifier and TypeMethodDescriptionbooleanforCapability(TerminalColorCapability capability) Create the best representation of this color for the given terminal capability.static TerminalColorforError(TerminalColorCapability capability) Create a TerminalColor appropriate for error messages based on terminal theme.static TerminalColorforHighlight(TerminalColorCapability capability) Create a TerminalColor appropriate for highlighted/emphasized text.static TerminalColorforInfo(TerminalColorCapability capability) Create a TerminalColor appropriate for info messages based on terminal theme.static TerminalColorforMessage(TerminalColorCapability capability) Create a TerminalColor appropriate for highlighted messages in log output.static TerminalColorforMuted(TerminalColorCapability capability) Create a TerminalColor appropriate for muted/secondary text.static TerminalColorforSuccess(TerminalColorCapability capability) Create a TerminalColor appropriate for success messages based on terminal theme.static TerminalColorforTimestamp(TerminalColorCapability capability) Create a TerminalColor appropriate for timestamps in log output.static TerminalColorforWarning(TerminalColorCapability capability) Create a TerminalColor appropriate for warning messages based on terminal theme.static TerminalColorCreate a TerminalColor with true color foreground from hex string.static TerminalColorCreate a TerminalColor with true color foreground and background from hex strings.static TerminalColorfromHSL(float h, float s, float l) Create a TerminalColor with true color foreground from HSL values.static TerminalColorfromHSL(float textH, float textS, float textL, float bgH, float bgS, float bgL) Create a TerminalColor with true color foreground and background from HSL values.static TerminalColorfromRGB(int r, int g, int b) Create a TerminalColor with true color (24-bit RGB) foreground.static TerminalColorfromRGB(int textR, int textG, int textB, int bgR, int bgG, int bgB) Create a TerminalColor with true color (24-bit RGB) foreground and background.Get the full ANSI escape sequence for this color.float[]Get the background color as HSL values, if using true color.int[]Get the background RGB values if using true color.intGet the length of the ANSI escape sequence.float[]Get the foreground color as HSL values, if using true color.int[]Get the foreground RGB values if using true color.inthashCode()static int[]hslToRgb(float h, float s, float l) Convert HSL (Hue, Saturation, Lightness) to RGB color values.booleanCheck if this color has any formatting applied.booleanCheck if this color uses true color (24-bit RGB) mode.static float[]rgbToHsl(int r, int g, int b) Convert RGB color values to HSL (Hue, Saturation, Lightness).Convert RGB color to nearest 16-color.Convert RGB color to nearest 256-color palette index.toString()toString(TerminalColor prev) Get the ANSI codes for this color relative to a previous color.voidwrite(PrintStream out) Write the color code to a print stream.
-
Constructor Details
-
TerminalColor
public TerminalColor()Create a default TerminalColor with no formatting. -
TerminalColor
Create a TerminalColor with the specified foreground and background colors.- Parameters:
text- the foreground colorbackground- the background color
-
TerminalColor
Create a TerminalColor with the specified colors and intensity.- Parameters:
textColor- the foreground colorbackground- the background colorintensity- the color intensity
-
TerminalColor
public TerminalColor(int text, int background) Create a TerminalColor using 256-color palette indices.0x00-0x07: standard colors (as in ESC [ 30..37 m) 0x08-0x0f: high intensity colors (as in ESC [ 90..97 m) 0x10-0xe7: 6*6*6=216 colors: 16 + 36*r + 6*g + b (0 le r,g,b le 5) 0xe8-0xff: grayscale from black to white in 24 steps
- Parameters:
text- foreground color index (0-255)background- background color index (0-255)
-
TerminalColor
Create a TerminalColor with 256-color foreground and basic background.- Parameters:
text- foreground color index (0-255)background- background color
-
TerminalColor
Create a TerminalColor with 256-color foreground, basic background, and intensity.- Parameters:
text- foreground color index (0-255)background- background colorintensity- the color intensity
-
TerminalColor
Create a TerminalColor with basic foreground and 256-color background.- Parameters:
text- foreground colorbackground- background color index (0-255)
-
TerminalColor
Create a TerminalColor with basic foreground, 256-color background, and intensity.- Parameters:
text- foreground colorbackground- background color index (0-255)intensity- the color intensity
-
-
Method Details
-
fromRGB
Create a TerminalColor with true color (24-bit RGB) foreground.- Parameters:
r- red component (0-255)g- green component (0-255)b- blue component (0-255)- Returns:
- a new TerminalColor with RGB foreground
-
fromRGB
Create a TerminalColor with true color (24-bit RGB) foreground and background.- Parameters:
textR- foreground red component (0-255)textG- foreground green component (0-255)textB- foreground blue component (0-255)bgR- background red component (0-255)bgG- background green component (0-255)bgB- background blue component (0-255)- Returns:
- a new TerminalColor with RGB foreground and background
-
fromHex
Create a TerminalColor with true color foreground from hex string.- Parameters:
hex- hex color string (e.g., "#FF5733" or "FF5733")- Returns:
- a new TerminalColor with RGB foreground, or default if invalid
-
fromHex
Create a TerminalColor with true color foreground and background from hex strings.- Parameters:
textHex- foreground hex color stringbgHex- background hex color string- Returns:
- a new TerminalColor with RGB colors, or default if invalid
-
fromHSL
Create a TerminalColor with true color foreground from HSL values.HSL (Hue, Saturation, Lightness) is often more intuitive for generating color palettes and gradients than RGB.
- Parameters:
h- hue in degrees (0-360)s- saturation as percentage (0-100)l- lightness as percentage (0-100)- Returns:
- a new TerminalColor with RGB foreground converted from HSL
-
fromHSL
public static TerminalColor fromHSL(float textH, float textS, float textL, float bgH, float bgS, float bgL) Create a TerminalColor with true color foreground and background from HSL values.- Parameters:
textH- foreground hue in degrees (0-360)textS- foreground saturation as percentage (0-100)textL- foreground lightness as percentage (0-100)bgH- background hue in degrees (0-360)bgS- background saturation as percentage (0-100)bgL- background lightness as percentage (0-100)- Returns:
- a new TerminalColor with RGB colors converted from HSL
-
hslToRgb
public static int[] hslToRgb(float h, float s, float l) Convert HSL (Hue, Saturation, Lightness) to RGB color values.This is useful for generating color palettes, as HSL makes it easy to:
- Create color variations by adjusting lightness
- Generate harmonious colors by rotating hue
- Control color intensity via saturation
- Parameters:
h- hue in degrees (0-360), wraps arounds- saturation as percentage (0-100), clampedl- lightness as percentage (0-100), clamped- Returns:
- RGB array [r, g, b] with values 0-255
-
rgbToHsl
public static float[] rgbToHsl(int r, int g, int b) Convert RGB color values to HSL (Hue, Saturation, Lightness).Useful for analyzing colors or adjusting their properties.
- Parameters:
r- red component (0-255)g- green component (0-255)b- blue component (0-255)- Returns:
- HSL array [h, s, l] where h is 0-360 and s,l are 0-100
-
getTextHSL
public float[] getTextHSL()Get the foreground color as HSL values, if using true color.- Returns:
- HSL array [h, s, l] or null if not using RGB foreground
-
getBackgroundHSL
public float[] getBackgroundHSL()Get the background color as HSL values, if using true color.- Returns:
- HSL array [h, s, l] or null if not using RGB background
-
isTrueColor
public boolean isTrueColor()Check if this color uses true color (24-bit RGB) mode.- Returns:
- true if using RGB colors
-
getTextRGB
public int[] getTextRGB()Get the foreground RGB values if using true color.- Returns:
- RGB array [r, g, b] or null if not using RGB foreground
-
getBackgroundRGB
public int[] getBackgroundRGB()Get the background RGB values if using true color.- Returns:
- RGB array [r, g, b] or null if not using RGB background
-
forError
Create a TerminalColor appropriate for error messages based on terminal theme.For dark themes, uses bright red. For light themes, uses dark red.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for error messages
-
forSuccess
Create a TerminalColor appropriate for success messages based on terminal theme.For dark themes, uses bright green. For light themes, uses dark green.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for success messages
-
forWarning
Create a TerminalColor appropriate for warning messages based on terminal theme.For dark themes, uses bright yellow. For light themes, uses dark yellow/orange.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for warning messages
-
forInfo
Create a TerminalColor appropriate for info messages based on terminal theme.For dark themes, uses bright blue/cyan. For light themes, uses dark blue.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for info messages
-
forHighlight
Create a TerminalColor appropriate for highlighted/emphasized text.For dark themes, uses bright white. For light themes, uses black.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for highlighted text
-
forMuted
Create a TerminalColor appropriate for muted/secondary text.Uses gray tones that are visible but not prominent.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for muted text
-
forTimestamp
Create a TerminalColor appropriate for timestamps in log output.Uses cyan tones that are subdued compared to the main message content, making timestamps visible but not distracting.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for timestamps
-
forMessage
Create a TerminalColor appropriate for highlighted messages in log output.Uses magenta tones that stand out from regular text and other log elements, suitable for emphasizing important message content.
- Parameters:
capability- the detected terminal color capability- Returns:
- a TerminalColor suitable for highlighted messages
-
forCapability
Create the best representation of this color for the given terminal capability.If the terminal doesn't support true color but this TerminalColor uses RGB, it will be downgraded to the nearest 256-color or 16-color equivalent.
- Parameters:
capability- the terminal's color capability- Returns:
- a TerminalColor compatible with the terminal's capabilities
-
toColor256
Convert RGB color to nearest 256-color palette index.- Returns:
- a new TerminalColor using 256-color mode
-
toColor16
Convert RGB color to nearest 16-color.- Returns:
- a new TerminalColor using basic 16 colors
-
isFormatted
public boolean isFormatted()Check if this color has any formatting applied.- Returns:
- true if any color is set (not all defaults)
-
equals
-
hashCode
public int hashCode() -
fullString
Get the full ANSI escape sequence for this color.- Returns:
- the complete ANSI color code
-
toString
-
getLength
public int getLength()Get the length of the ANSI escape sequence.- Returns:
- the length of the color code string
-
write
Write the color code to a print stream.- Parameters:
out- the output stream to write to
-
toString
Get the ANSI codes for this color relative to a previous color. Only outputs codes for attributes that differ from the previous color.- Parameters:
prev- the previous terminal color- Returns:
- the ANSI codes for changed attributes
-