Skip to content

Commit f1eef73

Browse files
committed
test: add fallback for named colors tests
1 parent 4033615 commit f1eef73

File tree

3 files changed

+115
-15
lines changed

3 files changed

+115
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 4.2.0 (2025-09-20)
44

5-
- feat: add support extending with named truecolor HEX values.
5+
- feat: add support named truecolor via `ansis.extend()`.
66
Foreground methods are created from the provided color names, and matching background methods `bg*` are generated automatically.
77
Example:
88
```js
@@ -11,9 +11,11 @@
1111

1212
const color = ansis.extend(colorNames);
1313

14-
console.log(color.pink('Pink foreground'));
15-
console.log(color.bgPink('Pink background')); // auto-generated from "pink"
14+
console.log(color.orange('Orange foreground'));
15+
console.log(color.bgOrange('Orange background')); // auto-generated from "orange"
1616
```
17+
This release removes the last barrier for projects migrating from Chalk v4 that used named truecolor, e.g.
18+
`chalk.keyword('orange')('text')`. Ansis now provides this feature with a simpler, more intuitive API.
1719

1820
## 4.1.0 (2025-05-28)
1921

README.md

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Ansis is focused on [small size](#compare-size) and [speed](#benchmark) while pr
4040
- [ANSI 16 colors](#base-colors): `red`, `redBright`, `bgRed`, `bgRedBright`, ...
4141
- [ANSI 256 colors](#256-colors) via methods: `fg(num)`, `bg(num)`
4242
- [Truecolor](#truecolor) via methods: `rgb(r,g,b)`, `bgRgb(r,g,b)`, `hex('#rrggbb')`, `bgHex('#rrggbb')`
43-
- [Named truecolor](#extend-colors) (extendable with colors such as [pink, indigo, navy, ...](https://drafts.csswg.org/css-color/#named-colors)): `ansis.pink()`, `ansis.bgPink()`, ...
43+
- [Named truecolors](#extend-colors) (extend with colors such as [orange, pink, navy, ...](https://drafts.csswg.org/css-color/#named-colors)): `ansis.pink()`, `ansis.bgPink()`, ...
4444
- Auto-detects [color support](#color-support): Truecolor, 256 colors, 16 colors, no colors
4545
- Automatic [fallback](#fallback): Truecolor → 256 colors → 16 colors → no colors
4646
- Raw ANSI escape codes: ``` `File ${red.open}not found${red.close} in directory` ```
@@ -449,7 +449,7 @@ If a terminal supports only 16 colors then ANSI 256 colors will be interpolated
449449
</a>
450450
</div>
451451

452-
#### Usage example
452+
#### Example
453453

454454
```js
455455
import { bold, fg, bg } from 'ansis';
@@ -512,28 +512,30 @@ If you use the `hex()`, `rgb()` or `ansis256()` functions in a terminal not supp
512512

513513
![output](https://github.com/webdiscus/ansis/raw/master/docs/img/ansis-fallback.png?raw=true "Fallback to ANSI colors")
514514

515+
See also [fallback for named truecolors](#fallback-for-named-truecolors).
516+
515517
#### [↑ top](#top)
516518

517519
<a id="extend-colors" name="extend-colors"></a>
518520

519-
## Named truecolor
521+
## Named truecolors
520522

521523
Ansis supports full 24-bit color via `ansis.rgb(r, g, b)` and `ansis.hex('#rrggbb')`.\
522-
If you prefer [**named colors**](http://dev.w3.org/csswg/css-color/#named-colors) (e.g. `pink`, `orange`, `indigo`, etc.)
524+
If you prefer [**named colors**](http://dev.w3.org/csswg/css-color/#named-colors) (e.g. `orange`, `pink`, `navy`, etc.)
523525
instead of writing hex or RGB values by hand, resolve color names in your app and register them as extended styles on an Ansis instance via `ansis.extend()`.
524526
Then you can call e.g., `color.pink()` or `color.bgPink()` rather than using `ansis.hex('#ffc0cb')` or `ansis.bgHex('#ffc0cb')` directly.
525527

526528
> [!IMPORTANT]
527529
> Foreground methods are created from the provided color names, and matching background methods `bg*` are generated automatically.
528530
529531
> [!NOTE]
530-
> To keep Ansis small, it doesn't bundle large truecolor name tables.\
532+
> To keep Ansis small, it doesn't bundle large truecolors name table.\
531533
> Use any mapping package you like, e.g. [css-color-names](https://www.npmjs.com/package/css-color-names) (~6 kB).
532534
> ```bash
533535
> npm i css-color-names
534536
> ```
535537
536-
**Example (extend with all color names)**
538+
**Example (extend with all [CSS color names](http://dev.w3.org/csswg/css-color/#named-colors) )**
537539
538540
```js
539541
import ansis from 'ansis';
@@ -548,6 +550,18 @@ console.log(color.pink('Pink foreground'));
548550
console.log(color.bgPink('Pink background')); // auto-generated from "pink"
549551
```
550552
553+
If you prefer to keep the `ansis` namespace:
554+
555+
```js
556+
import { Ansis } from 'ansis';
557+
import colorNames from 'css-color-names';
558+
559+
// Create a new instance and extend it with colors
560+
const ansis = new Ansis().extend(colorNames);
561+
console.log(ansis.pink('Pink foreground'));
562+
console.log(ansis.bgPink('Pink background'));
563+
```
564+
551565
Of course, you can define a custom subset with only the colors you actually use.
552566
553567
> [!TIP]
@@ -556,21 +570,21 @@ Of course, you can define a custom subset with only the colors you actually use.
556570
**Example (custom subset)**
557571
558572
```js
559-
import { Ansis } from 'ansis';
573+
import ansis from 'ansis';
560574
561575
const myTheme = {
562576
orange: '#ffa500',
563577
pink: '#ffc0cb',
564578
};
565579
566-
// Create a new instance and extend it with only your colors
567-
const ansis = new Ansis().extend(myTheme);
580+
// Extend with only your colors
581+
const color = ansis.extend(myTheme);
568582
569583
// You can still use base styles together with extended ones
570-
const { orange, pink, bgPink, red } = ansis;
584+
const { orange, pink, bgPink, red } = color;
571585
572-
console.log(ansis.orange('orange foreground')); // extended foreground
573-
console.log(ansis.bgOrange('orange background')); // extended background
586+
console.log(color.orange('orange foreground')); // extended foreground
587+
console.log(color.bgOrange('orange background')); // extended background
574588
console.log(orange.italic`orange italic`); // extended + base style
575589
console.log(pink`pink foreground`); // extended as a tag
576590
console.log(bgPink`pink background`); // extended as a tag
@@ -619,6 +633,32 @@ console.log(color.bgPink`pink background`); // ✅ auto-generated background fro
619633
> color.bold.orange('bold orange'); // ❌ won't work: extended is on a sub-chain
620634
> ```
621635
636+
637+
<a id="fallback-for-named-truecolors"></a>
638+
### Fallback for named truecolors
639+
640+
Ansis automatically interpolates named truecolors to the highest available color level supported by the current environment.
641+
So you can safely use named truecolors anywhere without worrying about compatibility.
642+
643+
Example:
644+
```js
645+
import ansis from 'ansis';
646+
import colorNames from 'css-color-names';
647+
648+
const color = ansis.extend(colorNames);
649+
650+
console.log(color.orange('Text'));
651+
```
652+
653+
Output depending on terminal color support:
654+
655+
| Color level | Result | Example output |
656+
|--------------------|------------------------------------|------------------------------------|
657+
| Truecolor / 24-bit | `rgb(255,165,0)` (orange) | `\x1b[38;2;255;165;0mText\x1b[39m` |
658+
| 256 colors | [palette index](#256-colors) `214` | `\x1b[38;5;214mText\x1b[39m` |
659+
| 16 colors | code `93` (bright yellow) | `\x1b[93mText\x1b[39m` |
660+
| No color | plain text | `Text` |
661+
622662
---
623663
624664
#### [↑ top](#top)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { describe, test, expect, vi } from 'vitest';
2+
3+
import colorNames from 'css-color-names';
4+
import { Ansis } from 'ansis';
5+
6+
describe('fallback named colors', () => {
7+
const ansis16m = new Ansis(3); // force init with truecolor
8+
const ansis256 = new Ansis(2); // force init with 256 colors
9+
const ansis16 = new Ansis(1); // force init with 16 colors
10+
const ansisBW = new Ansis(0); // force init with black & white
11+
12+
test('fallback orange to 256 colors', async () => {
13+
const color = ansis256.extend(colorNames);
14+
const received = color.orange('orange');
15+
const expected = 'orange';
16+
console.log(received);
17+
expect(received).toEqual(expected);
18+
});
19+
20+
test('fallback bgOrange to 256 colors', async () => {
21+
const color = ansis256.extend(colorNames);
22+
const received = color.bgOrange('orange');
23+
const expected = 'orange';
24+
console.log(received);
25+
expect(received).toEqual(expected);
26+
});
27+
28+
test('fallback orange to 16 colors', async () => {
29+
const color = ansis16.extend(colorNames);
30+
const received = color.orange('orange');
31+
const expected = 'orange';
32+
console.log(received);
33+
expect(received).toEqual(expected);
34+
});
35+
36+
test('fallback bgOrange to 16 colors', async () => {
37+
const color = ansis16.extend(colorNames);
38+
const received = color.bgOrange('orange');
39+
const expected = 'orange';
40+
console.log(received);
41+
expect(received).toEqual(expected);
42+
});
43+
44+
test('fallback orange to BW colors', async () => {
45+
const color = ansisBW.extend(colorNames);
46+
const received = color.orange('orange');
47+
const expected = 'orange';
48+
expect(received).toEqual(expected);
49+
});
50+
51+
test('fallback bgOrange to BW colors', async () => {
52+
const color = ansisBW.extend(colorNames);
53+
const received = color.bgOrange('orange');
54+
const expected = 'orange';
55+
expect(received).toEqual(expected);
56+
});
57+
58+
});

0 commit comments

Comments
 (0)