ansi-styles
Advanced tools
Comparing version 6.0.0 to 6.1.0
@@ -19,2 +19,4 @@ export interface CSPair { | ||
ansi(code: number): string; | ||
ansi256(code: number): string; | ||
@@ -157,2 +159,25 @@ | ||
hexToAnsi256(hex: string): number; | ||
/** | ||
Convert from the ANSI 256 color space to the ANSI 16 color space. | ||
@param code - A number representing the ANSI 256 color. | ||
*/ | ||
ansi256ToAnsi(code: number): number; | ||
/** | ||
Convert from the RGB color space to the ANSI 16 color space. | ||
@param red - (`0...255`) | ||
@param green - (`0...255`) | ||
@param blue - (`0...255`) | ||
*/ | ||
rgbToAnsi(red: number, green: number, blue: number): number; | ||
/** | ||
Convert from the RGB HEX color space to the ANSI 16 color space. | ||
@param hex - A hexadecimal string containing RGB data. | ||
*/ | ||
hexToAnsi(hex: string): number; | ||
} | ||
@@ -159,0 +184,0 @@ |
56
index.js
const ANSI_BACKGROUND_OFFSET = 10; | ||
const wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`; | ||
const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`; | ||
@@ -96,4 +98,6 @@ | ||
styles.color.ansi = wrapAnsi16(); | ||
styles.color.ansi256 = wrapAnsi256(); | ||
styles.color.ansi16m = wrapAnsi16m(); | ||
styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET); | ||
styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET); | ||
@@ -153,2 +157,54 @@ styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET); | ||
enumerable: false | ||
}, | ||
ansi256ToAnsi: { | ||
value: code => { | ||
if (code < 8) { | ||
return 30 + code; | ||
} | ||
if (code < 16) { | ||
return 90 + (code - 8); | ||
} | ||
let red; | ||
let green; | ||
let blue; | ||
if (code >= 232) { | ||
red = (((code - 232) * 10) + 8) / 255; | ||
green = red; | ||
blue = red; | ||
} else { | ||
code -= 16; | ||
const remainder = code % 36; | ||
red = Math.floor(code / 36) / 5; | ||
green = Math.floor(remainder / 6) / 5; | ||
blue = (remainder % 6) / 5; | ||
} | ||
const value = Math.max(red, green, blue) * 2; | ||
if (value === 0) { | ||
return 30; | ||
} | ||
let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red)); | ||
if (value === 2) { | ||
result += 60; | ||
} | ||
return result; | ||
}, | ||
enumerable: false | ||
}, | ||
rgbToAnsi: { | ||
value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)), | ||
enumerable: false | ||
}, | ||
hexToAnsi: { | ||
value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)), | ||
enumerable: false | ||
} | ||
@@ -155,0 +211,0 @@ }); |
{ | ||
"name": "ansi-styles", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "ANSI escape codes for styling strings in the terminal", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -28,2 +28,3 @@ # ansi-styles | ||
// original color. | ||
console.log(`${styles.color.ansi(styles.rgbToAnsi(199, 20, 250))}Hello World${styles.color.close}`) | ||
console.log(`${styles.color.ansi256(styles.rgbToAnsi256(199, 20, 250))}Hello World${styles.color.close}`) | ||
@@ -116,7 +117,7 @@ console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${styles.color.close}`) | ||
## [256 / 16 million (TrueColor) support](https://gist.github.com/XVilka/8346728) | ||
## 16 / 256 / 16 million (TrueColor) support | ||
`ansi-styles` allows converting between various color formats and ANSI escapes, with support for 256 and 16 million colors. | ||
`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). | ||
The following color spaces from `color-convert` are supported: | ||
The following color spaces are supported: | ||
@@ -126,2 +127,3 @@ - `rgb` | ||
- `ansi256` | ||
- `ansi` | ||
@@ -133,2 +135,5 @@ To use these, call the associated conversion function with the intended output, for example: | ||
styles.color.ansi(styles.rgbToAnsi(100, 200, 15)); // RGB to 16 color ansi foreground code | ||
styles.bgColor.ansi(styles.hexToAnsi('#C0FFEE')); // HEX to 16 color ansi foreground code | ||
styles.color.ansi256(styles.rgbToAnsi256(100, 200, 15)); // RGB to 256 color ansi foreground code | ||
@@ -135,0 +140,0 @@ styles.bgColor.ansi256(styles.hexToAnsi256('#C0FFEE')); // HEX to 256 color ansi foreground code |
15535
334
156