ansi-styles
Advanced tools
Comparing version 4.3.0 to 5.0.0
243
index.d.ts
@@ -1,212 +0,2 @@ | ||
declare type CSSColor = | ||
| 'aliceblue' | ||
| 'antiquewhite' | ||
| 'aqua' | ||
| 'aquamarine' | ||
| 'azure' | ||
| 'beige' | ||
| 'bisque' | ||
| 'black' | ||
| 'blanchedalmond' | ||
| 'blue' | ||
| 'blueviolet' | ||
| 'brown' | ||
| 'burlywood' | ||
| 'cadetblue' | ||
| 'chartreuse' | ||
| 'chocolate' | ||
| 'coral' | ||
| 'cornflowerblue' | ||
| 'cornsilk' | ||
| 'crimson' | ||
| 'cyan' | ||
| 'darkblue' | ||
| 'darkcyan' | ||
| 'darkgoldenrod' | ||
| 'darkgray' | ||
| 'darkgreen' | ||
| 'darkgrey' | ||
| 'darkkhaki' | ||
| 'darkmagenta' | ||
| 'darkolivegreen' | ||
| 'darkorange' | ||
| 'darkorchid' | ||
| 'darkred' | ||
| 'darksalmon' | ||
| 'darkseagreen' | ||
| 'darkslateblue' | ||
| 'darkslategray' | ||
| 'darkslategrey' | ||
| 'darkturquoise' | ||
| 'darkviolet' | ||
| 'deeppink' | ||
| 'deepskyblue' | ||
| 'dimgray' | ||
| 'dimgrey' | ||
| 'dodgerblue' | ||
| 'firebrick' | ||
| 'floralwhite' | ||
| 'forestgreen' | ||
| 'fuchsia' | ||
| 'gainsboro' | ||
| 'ghostwhite' | ||
| 'gold' | ||
| 'goldenrod' | ||
| 'gray' | ||
| 'green' | ||
| 'greenyellow' | ||
| 'grey' | ||
| 'honeydew' | ||
| 'hotpink' | ||
| 'indianred' | ||
| 'indigo' | ||
| 'ivory' | ||
| 'khaki' | ||
| 'lavender' | ||
| 'lavenderblush' | ||
| 'lawngreen' | ||
| 'lemonchiffon' | ||
| 'lightblue' | ||
| 'lightcoral' | ||
| 'lightcyan' | ||
| 'lightgoldenrodyellow' | ||
| 'lightgray' | ||
| 'lightgreen' | ||
| 'lightgrey' | ||
| 'lightpink' | ||
| 'lightsalmon' | ||
| 'lightseagreen' | ||
| 'lightskyblue' | ||
| 'lightslategray' | ||
| 'lightslategrey' | ||
| 'lightsteelblue' | ||
| 'lightyellow' | ||
| 'lime' | ||
| 'limegreen' | ||
| 'linen' | ||
| 'magenta' | ||
| 'maroon' | ||
| 'mediumaquamarine' | ||
| 'mediumblue' | ||
| 'mediumorchid' | ||
| 'mediumpurple' | ||
| 'mediumseagreen' | ||
| 'mediumslateblue' | ||
| 'mediumspringgreen' | ||
| 'mediumturquoise' | ||
| 'mediumvioletred' | ||
| 'midnightblue' | ||
| 'mintcream' | ||
| 'mistyrose' | ||
| 'moccasin' | ||
| 'navajowhite' | ||
| 'navy' | ||
| 'oldlace' | ||
| 'olive' | ||
| 'olivedrab' | ||
| 'orange' | ||
| 'orangered' | ||
| 'orchid' | ||
| 'palegoldenrod' | ||
| 'palegreen' | ||
| 'paleturquoise' | ||
| 'palevioletred' | ||
| 'papayawhip' | ||
| 'peachpuff' | ||
| 'peru' | ||
| 'pink' | ||
| 'plum' | ||
| 'powderblue' | ||
| 'purple' | ||
| 'rebeccapurple' | ||
| 'red' | ||
| 'rosybrown' | ||
| 'royalblue' | ||
| 'saddlebrown' | ||
| 'salmon' | ||
| 'sandybrown' | ||
| 'seagreen' | ||
| 'seashell' | ||
| 'sienna' | ||
| 'silver' | ||
| 'skyblue' | ||
| 'slateblue' | ||
| 'slategray' | ||
| 'slategrey' | ||
| 'snow' | ||
| 'springgreen' | ||
| 'steelblue' | ||
| 'tan' | ||
| 'teal' | ||
| 'thistle' | ||
| 'tomato' | ||
| 'turquoise' | ||
| 'violet' | ||
| 'wheat' | ||
| 'white' | ||
| 'whitesmoke' | ||
| 'yellow' | ||
| 'yellowgreen'; | ||
declare namespace ansiStyles { | ||
interface ColorConvert { | ||
/** | ||
The RGB color space. | ||
@param red - (`0`-`255`) | ||
@param green - (`0`-`255`) | ||
@param blue - (`0`-`255`) | ||
*/ | ||
rgb(red: number, green: number, blue: number): string; | ||
/** | ||
The RGB HEX color space. | ||
@param hex - A hexadecimal string containing RGB data. | ||
*/ | ||
hex(hex: string): string; | ||
/** | ||
@param keyword - A CSS color name. | ||
*/ | ||
keyword(keyword: CSSColor): string; | ||
/** | ||
The HSL color space. | ||
@param hue - (`0`-`360`) | ||
@param saturation - (`0`-`100`) | ||
@param lightness - (`0`-`100`) | ||
*/ | ||
hsl(hue: number, saturation: number, lightness: number): string; | ||
/** | ||
The HSV color space. | ||
@param hue - (`0`-`360`) | ||
@param saturation - (`0`-`100`) | ||
@param value - (`0`-`100`) | ||
*/ | ||
hsv(hue: number, saturation: number, value: number): string; | ||
/** | ||
The HSV color space. | ||
@param hue - (`0`-`360`) | ||
@param whiteness - (`0`-`100`) | ||
@param blackness - (`0`-`100`) | ||
*/ | ||
hwb(hue: number, whiteness: number, blackness: number): string; | ||
/** | ||
Use a [4-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4-bit) to set text color. | ||
*/ | ||
ansi(ansi: number): string; | ||
/** | ||
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color. | ||
*/ | ||
ansi256(ansi: number): string; | ||
} | ||
interface CSPair { | ||
@@ -225,6 +15,6 @@ /** | ||
interface ColorBase { | ||
readonly ansi: ColorConvert; | ||
readonly ansi256: ColorConvert; | ||
readonly ansi16m: ColorConvert; | ||
ansi256(code: number): string; | ||
ansi16m(red: number, green: number, blue: number): string; | ||
/** | ||
@@ -337,2 +127,27 @@ The ANSI terminal control sequence for ending this color. | ||
} | ||
interface ConvertColor { | ||
/** | ||
Convert from the RGB color space to the ANSI 256 color space. | ||
@param red - (`0...255`) | ||
@param green - (`0...255`) | ||
@param blue - (`0...255`) | ||
*/ | ||
rgbToAnsi256(red: number, green: number, blue: number): number; | ||
/** | ||
Convert from the RGB HEX color space to the RGB color space. | ||
@param hex - A hexadecimal string containing RGB data. | ||
*/ | ||
hexToRgb(hex: string): [red: number, green: number, blue: number]; | ||
/** | ||
Convert from the RGB HEX color space to the ANSI 256 color space. | ||
@param hex - A hexadecimal string containing RGB data. | ||
*/ | ||
hexToAnsi256(hex: string): number; | ||
} | ||
} | ||
@@ -345,4 +160,4 @@ | ||
readonly codes: ReadonlyMap<number, number>; | ||
} & ansiStyles.BackgroundColor & ansiStyles.ForegroundColor & ansiStyles.Modifier; | ||
} & ansiStyles.BackgroundColor & ansiStyles.ForegroundColor & ansiStyles.Modifier & ansiStyles.ConvertColor; | ||
export = ansiStyles; |
113
index.js
'use strict'; | ||
const wrapAnsi16 = (fn, offset) => (...args) => { | ||
const code = fn(...args); | ||
return `\u001B[${code + offset}m`; | ||
}; | ||
const ANSI_BACKGROUND_OFFSET = 10; | ||
const wrapAnsi256 = (fn, offset) => (...args) => { | ||
const code = fn(...args); | ||
return `\u001B[${38 + offset};5;${code}m`; | ||
}; | ||
const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`; | ||
const wrapAnsi16m = (fn, offset) => (...args) => { | ||
const rgb = fn(...args); | ||
return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`; | ||
}; | ||
const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`; | ||
const ansi2ansi = n => n; | ||
const rgb2rgb = (r, g, b) => [r, g, b]; | ||
const setLazyProperty = (object, property, get) => { | ||
Object.defineProperty(object, property, { | ||
get: () => { | ||
const value = get(); | ||
Object.defineProperty(object, property, { | ||
value, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return value; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
}; | ||
/** @type {typeof import('color-convert')} */ | ||
let colorConvert; | ||
const makeDynamicStyles = (wrap, targetSpace, identity, isBackground) => { | ||
if (colorConvert === undefined) { | ||
colorConvert = require('color-convert'); | ||
} | ||
const offset = isBackground ? 10 : 0; | ||
const styles = {}; | ||
for (const [sourceSpace, suite] of Object.entries(colorConvert)) { | ||
const name = sourceSpace === 'ansi16' ? 'ansi' : sourceSpace; | ||
if (sourceSpace === targetSpace) { | ||
styles[name] = wrap(identity, offset); | ||
} else if (typeof suite === 'object') { | ||
styles[name] = wrap(suite[targetSpace], offset); | ||
} | ||
} | ||
return styles; | ||
}; | ||
function assembleStyles() { | ||
@@ -149,9 +97,52 @@ const codes = new Map(); | ||
setLazyProperty(styles.color, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, false)); | ||
setLazyProperty(styles.color, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, false)); | ||
setLazyProperty(styles.color, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, false)); | ||
setLazyProperty(styles.bgColor, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, true)); | ||
setLazyProperty(styles.bgColor, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, true)); | ||
setLazyProperty(styles.bgColor, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, true)); | ||
styles.color.ansi256 = wrapAnsi256(); | ||
styles.color.ansi16m = wrapAnsi16m(); | ||
styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET); | ||
styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET); | ||
// From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js | ||
styles.rgbToAnsi256 = (red, green, blue) => { | ||
// We use the extended greyscale palette here, with the exception of | ||
// black and white. normal palette only has 4 greyscale shades. | ||
if (red === green && green === blue) { | ||
if (red < 8) { | ||
return 16; | ||
} | ||
if (red > 248) { | ||
return 231; | ||
} | ||
return Math.round(((red - 8) / 247) * 24) + 232; | ||
} | ||
return 16 + | ||
(36 * Math.round(red / 255 * 5)) + | ||
(6 * Math.round(green / 255 * 5)) + | ||
Math.round(blue / 255 * 5); | ||
}; | ||
styles.hexToRgb = hex => { | ||
const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16)); | ||
if (!matches) { | ||
return [0, 0, 0]; | ||
} | ||
let {colorString} = matches.groups; | ||
if (colorString.length === 3) { | ||
colorString = colorString.split('').map(character => character + character).join(''); | ||
} | ||
const integer = Number.parseInt(colorString, 16); | ||
return [ | ||
(integer >> 16) & 0xFF, | ||
(integer >> 8) & 0xFF, | ||
integer & 0xFF | ||
]; | ||
}; | ||
styles.hexToAnsi256 = hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)); | ||
return styles; | ||
@@ -158,0 +149,0 @@ } |
{ | ||
"name": "ansi-styles", | ||
"version": "4.3.0", | ||
"version": "5.0.0", | ||
"description": "ANSI escape codes for styling strings in the terminal", | ||
@@ -11,6 +11,6 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -47,5 +47,2 @@ "scripts": { | ||
], | ||
"dependencies": { | ||
"color-convert": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
@@ -52,0 +49,0 @@ "@types/color-convert": "^1.9.0", |
@@ -1,2 +0,2 @@ | ||
# ansi-styles [![Build Status](https://travis-ci.org/chalk/ansi-styles.svg?branch=master)](https://travis-ci.org/chalk/ansi-styles) | ||
# ansi-styles | ||
@@ -28,5 +28,4 @@ > [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal | ||
// original color. | ||
console.log(style.bgColor.ansi.hsl(120, 80, 72) + 'Hello world!' + style.bgColor.close); | ||
console.log(style.color.ansi256.rgb(199, 20, 250) + 'Hello world!' + style.color.close); | ||
console.log(style.color.ansi16m.hex('#abcdef') + 'Hello world!' + style.color.close); | ||
console.log(`${style.color.ansi256(style.rgbToAnsi256(199, 20, 250))}Hello World${style.color.close}`) | ||
console.log(`${style.color.ansi16m(...style.hexToRgb('#abcdef'))}Hello World${style.color.close}`) | ||
``` | ||
@@ -114,3 +113,3 @@ | ||
`ansi-styles` uses the [`color-convert`](https://github.com/Qix-/color-convert) package to allow for converting between various colors 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 256 and 16 million colors. | ||
@@ -121,7 +120,2 @@ The following color spaces from `color-convert` are supported: | ||
- `hex` | ||
- `keyword` | ||
- `hsl` | ||
- `hsv` | ||
- `hwb` | ||
- `ansi` | ||
- `ansi256` | ||
@@ -132,10 +126,7 @@ | ||
```js | ||
style.color.ansi.rgb(100, 200, 15); // RGB to 16 color ansi foreground code | ||
style.bgColor.ansi.rgb(100, 200, 15); // RGB to 16 color ansi background code | ||
style.color.ansi256(style.rgbToAnsi256(100, 200, 15)); // RGB to 256 color ansi foreground code | ||
style.bgColor.ansi256(style.hexToAnsi256('#C0FFEE')); // HEX to 256 color ansi foreground code | ||
style.color.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code | ||
style.bgColor.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code | ||
style.color.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground code | ||
style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color background code | ||
style.color.ansi16m(100, 200, 15); // RGB to 16 million color foreground code | ||
style.bgColor.ansi16m(...style.hexToRgb('#C0FFEE')); // Hex (RGB) to 16 million color foreground code | ||
``` | ||
@@ -142,0 +133,0 @@ |
0
13185
260
144
- Removedcolor-convert@^2.0.1
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)