# to-ansi

> Convert HTML, RGB or HSL to ANSI

Latest version **1.4.4** (published 2023-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install to-ansi
pnpm add to-ansi
yarn add to-ansi
bun add to-ansi
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.4 |
| Published | 2023-01-18 |
| First published | 2022-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 27.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Patrice THIMOTHEE |
| Maintainers | thimpat |
| Keywords | color, convert colors, colour, console, terminal, productivity |

## Links

- npm: https://www.npmjs.com/package/to-ansi
- Repository: https://github.com/thimpat/to-ansi
- Homepage: https://github.com/thimpat/to-ansi/blob/main/README.md
- Issues: https://github.com/thimpat/to-ansi/issues
- npm.io page: https://npm.io/package/to-ansi

## Dependencies (1)

- [@types/node](https://npm.io/package/@types/node.md) ^18.11.18

## 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.4.4 (latest) — 2023-01-18
- 1.4.3 — 2022-04-28
- 1.4.2 — 2022-04-28
- 1.4.1 — 2022-04-28
- 1.4.0 — 2022-04-28
- 1.3.1 — 2022-04-27
- 1.3.0 — 2022-04-27
- 1.2.1 — 2022-04-23
- 1.2.0 — 2022-04-22
- 1.1.2 — 2022-04-19
- 1.1.1 — 2022-04-19
- 1.1.0 — 2022-04-19
- 1.0.5 — 2022-02-18
- 1.0.4 — 2022-02-17
- 1.0.3 — 2022-02-17
- … 3 more at https://npm.io/package/to-ansi/versions

## README

[![Test workflow](https://github.com/thimpat/to-ansi/actions/workflows/test.yml/badge.svg)](https://github.com/thimpat/to-ansi/blob/main/README.md)
[![nycrc Coverage](https://img.shields.io/nycrc/thimpat/to-ansi?preferredThreshold=lines)](https://github.com/thimpat/to-ansi/blob/main/README.md)
[![Version workflow](https://github.com/thimpat/to-ansi/actions/workflows/versioning.yml/badge.svg)](https://github.com/thimpat/to-ansi/blob/main/README.md)
[![npm version](https://badge.fury.io/js/to-ansi.svg)](https://www.npmjs.com/package/to-ansi)
<img alt="semantic-release" src="https://img.shields.io/badge/semantic--release-19.0.2-e10079?logo=semantic-release">



## Texts colorizer for terminals..
> 
> Easily change terminal content style with to-ansi.


### Installation

```shell
npm install to-ansi
```


---

### Usage

#### With commonJs

```javascript
const toAnsi = require("to-ansi");
```

#### With ESM

```javascript
import toAnsi from "to-ansi";
```

---

### Examples

#### Convert Hex, RGB, HSL or Color name to ANSI
```javascript
toAnsi.fromHexa("#00FF00");                      // => \x1b[38;5;46m 
toAnsi.fromRgb({red: 0, blue: 0, green: 255})    // => \x1b[38;5;46m 
toAnsi.fromColor("green");                       // => \x1b[38;5;46m
toAnsi.fromHsl({hue: 0.5, saturation: 0.5, lightness: 0.5})
```

#### Apply colors to the console via the **fromHexa** function
```javascript
console.log(
    toAnsi.fromHexa("#00FF00") +                        // Green text
    toAnsi.fromHexa("#FF0000", false) +                 // Red background (false = background)
    toAnsi.STYLE.Underline +                            // Underline text
    toAnsi.STYLE.Bold +                                 // Bold text
    "Okay" +                                            // Text to display
    toAnsi.RESET                                        
);

```

![](https://github.com/thimpat/to-ansi/blob/main/docs/images/example-1.png)


#### Apply colors to the console via the **getText** function (Recommended way)

```javascript
// Yellow
console.log( toAnsi.getTextFromColor("Hello you!", {fg: "yellow"}) );

// Yellow with blue background
console.log( toAnsi.getTextFromHex("Hello you!", {fg: "#FFFF00", bg: "#0000FF"}) );

// Yellow-ish on purple-lish
console.log( toAnsi.getTextFromHex("Hello you!", {fg: "#FFCCFF", bg: "#DD00FF", isUnderline: true}) );
```

![img.png](https://github.com/thimpat/to-ansi/blob/main/docs/images/example-2.png)


<br/>
---

### getTextFromColor

All call below are valid 
```javascript
toAnsi.getTextFromColor("Hello you!", {fg: "red"})
toAnsi.getTextFromColor("Hello you!", {fg: "#FF0000"})
toAnsi.getTextFromColor("Hello you!", {fg: {red: 255, green: 0, blue: 0}})
toAnsi.getTextFromColor("Hello you!", {fg: {hue: 0, saturation: 1, lightness: 0.5}})
```
<br/>

---

### Utility functions

```javascript
// Convert rgb to Hex
toAnsi.rgbToHex({red: 255, blue: 255, green: 255})

// Convert Hex to RGB
toAnsi.hexToRgb("#fff");

// Convert HSL to RGB
toAnsi.hslToRgb({hue: 0, saturation: 1, lightness: 0.5});

// Convert color name to Hex
toAnsi.colorNameToHex("red");
```

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