You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Extend](#extend-colors)base colors with [**named truecolor**](https://drafts.csswg.org/css-color/#named-colors) via `ansis.extend()`, then use colors by name, e.g. `ansis.pink()`
41
+
-[ANSI 256 colors](#256-colors) via methods: `fg(num)`, `bg(num)`
-[Named truecolors](#extend-colors)(extend with colors such as [orange, pink, navy, ...](https://drafts.csswg.org/css-color/#named-colors)): `ansis.pink()`, `ansis.bgPink()`, ...
-Enables reliable[CLI testing](#cli-testing) with forced [color levels](#color-levels): no color, 16, 256 or Truecolor
50
-
-Replacement for [`chalk`](#replacing-chalk)[`ansi-colors`](#replacing-ansi-colors)[`colorette`](#replacing-colorette)[`picocolors`](#replacing-picocolors) and others [alternatives](#alternatives)
49
+
-Reliable[CLI testing](#cli-testing) with forced [color levels](#color-levels): no color, 16, 256 or Truecolor
50
+
-Drop-in replacement for [`chalk`](#replacing-chalk)[`ansi-colors`](#replacing-ansi-colors)[`colorette`](#replacing-colorette)[`picocolors`](#replacing-picocolors) and others [alternatives](#alternatives)
51
51
52
52
53
53
> 🚀 **You might also like**[`flaget`](https://github.com/webdiscus/flaget) - a smaller (5 kB) and faster alternative to [`yargs-parser`](https://www.npmjs.com/package/yargs-parser) (85 kB) for CLI argument parsing.
@@ -449,7 +449,7 @@ If a terminal supports only 16 colors then ANSI 256 colors will be interpolated
449
449
</a>
450
450
</div>
451
451
452
-
#### Usage example
452
+
#### Example
453
453
454
454
```js
455
455
import { bold, fg, bg } from'ansis';
@@ -512,26 +512,30 @@ If you use the `hex()`, `rgb()` or `ansis256()` functions in a terminal not supp
512
512
513
513

514
514
515
+
See also [fallback for named truecolors](#fallback-for-named-truecolors).
516
+
515
517
#### [↑ top](#top)
516
518
517
519
<aid="extend-colors"name="extend-colors"></a>
518
520
519
-
## Named truecolor
521
+
## Named truecolors
520
522
521
523
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. `beige`, `orange`, `pink`, `royalblue`, etc.) instead of writing hex or RGB values by hand,
523
-
resolve color names in your app and register them as extended styles on an Ansis instance via `ansis.extend()`.
524
-
Then you can call `color.pink()` rather than using `ansis.hex()` or `ansis.rgb()` directly.
524
+
If you prefer [**named colors**](http://dev.w3.org/csswg/css-color/#named-colors) (e.g. `orange`, `pink`, `navy`, etc.)
525
+
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()`.
526
+
Then you can call e.g., `color.pink()` or `color.bgPink()` rather than using `ansis.hex('#ffc0cb')` or `ansis.bgHex('#ffc0cb')` directly.
527
+
528
+
> [!IMPORTANT]
529
+
> Foreground methods are created from the provided color names, and matching background methods `bg*` are generated automatically.
525
530
526
531
> [!NOTE]
527
-
> To keep Ansis small, it doesn't bundle large color name tables.\
532
+
> To keep Ansis small, it doesn't bundle large truecolors name table.\
528
533
> Use any mapping package you like, e.g. [css-color-names](https://www.npmjs.com/package/css-color-names) (~6 kB).
534
+
> ```bash
535
+
> npm i css-color-names
536
+
>```
529
537
530
-
```bash
531
-
npm i css-color-names
532
-
```
533
-
534
-
**Example (extend with all color names)**
538
+
**Example (extend with all [CSS color names](http://dev.w3.org/csswg/css-color/#named-colors) )**
535
539
536
540
```js
537
541
import ansis from 'ansis';
@@ -542,47 +546,59 @@ import colorNames from 'css-color-names';
542
546
const color = ansis.extend(colorNames);
543
547
544
548
// All color names are now avaliable as chainable methods on the extended instance:
545
-
console.log(color.pink('Pink color'));
546
-
console.log(color.pink.underline('Pink color underlined'));
549
+
console.log(color.pink('Pink foreground'));
550
+
console.log(color.bgPink('Pink background')); // auto-generated from "pink"
551
+
```
547
552
548
-
// You can achieve the same result without extend():
549
-
console.log(ansis.hex(colorNames.pink).underline('Pink color via hex'));
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'));
550
563
```
551
564
552
-
Of course, you can define a custom subset with only the names you actually use.
565
+
Of course, you can define a custom subset with only the colors you actually use.
553
566
554
567
> [!TIP]
555
568
> Need help picking a color name? Try the [Name that Color](https://chir.ag/projects/name-that-color/#FF681F) tool - paste a hex and get its closest color name.
556
569
557
570
**Example (custom subset)**
558
571
559
572
```js
560
-
import{ Ansis }from'ansis';
573
+
import ansis from 'ansis';
561
574
562
575
const myTheme = {
563
576
orange: '#ffa500',
564
577
pink: '#ffc0cb',
565
578
};
566
579
567
-
//Create a new instance and extend it with only your custom names
568
-
constansis=newAnsis().extend(myTheme);
580
+
// Extend with only your colors
581
+
const color = ansis.extend(myTheme);
569
582
570
583
// You can still use base styles together with extended ones
571
-
const { orange, pink, red } =ansis;
584
+
const { orange, pink, bgPink, red } = color;
572
585
573
-
console.log(ansis.orange.bold('orange bold')); // extended + base
0 commit comments