Skip to content

Commit ff549c5

Browse files
authored
Fix: Downsample ansi256() and bgAnsi256() to 16 colors at level 1 (#687)
1 parent 678e550 commit ff549c5

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎readme.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ console.log(foregroundColorNames.includes('pink'));
225225

226226
Chalk supports 256 colors and [Truecolor](https://github.com/termstandard/colors) (16 million colors) on supported terminal apps.
227227

228-
Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying `{level: n}` as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 91 (ANSI escape for bright red).
228+
Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying `{level: n}` as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 91 (ANSI escape for bright red). The same applies to `ansi256` values, so `chalk.ansi256(196)` also becomes 91 at level 1.
229229

230230
Examples:
231231

‎source/index.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export interface ChalkInstance {
7474
/**
7575
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
7676
77+
The value is downsampled to the 16-color palette on terminals that only support basic colors (level 1), so `chalk.ansi256(196)` becomes 91 (ANSI escape for bright red).
78+
7779
@example
7880
```
7981
import chalk from 'chalk';
@@ -112,6 +114,8 @@ export interface ChalkInstance {
112114
/**
113115
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
114116
117+
The value is downsampled to the 16-color palette on terminals that only support basic colors (level 1), so `chalk.bgAnsi256(196)` becomes 101 (ANSI escape for bright red background).
118+
115119
@example
116120
```
117121
import chalk from 'chalk';

‎source/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ const getModelAnsi = (model, level, type, ...arguments_) => {
8888
return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_));
8989
}
9090

91+
if (model === 'ansi256' && level === 'ansi') {
92+
return ansiStyles[type].ansi(ansiStyles.ansi256ToAnsi(...arguments_));
93+
}
94+
9195
return ansiStyles[type][model](...arguments_);
9296
};
9397

‎test/chalk.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,28 @@ test('properly convert RGB to 256 colors on basic color terminals', t => {
105105
t.is(new Chalk({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m');
106106
});
107107

108-
test('don\'t emit RGB codes if level is 0', t => {
108+
test('properly convert ANSI 256 to 16 colors on basic color terminals', t => {
109+
t.is(new Chalk({level: 1}).ansi256(196)('hello'), '\u001B[91mhello\u001B[39m');
110+
t.is(new Chalk({level: 1}).bgAnsi256(196)('hello'), '\u001B[101mhello\u001B[49m');
111+
t.is(new Chalk({level: 1}).ansi256(2)('hello'), '\u001B[32mhello\u001B[39m');
112+
t.is(new Chalk({level: 1}).bgAnsi256(2)('hello'), '\u001B[42mhello\u001B[49m');
113+
t.is(new Chalk({level: 1}).ansi256(8)('hello'), '\u001B[90mhello\u001B[39m');
114+
t.is(new Chalk({level: 1}).ansi256(232)('hello'), '\u001B[30mhello\u001B[39m');
115+
t.is(new Chalk({level: 1}).ansi256(255)('hello'), '\u001B[37mhello\u001B[39m');
116+
});
117+
118+
test('keep ANSI 256 colors on 256 color and Truecolor terminals', t => {
119+
t.is(new Chalk({level: 2}).ansi256(196)('hello'), '\u001B[38;5;196mhello\u001B[39m');
120+
t.is(new Chalk({level: 2}).bgAnsi256(196)('hello'), '\u001B[48;5;196mhello\u001B[49m');
121+
t.is(new Chalk({level: 3}).ansi256(196)('hello'), '\u001B[38;5;196mhello\u001B[39m');
122+
t.is(new Chalk({level: 3}).bgAnsi256(196)('hello'), '\u001B[48;5;196mhello\u001B[49m');
123+
});
124+
125+
test('don\'t emit color codes if level is 0', t => {
109126
t.is(new Chalk({level: 0}).hex('#FF0000')('hello'), 'hello');
110127
t.is(new Chalk({level: 0}).bgHex('#FF0000')('hello'), 'hello');
128+
t.is(new Chalk({level: 0}).ansi256(196)('hello'), 'hello');
129+
t.is(new Chalk({level: 0}).bgAnsi256(196)('hello'), 'hello');
111130
});
112131

113132
test('supports blackBright color', t => {

0 commit comments

Comments
 (0)