|
| 1 | +# colors |
| 2 | + |
| 3 | +`colors` is a small Java 8+ library for querying and setting terminal colour palettes via OSC escape sequences, part of the [java-miniterm](../README.md) project. |
| 4 | + |
| 5 | +It supports reading the full 256-entry palette in a single burst, querying the foreground/background/cursor colours, redefining palette entries, and resetting them to the profile defaults. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +### Query foreground and background |
| 10 | + |
| 11 | +```java |
| 12 | +Color fg = TermColors.queryForeground(terminal, in); |
| 13 | +Color bg = TermColors.queryBackground(terminal, in); |
| 14 | +Color cursor = TermColors.queryCursor(terminal, in); |
| 15 | + |
| 16 | +if (bg != null) { |
| 17 | + // luminance check for dark/light theme detection |
| 18 | + boolean dark = (0.299 * bg.r8() + 0.587 * bg.g8() + 0.114 * bg.b8()) < 128; |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +Returns `null` if the terminal does not respond within the timeout. |
| 23 | + |
| 24 | +### Query a single palette entry |
| 25 | + |
| 26 | +```java |
| 27 | +Color color1 = TermColors.queryColor(terminal, in, 1); // ANSI red |
| 28 | +``` |
| 29 | + |
| 30 | +### Query the full 256-entry palette (burst) |
| 31 | + |
| 32 | +All 256 queries are sent at once; responses are collected until a read timeout indicates the terminal has no more data: |
| 33 | + |
| 34 | +```java |
| 35 | +Color[] palette = TermColors.queryPalette(terminal, in); |
| 36 | +// palette[n] is null if the terminal did not respond for that index |
| 37 | +``` |
| 38 | + |
| 39 | +Query a specific subset: |
| 40 | + |
| 41 | +```java |
| 42 | +// Query only the 16 ANSI colours |
| 43 | +int[] ansi16 = new int[16]; |
| 44 | +for (int i = 0; i < 16; i++) ansi16[i] = i; |
| 45 | +Color[] result = TermColors.queryPalette(terminal, in, ansi16); |
| 46 | +``` |
| 47 | + |
| 48 | +### Set colours |
| 49 | + |
| 50 | +```java |
| 51 | +TermColors.setForeground(terminal, Color.ofRgb8(220, 220, 220)); |
| 52 | +TermColors.setBackground(terminal, Color.ofRgb8(30, 30, 30)); |
| 53 | +TermColors.setCursor(terminal, Color.ofRgb8(255, 165, 0)); |
| 54 | + |
| 55 | +TermColors.setColor(terminal, 1, Color.ofRgb8(204, 0, 0)); // redefine ANSI red |
| 56 | +``` |
| 57 | + |
| 58 | +Set the full palette from an array (null entries are skipped): |
| 59 | + |
| 60 | +```java |
| 61 | +Color[] palette = buildThemePalette(); // Color[256] |
| 62 | +TermColors.setPalette(terminal, palette); |
| 63 | +``` |
| 64 | + |
| 65 | +### Reset to profile defaults |
| 66 | + |
| 67 | +```java |
| 68 | +TermColors.resetForeground(terminal); // OSC 110 |
| 69 | +TermColors.resetBackground(terminal); // OSC 111 |
| 70 | +TermColors.resetCursor(terminal); // OSC 112 |
| 71 | + |
| 72 | +TermColors.resetColor(terminal, 1); // reset single palette entry (OSC 104) |
| 73 | +TermColors.resetPalette(terminal); // reset all 256 entries (OSC 104) |
| 74 | +``` |
| 75 | + |
| 76 | +## Concepts |
| 77 | + |
| 78 | +### `TermColors` |
| 79 | + |
| 80 | +All methods are static. Query methods require the terminal in **raw mode** and an `IntReader` that returns negative values on timeout or EOF: |
| 81 | + |
| 82 | +```java |
| 83 | +terminal.enableRawMode(); |
| 84 | +IntReader in = () -> terminal.read(500); // 500 ms timeout per read |
| 85 | +``` |
| 86 | + |
| 87 | +### `Color` |
| 88 | + |
| 89 | +An immutable 16-bit-per-channel RGB colour, matching the precision used in the X11 `rgb:` colour specification that terminals return in OSC responses. |
| 90 | + |
| 91 | +```java |
| 92 | +Color red = Color.of(0xFFFF, 0x0000, 0x0000); // 16-bit channels |
| 93 | +Color green = Color.ofRgb8(0, 255, 0); // 8-bit, expanded |
| 94 | +Color blue = Color.parse("rgb:0000/0000/FFFF"); // from OSC string |
| 95 | +``` |
| 96 | + |
| 97 | +Conversion helpers: |
| 98 | + |
| 99 | +```java |
| 100 | +color.r8() // red channel downsampled to 8 bits (0-255) |
| 101 | +color.toRgb8() // packed 0xRRGGBB int |
| 102 | +color.toString() // "rgb:RRRR/GGGG/BBBB" — ready for use in OSC sequences |
| 103 | +``` |
| 104 | + |
| 105 | +## OSC sequence reference |
| 106 | + |
| 107 | +| Operation | Sequence | |
| 108 | +|---|---| |
| 109 | +| Query foreground | `OSC 10 ; ? BEL` | |
| 110 | +| Query background | `OSC 11 ; ? BEL` | |
| 111 | +| Query cursor colour | `OSC 12 ; ? BEL` | |
| 112 | +| Query palette entry *n* | `OSC 4 ; n ; ? BEL` | |
| 113 | +| Set foreground | `OSC 10 ; rgb:RRRR/GGGG/BBBB BEL` | |
| 114 | +| Set background | `OSC 11 ; rgb:RRRR/GGGG/BBBB BEL` | |
| 115 | +| Set cursor colour | `OSC 12 ; rgb:RRRR/GGGG/BBBB BEL` | |
| 116 | +| Set palette entry *n* | `OSC 4 ; n ; rgb:RRRR/GGGG/BBBB BEL` | |
| 117 | +| Reset foreground | `OSC 110 BEL` | |
| 118 | +| Reset background | `OSC 111 BEL` | |
| 119 | +| Reset cursor colour | `OSC 112 BEL` | |
| 120 | +| Reset palette entry *n* | `OSC 104 ; n BEL` | |
| 121 | +| Reset all palette entries | `OSC 104 BEL` | |
| 122 | + |
| 123 | +## Dependency |
| 124 | + |
| 125 | +### JBang |
| 126 | + |
| 127 | +```java |
| 128 | +//DEPS org.codejive.miniterm:colors:0.1.1 |
| 129 | +``` |
| 130 | + |
| 131 | +### Maven |
| 132 | + |
| 133 | +```xml |
| 134 | +<dependency> |
| 135 | + <groupId>org.codejive.miniterm</groupId> |
| 136 | + <artifactId>colors</artifactId> |
| 137 | + <version>0.1.1</version> |
| 138 | +</dependency> |
| 139 | +``` |
| 140 | + |
| 141 | +### Gradle |
| 142 | + |
| 143 | +```kotlin |
| 144 | +implementation("org.codejive.miniterm:colors:0.1.1") |
| 145 | +``` |
| 146 | + |
| 147 | +`colors` requires `ansiparser` (included transitively) for parsing OSC responses. |
0 commit comments