You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/readline/terminal-colors.md
+116Lines changed: 116 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -224,6 +224,122 @@ The RGB to 16-color conversion:
224
224
2.**Dominant channel**: Determines base color from strongest RGB channel
225
225
3.**Intensity**: Bright if luminance > 60%, normal otherwise
226
226
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
+
importorg.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)
0 commit comments