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(TerminalFeatures) for cached detection.
- Author:
- Ståle W. Pedersen
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidClear the cached color capability.static TerminalColorCapabilitydetect(TerminalFeatures terminal) Detect terminal color capabilities using a fast OSC query (FG + BG only).static TerminalColorCapabilitydetect(TerminalFeatures terminal, boolean queryTerminal) Detect terminal color capabilities.static TerminalColorCapabilitydetectCached(TerminalFeatures terminal) Detect terminal color capabilities using cached result if available.static ColorDepthdetectColorDepth(TerminalFeatures terminal) Detect only the color depth without querying the terminal for colors.static TerminalColorCapabilitydetectFast(TerminalFeatures terminal) Get a fast, non-blocking color capability based only on environment detection.static TerminalColorCapabilitydetectFull(TerminalFeatures terminal) 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(TerminalFeatures terminal) Check if the terminal likely supports OSC color queries.queryAnsi16Colors(TerminalFeatures terminal, long timeoutMs) Query the ANSI 16-color palette (colors 0-15) in a single batch operation.static TerminalColorCapabilityqueryColorCapability(TerminalFeatures terminal, long timeoutMs) Query terminal color capabilities using synchronous I/O.queryColors(TerminalFeatures terminal, long timeoutMs) Query foreground, background, and cursor colors in a single batch operation.queryColorsWithFallback(TerminalFeatures terminal, long timeoutMs) Query colors with automatic fallback to environment-based detection.queryPaletteColors(TerminalFeatures terminal, long timeoutMs, int... indices) Query multiple palette colors in a single batch operation.static TerminalColorCapabilityqueryThemeColors(TerminalFeatures terminal, 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:
terminal- the terminal features- 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:
terminal- the terminal features- Returns:
- detected color capabilities
-
detect
Detect terminal color capabilities.- Parameters:
terminal- the terminal featuresqueryTerminal- 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:
terminal- the terminal features- Returns:
- detected color capabilities including palette colors
-
detectFast
Get a fast, non-blocking color capability based only on environment detection.- Parameters:
terminal- the terminal features (may be null)- Returns:
- detected color capabilities
-
detectColorDepth
Detect only the color depth without querying the terminal for colors.- Parameters:
terminal- the terminal features- 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(TerminalFeatures terminal, long timeoutMs) Query terminal color capabilities using synchronous I/O.- Parameters:
terminal- the terminal features (must wrap 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:
terminal- the terminal featurestimeoutMs- 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:
terminal- the terminal featurestimeoutMs- 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(TerminalFeatures terminal, long timeoutMs, int... indices) Query multiple palette colors in a single batch operation.- Parameters:
terminal- the terminal featurestimeoutMs- 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:
terminal- the terminal featurestimeoutMs- 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:
terminal- the terminal featurestimeoutMs- 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:
terminal- the terminal features 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
-