Skip to content

Commit 4af983c

Browse files
committed
added docs for ANSI improvements
1 parent c1edd8a commit 4af983c

3 files changed

Lines changed: 174 additions & 0 deletions

File tree

content/docs/readline/color-detection.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,30 @@ if (connection.querySupportsOscQueries(500)) {
279279

280280
Terminals that report modern features (ANSI color, Sixel graphics, device class >= 62) typically support OSC queries.
281281

282+
#### Converting RGB to ANSI Color Codes
283+
284+
After querying RGB colors, you can convert them to ANSI color codes using the `ANSI` utility class:
285+
286+
```java
287+
int[] bgColor = connection.queryBackgroundColor(500);
288+
289+
if (bgColor != null) {
290+
// Convert to 256-color palette index
291+
int paletteIndex = ANSI.rgbTo256Color(bgColor[0], bgColor[1], bgColor[2]);
292+
293+
// Convert to basic ANSI foreground code (30-37 or 90-97)
294+
int ansiCode = ANSI.rgbToAnsiColor(bgColor[0], bgColor[1], bgColor[2]);
295+
296+
// Check brightness for theme detection
297+
boolean isDark = !ANSI.rgbIsBright(bgColor[0], bgColor[1], bgColor[2]);
298+
299+
// Reverse conversion: get RGB from palette index
300+
int[] rgb = ANSI.color256ToRgb(paletteIndex);
301+
}
302+
```
303+
304+
See [Terminal Colors](terminal-colors#ansi-color-utilities) for complete documentation of RGB/ANSI conversion utilities.
305+
282306
### 2. Environment Variables
283307

284308
Checks standard environment variables:

content/docs/readline/connection.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,40 @@ if (bg != null) {
199199
int[] cursor = connection.queryCursorColor(500);
200200
```
201201
202+
### Palette Color Queries
203+
204+
Query colors from the 256-color palette using OSC 4:
205+
206+
```java
207+
// Query palette color by index (0-255)
208+
int[] color = connection.queryPaletteColor(1, 500);
209+
if (color != null) {
210+
System.out.println("Palette 1: RGB(" + color[0] + "," + color[1] + "," + color[2] + ")");
211+
212+
// Convert to nearest 256-color index
213+
int index = ANSI.rgbTo256Color(color[0], color[1], color[2]);
214+
215+
// Convert to basic ANSI code
216+
int ansiCode = ANSI.rgbToAnsiColor(color[0], color[1], color[2]);
217+
}
218+
```
219+
220+
Palette indices:
221+
- 0-7: Standard ANSI colors
222+
- 8-15: Bright ANSI colors
223+
- 16-231: 6x6x6 color cube
224+
- 232-255: Grayscale ramp
225+
226+
### OSC Query with Index Parameter
227+
228+
For OSC codes that require an index (like OSC 4), use the indexed query method:
229+
230+
```java
231+
// Query OSC 4 with index parameter
232+
int[] rgb = connection.queryOsc(4, 1, "?", 500,
233+
input -> ANSI.parseOscColorResponse(input, 4, 1));
234+
```
235+
202236
### OSC Support Detection
203237
204238
Check if the terminal supports OSC queries:

content/docs/readline/terminal-colors.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,122 @@ The RGB to 16-color conversion:
224224
2. **Dominant channel**: Determines base color from strongest RGB channel
225225
3. **Intensity**: Bright if luminance > 60%, normal otherwise
226226

227+
## ANSI Color Utilities
228+
229+
The `ANSI` class provides utility methods for converting between RGB values and ANSI color codes. This is useful when you have RGB values (e.g., from OSC color queries) and need to find the closest ANSI equivalent.
230+
231+
### RGB to 256-Color Palette
232+
233+
Convert RGB to the nearest 256-color palette index:
234+
235+
```java
236+
import org.aesh.terminal.utils.ANSI;
237+
238+
// Convert RGB to 256-color palette index (16-255)
239+
int index = ANSI.rgbTo256Color(255, 128, 0); // Returns palette index
240+
241+
// Use in escape sequence
242+
connection.write("\u001B[38;5;" + index + "mColored text\u001B[0m");
243+
```
244+
245+
### RGB to Basic ANSI Colors
246+
247+
Convert RGB to basic ANSI color codes (16 colors):
248+
249+
```java
250+
// Get foreground color code (30-37 or 90-97)
251+
// Brightness is automatically determined from luminance
252+
int fgCode = ANSI.rgbToAnsiColor(255, 0, 0); // Returns 91 (bright red)
253+
254+
// With explicit brightness control
255+
int normalRed = ANSI.rgbToAnsiColor(255, 0, 0, false); // Returns 31
256+
int brightRed = ANSI.rgbToAnsiColor(255, 0, 0, true); // Returns 91
257+
258+
// Get background color code (40-47 or 100-107)
259+
int bgCode = ANSI.rgbToAnsiBackgroundColor(0, 0, 128); // Returns blue background
260+
261+
// Get just the basic color code (30-37) without brightness offset
262+
int basicCode = ANSI.rgbToBasicColorCode(0, 200, 0); // Returns 32 (green)
263+
```
264+
265+
### Basic Color Code Reference
266+
267+
| Code | Color | Bright Code |
268+
|------|-------|-------------|
269+
| 30 | Black | 90 |
270+
| 31 | Red | 91 |
271+
| 32 | Green | 92 |
272+
| 33 | Yellow | 93 |
273+
| 34 | Blue | 94 |
274+
| 35 | Magenta | 95 |
275+
| 36 | Cyan | 96 |
276+
| 37 | White | 97 |
277+
278+
Background codes add 10 (40-47 normal, 100-107 bright).
279+
280+
### Brightness Detection
281+
282+
Check if an RGB color should use the bright variant:
283+
284+
```java
285+
boolean isBright = ANSI.rgbIsBright(200, 200, 200); // true
286+
boolean isDark = ANSI.rgbIsBright(50, 50, 50); // false
287+
```
288+
289+
### Reverse Conversion: 256-Color to RGB
290+
291+
Convert a 256-color palette index back to RGB:
292+
293+
```java
294+
// Get RGB values for a palette index
295+
int[] rgb = ANSI.color256ToRgb(196); // Returns [255, 0, 0] for bright red
296+
int[] gray = ANSI.color256ToRgb(244); // Returns grayscale RGB
297+
298+
System.out.println("R=" + rgb[0] + " G=" + rgb[1] + " B=" + rgb[2]);
299+
```
300+
301+
### Example: Using with OSC Color Queries
302+
303+
Combine color queries with ANSI conversion:
304+
305+
```java
306+
// Query the terminal's background color
307+
int[] bgColor = connection.queryBackgroundColor(500);
308+
309+
if (bgColor != null) {
310+
// Convert to 256-color index
311+
int paletteIndex = ANSI.rgbTo256Color(bgColor[0], bgColor[1], bgColor[2]);
312+
System.out.println("Background is palette color: " + paletteIndex);
313+
314+
// Convert to basic ANSI code
315+
int ansiCode = ANSI.rgbToAnsiColor(bgColor[0], bgColor[1], bgColor[2]);
316+
System.out.println("Nearest basic ANSI code: " + ansiCode);
317+
318+
// Check brightness for theme detection
319+
boolean isDark = !ANSI.rgbIsBright(bgColor[0], bgColor[1], bgColor[2]);
320+
System.out.println("Theme: " + (isDark ? "dark" : "light"));
321+
}
322+
```
323+
324+
### Example: Palette Color Round-Trip
325+
326+
```java
327+
// Query a palette color from the terminal
328+
int[] rgb = connection.queryPaletteColor(1, 500);
329+
330+
if (rgb != null) {
331+
// Convert back to palette index
332+
int index = ANSI.rgbTo256Color(rgb[0], rgb[1], rgb[2]);
333+
334+
// Find nearest basic ANSI equivalent
335+
int ansiCode = ANSI.rgbToAnsiColor(rgb[0], rgb[1], rgb[2]);
336+
337+
System.out.println("Palette 1: RGB(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")");
338+
System.out.println(" -> 256-color index: " + index);
339+
System.out.println(" -> Basic ANSI code: " + ansiCode);
340+
}
341+
```
342+
227343
## Using with TerminalString
228344

229345
`TerminalColor` is typically used with `TerminalString` for styled output:

0 commit comments

Comments
 (0)