ansi-styles 6.0.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +25 -0
- package/index.js +56 -0
- package/package.json +1 -1
- package/readme.md +8 -3
package/index.d.ts
CHANGED
@@ -16,6 +16,8 @@ export interface ColorBase {
|
|
16
16
|
*/
|
17
17
|
readonly close: string;
|
18
18
|
|
19
|
+
ansi(code: number): string;
|
20
|
+
|
19
21
|
ansi256(code: number): string;
|
20
22
|
|
21
23
|
ansi16m(red: number, green: number, blue: number): string;
|
@@ -153,6 +155,29 @@ export interface ConvertColor {
|
|
153
155
|
@param hex - A hexadecimal string containing RGB data.
|
154
156
|
*/
|
155
157
|
hexToAnsi256(hex: string): number;
|
158
|
+
|
159
|
+
/**
|
160
|
+
Convert from the ANSI 256 color space to the ANSI 16 color space.
|
161
|
+
|
162
|
+
@param code - A number representing the ANSI 256 color.
|
163
|
+
*/
|
164
|
+
ansi256ToAnsi(code: number): number;
|
165
|
+
|
166
|
+
/**
|
167
|
+
Convert from the RGB color space to the ANSI 16 color space.
|
168
|
+
|
169
|
+
@param red - (`0...255`)
|
170
|
+
@param green - (`0...255`)
|
171
|
+
@param blue - (`0...255`)
|
172
|
+
*/
|
173
|
+
rgbToAnsi(red: number, green: number, blue: number): number;
|
174
|
+
|
175
|
+
/**
|
176
|
+
Convert from the RGB HEX color space to the ANSI 16 color space.
|
177
|
+
|
178
|
+
@param hex - A hexadecimal string containing RGB data.
|
179
|
+
*/
|
180
|
+
hexToAnsi(hex: string): number;
|
156
181
|
}
|
157
182
|
|
158
183
|
declare const ansiStyles: {
|
package/index.js
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
const ANSI_BACKGROUND_OFFSET = 10;
|
2
2
|
|
3
|
+
const wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`;
|
4
|
+
|
3
5
|
const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
|
4
6
|
|
5
7
|
const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
|
@@ -93,8 +95,10 @@ function assembleStyles() {
|
|
93
95
|
styles.color.close = '\u001B[39m';
|
94
96
|
styles.bgColor.close = '\u001B[49m';
|
95
97
|
|
98
|
+
styles.color.ansi = wrapAnsi16();
|
96
99
|
styles.color.ansi256 = wrapAnsi256();
|
97
100
|
styles.color.ansi16m = wrapAnsi16m();
|
101
|
+
styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
|
98
102
|
styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
|
99
103
|
styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
|
100
104
|
|
@@ -149,6 +153,58 @@ function assembleStyles() {
|
|
149
153
|
hexToAnsi256: {
|
150
154
|
value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
|
151
155
|
enumerable: false
|
156
|
+
},
|
157
|
+
ansi256ToAnsi: {
|
158
|
+
value: code => {
|
159
|
+
if (code < 8) {
|
160
|
+
return 30 + code;
|
161
|
+
}
|
162
|
+
|
163
|
+
if (code < 16) {
|
164
|
+
return 90 + (code - 8);
|
165
|
+
}
|
166
|
+
|
167
|
+
let red;
|
168
|
+
let green;
|
169
|
+
let blue;
|
170
|
+
|
171
|
+
if (code >= 232) {
|
172
|
+
red = (((code - 232) * 10) + 8) / 255;
|
173
|
+
green = red;
|
174
|
+
blue = red;
|
175
|
+
} else {
|
176
|
+
code -= 16;
|
177
|
+
|
178
|
+
const remainder = code % 36;
|
179
|
+
|
180
|
+
red = Math.floor(code / 36) / 5;
|
181
|
+
green = Math.floor(remainder / 6) / 5;
|
182
|
+
blue = (remainder % 6) / 5;
|
183
|
+
}
|
184
|
+
|
185
|
+
const value = Math.max(red, green, blue) * 2;
|
186
|
+
|
187
|
+
if (value === 0) {
|
188
|
+
return 30;
|
189
|
+
}
|
190
|
+
|
191
|
+
let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
|
192
|
+
|
193
|
+
if (value === 2) {
|
194
|
+
result += 60;
|
195
|
+
}
|
196
|
+
|
197
|
+
return result;
|
198
|
+
},
|
199
|
+
enumerable: false
|
200
|
+
},
|
201
|
+
rgbToAnsi: {
|
202
|
+
value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
|
203
|
+
enumerable: false
|
204
|
+
},
|
205
|
+
hexToAnsi: {
|
206
|
+
value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
|
207
|
+
enumerable: false
|
152
208
|
}
|
153
209
|
});
|
154
210
|
|
package/package.json
CHANGED
package/readme.md
CHANGED
@@ -25,6 +25,7 @@ console.log(`${styles.green.open}Hello world!${styles.green.close}`);
|
|
25
25
|
// may be degraded to fit the new color palette. This means terminals
|
26
26
|
// that do not support 16 million colors will best-match the
|
27
27
|
// original color.
|
28
|
+
console.log(`${styles.color.ansi(styles.rgbToAnsi(199, 20, 250))}Hello World${styles.color.close}`)
|
28
29
|
console.log(`${styles.color.ansi256(styles.rgbToAnsi256(199, 20, 250))}Hello World${styles.color.close}`)
|
29
30
|
console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${styles.color.close}`)
|
30
31
|
```
|
@@ -112,21 +113,25 @@ console.log(styles.codes.get(36));
|
|
112
113
|
//=> 39
|
113
114
|
```
|
114
115
|
|
115
|
-
##
|
116
|
+
## 16 / 256 / 16 million (TrueColor) support
|
116
117
|
|
117
|
-
`ansi-styles` allows converting between various color formats and ANSI escapes, with support for 256 and 16 million colors.
|
118
|
+
`ansi-styles` allows converting between various color formats and ANSI escapes, with support for 16, 256 and [16 million colors](https://gist.github.com/XVilka/8346728).
|
118
119
|
|
119
|
-
The following color spaces
|
120
|
+
The following color spaces are supported:
|
120
121
|
|
121
122
|
- `rgb`
|
122
123
|
- `hex`
|
123
124
|
- `ansi256`
|
125
|
+
- `ansi`
|
124
126
|
|
125
127
|
To use these, call the associated conversion function with the intended output, for example:
|
126
128
|
|
127
129
|
```js
|
128
130
|
import styles from 'ansi-styles';
|
129
131
|
|
132
|
+
styles.color.ansi(styles.rgbToAnsi(100, 200, 15)); // RGB to 16 color ansi foreground code
|
133
|
+
styles.bgColor.ansi(styles.hexToAnsi('#C0FFEE')); // HEX to 16 color ansi foreground code
|
134
|
+
|
130
135
|
styles.color.ansi256(styles.rgbToAnsi256(100, 200, 15)); // RGB to 256 color ansi foreground code
|
131
136
|
styles.bgColor.ansi256(styles.hexToAnsi256('#C0FFEE')); // HEX to 256 color ansi foreground code
|
132
137
|
|