Package org.aesh.terminal.tty
Class TerminalColorDetector
java.lang.Object
org.aesh.terminal.tty.TerminalColorDetector
Utility class to detect terminal color capabilities.
This detector can query the terminal to determine:
- Color depth (8, 16, 256, or true color)
- Background theme (light or dark)
- Actual foreground and background RGB colors
Detection methods:
- Environment variables (COLORTERM, TERM) via
TerminalEnvironment - terminfo/infocmp database (max_colors capability)
- OSC 10/11 queries for actual terminal colors
- Platform-specific config files via
PlatformThemeDetector
The detector supports caching to avoid repeated detection overhead.
Use detectCached(Connection) for cached detection.
- Author:
- Ståle W. Pedersen
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidClear the cached color capability.static TerminalColorCapabilitydetect(Connection connection) Detect terminal color capabilities using a fast OSC query (FG + BG only).static TerminalColorCapabilitydetect(Connection connection, boolean queryTerminal) Detect terminal color capabilities.static TerminalColorCapabilitydetectCached(Connection connection) Detect terminal color capabilities using cached result if available.static ColorDepthdetectColorDepth(Connection connection) Detect only the color depth without querying the terminal for colors.static TerminalColorCapabilitydetectFast(Connection connection) Get a fast, non-blocking color capability based only on environment detection.static TerminalColorCapabilitydetectFull(Connection connection) Detect terminal color capabilities including cursor and all 16 palette colors.static TerminalThemeDetect terminal theme from environment variables and platform-specific config files.static TerminalColorCapabilityGet the cached color capability without performing detection.static booleanisDarkColor(int[] rgb) Check if an RGB color represents a dark theme (luminance < 0.5).static booleanisOscColorQuerySupported(Connection connection) Check if the terminal likely supports OSC color queries.queryAnsi16Colors(Connection connection, long timeoutMs) Query the ANSI 16-color palette (colors 0-15) in a single batch operation.static TerminalColorCapabilityqueryColorCapability(TerminalConnection connection, long timeoutMs) Query terminal color capabilities using synchronous I/O.queryColors(Connection connection, long timeoutMs) Query foreground, background, and cursor colors in a single batch operation.queryColorsWithFallback(Connection connection, long timeoutMs) Query colors with automatic fallback to environment-based detection.queryPaletteColors(Connection connection, long timeoutMs, int... indices) Query multiple palette colors in a single batch operation.static TerminalColorCapabilityqueryThemeColors(Connection connection, long timeoutMs) Fast query for theme-relevant colors only (foreground and background).
-
Method Details
-
detectCached
Detect terminal color capabilities using cached result if available.- Parameters:
connection- the terminal connection- Returns:
- detected color capabilities (may be cached)
-
clearCache
public static void clearCache()Clear the cached color capability. -
getCached
Get the cached color capability without performing detection.- Returns:
- the cached capability, or null if not cached
-
detect
Detect terminal color capabilities using a fast OSC query (FG + BG only).- Parameters:
connection- the terminal connection- Returns:
- detected color capabilities
-
detect
Detect terminal color capabilities.- Parameters:
connection- the terminal connectionqueryTerminal- if true, send OSC queries to the terminal- Returns:
- detected color capabilities
-
detectFull
Detect terminal color capabilities including cursor and all 16 palette colors.- Parameters:
connection- the terminal connection- Returns:
- detected color capabilities including palette colors
-
detectFast
Get a fast, non-blocking color capability based only on environment detection.- Parameters:
connection- the terminal connection (may be null)- Returns:
- detected color capabilities
-
detectColorDepth
Detect only the color depth without querying the terminal for colors.- Parameters:
connection- the terminal connection- Returns:
- the detected color depth
-
detectThemeFromEnvironment
Detect terminal theme from environment variables and platform-specific config files.- Returns:
- the detected theme, or UNKNOWN if not detectable
-
queryColorCapability
public static TerminalColorCapability queryColorCapability(TerminalConnection connection, long timeoutMs) Query terminal color capabilities using synchronous I/O.- Parameters:
connection- the terminal connection (must be a TerminalConnection)timeoutMs- timeout in milliseconds to wait for all responses- Returns:
- TerminalColorCapability with detected colors, or null if not supported
-
queryThemeColors
Fast query for theme-relevant colors only (foreground and background).- Parameters:
connection- the terminal connectiontimeoutMs- timeout in milliseconds- Returns:
- TerminalColorCapability with FG/BG colors, or null if not supported
-
queryColors
Query foreground, background, and cursor colors in a single batch operation.- Parameters:
connection- the terminal connectiontimeoutMs- timeout in milliseconds to wait for all responses- Returns:
- map from OSC code to RGB array [r, g, b] (0-255 each)
-
queryPaletteColors
public static Map<Integer,int[]> queryPaletteColors(Connection connection, long timeoutMs, int... indices) Query multiple palette colors in a single batch operation.- Parameters:
connection- the terminal connectiontimeoutMs- timeout in milliseconds to wait for all responsesindices- the palette color indices to query (0-255)- Returns:
- map from palette index to RGB array [r, g, b] (0-255 each)
-
queryAnsi16Colors
Query the ANSI 16-color palette (colors 0-15) in a single batch operation.- Parameters:
connection- the terminal connectiontimeoutMs- timeout in milliseconds to wait for all responses- Returns:
- map from palette index (0-15) to RGB array [r, g, b] (0-255 each)
-
queryColorsWithFallback
Query colors with automatic fallback to environment-based detection.- Parameters:
connection- the terminal connectiontimeoutMs- timeout in milliseconds for OSC queries- Returns:
- map from OSC code to RGB array; always contains at least estimated foreground and background colors
-
isOscColorQuerySupported
Check if the terminal likely supports OSC color queries.- Parameters:
connection- the terminal connection to check- Returns:
- true if OSC color queries are likely supported
-
isDarkColor
public static boolean isDarkColor(int[] rgb) Check if an RGB color represents a dark theme (luminance < 0.5).- Parameters:
rgb- the RGB color array [r, g, b] (0-255 each)- Returns:
- true if the color is dark, false if light
-