# ansicolour

> Fork of ansicolor with CSS closer to that used in devtools and a few fixes

Latest version **1.1.0** (published 2017-02-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install ansicolour
pnpm add ansicolour
yarn add ansicolour
bun add ansicolour
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-02-28 |
| First published | 2017-02-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 126 |
| Author | Nexii Malthus |
| Maintainers | nexii |
| Keywords | ansi, color, colour, ansi color, ansi-color, ansi colour, ansi-colour, ansicolour, ansi styles, console, logging, log, developer tools, devtools, webinspector, colour logging, tty colours, tty |

## Links

- npm: https://www.npmjs.com/package/ansicolour
- Repository: https://github.com/xpl/ansicolor
- Homepage: https://github.com/xpl/ansicolor#readme
- Issues: https://github.com/xpl/ansicolor/issues
- npm.io page: https://npm.io/package/ansicolour

## Dependencies (1)

- [es7-object-polyfill](https://npm.io/package/es7-object-polyfill.md) ^0.0.1

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2017-02-28

## README

# ansicolor<sup>BETA</sup>

A quality library for the ANSI color/style management.

```bash
npm install ansicolor
```

## Why another one?

Other tools lack consistency, failing to solve the simple hierarchy problem:

```javascript
require ('colors') // most popular color utility

console.log (('foo'.cyan + 'bar').red)
```

![pic](http://wtf.jpg.wtf/85/9b/1470626860-859b24350e22df74fd7497e9dc0d8d42.png)

WTF, `bar` is not rendered red! It sucks. **Ansicolor** arranges styles in stack and reconstructs proper linear form from that stack:

```javascript
require ('ansicolor').nice // .nice for unsafe String extensions

console.log (('foo'.cyan + 'bar').red)
```

![pic](http://wtf.jpg.wtf/3c/61/1470626989-3c61b64d0690b0b413be367841650426.png)

Nice!

### Cross-platform rendering

Other tools provide output (rendering), but not input (parsing). Inspection of ANSI colors in arbitrary strings is essential when implementing cross-platform logging — that works not only in terminal, but in browsers too. Modern browsers support color logging with `console.log`, but it does not understand ANSI colors — having a proprietary CSS-based format instead. 

**Ansicolor** solves that problem by converting color codes to argument lists that are understandable by browser's consoles:

```javascript
parsed = color.parse ('foo' + ('bar'.red.underline.bright.inverse + 'baz').bgGreen)

parsed.browserConsoleArguments /* = [
    "%cfoo%cbar%cbaz",
    "",
    "font-weight: bold;font-style: underline;background:rgba(255,51,0,1);color:rgba(0,204,0,1);",
    "background:rgba(0,204,0,1);"
] */

console.log (...parsed.browserConsoleArguments) // prints with colors in Chrome!
```

## Crash course

String wrapping (safe):

```javascript
color = require ('ansicolor')

console.log ('foo' + color.green (color.inverse (color.bgBrightCyan ('bar')) + 'baz') + 'qux')
```

String wrapping (unsafe):

```javascript
require ('ansicolor').nice

console.log ('foo'.red.bright + 'bar'.bgYellow.underline.dim)
```

All supported options:

```javascript
'foreground colors'
    .black.red.green.yellow.blue.magenta.cyan.white
```
```javascript
'background colors'
    .bgBlack.bgRed.bgGreen.bgYellow.bgBlue.bgMagenta.bgCyan.bgWhite
```
```javascript
'bright background colors'
    .bgBrightBlack.bgBrightRed.bgBrightGreen.bgBrightYellow.bgBrightBlue.bgBrightMagenta.bgBrightCyan.bgBrightWhite
```
```javascript
'styles'
    .bright.dim.italic.underline.inverse // italic may lack support on your platform
```

## Converting to CSS

Parsing arbitrary strings styled with ANSI escape codes:

```javascript
parsed = color.parse ('foo'.bgBrightRed + 'bar')
                            
```

Will return a pseudo-array of styled spans (iterable with `for ... of` and convertable to an array with spread operator):

```javascript
[{ css: 'background:rgba(255,51,0,1);', text: 'foo' },
 { css: '',                             text: 'bar' } ])]
```

Converting parsed array to argument list (acceptable by Chrome's `console.log`):

```javascript
console.log (...parsed.browserConsoleArguments)
```

Happy logging!

---
_Source: https://npm.io/package/ansicolour · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
