# color-string

> Parser and generator for CSS color strings

Latest version **2.1.4** (published 2025-11-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install color-string
pnpm add color-string
yarn add color-string
bun add color-string
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.4 |
| Published | 2025-11-15 |
| First published | 2011-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 1 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 221 |
| Author | Josh Junon |
| Maintainers | qix |
| Keywords | color, colour, rgb, css |

## Links

- npm: https://www.npmjs.com/package/color-string
- Repository: https://github.com/Qix-/color-string
- Homepage: https://github.com/Qix-/color-string#readme
- Issues: https://github.com/Qix-/color-string/issues
- npm.io page: https://npm.io/package/color-string

## Dependencies (1)

- [color-name](https://npm.io/package/color-name.md) ^2.0.0

## 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

- 2.1.4 (latest) — 2025-11-15
- 2.1.3 — 2025-11-14
- 2.1.2 — 2025-09-13
- 2.1.0 — 2025-08-30
- 2.0.1 — 2025-02-14
- 2.0.0 — 2025-02-06
- 1.9.1 — 2022-04-22
- 1.9.0 — 2021-12-03
- 1.8.2 — 2021-11-30
- 1.8.1 — 2021-11-30
- 1.8.0 — 2021-11-30
- 1.7.4 — 2021-11-26
- 1.7.3 — 2021-11-26
- 1.7.2 — 2021-11-26
- 1.7.1 — 2021-11-26
- … 27 more at https://npm.io/package/color-string/versions

## README

# color-string

> library for parsing and generating CSS color strings.

## Install

```sh
npm install color-string
```

## Usage

### Parsing

```js
colorString.get('#FFF')                          // {model: 'rgb', value: [255, 255, 255, 1]}
colorString.get('#FFFA')                         // {model: 'rgb', value: [255, 255, 255, 0.67]}
colorString.get('#FFFFFFAA')                     // {model: 'rgb', value: [255, 255, 255, 0.67]}
colorString.get('hsl(360, 100%, 50%)')           // {model: 'hsl', value: [0, 100, 50, 1]}
colorString.get('hsl(360 100% 50%)')             // {model: 'hsl', value: [0, 100, 50, 1]}
colorString.get('hwb(60, 3%, 60%)')              // {model: 'hwb', value: [60, 3, 60, 1]}

colorString.get.rgb('#FFF')                      // [255, 255, 255, 1]
colorString.get.rgb('blue')                      // [0, 0, 255, 1]
colorString.get.rgb('rgba(200, 60, 60, 0.3)')    // [200, 60, 60, 0.3]
colorString.get.rgb('rgba(200 60 60 / 0.3)')     // [200, 60, 60, 0.3]
colorString.get.rgb('rgba(200 60 60 / 30%)')     // [200, 60, 60, 0.3]
colorString.get.rgb('rgb(200, 200, 200)')        // [200, 200, 200, 1]
colorString.get.rgb('rgb(200 200 200)')          // [200, 200, 200, 1]

colorString.get.hsl('hsl(360, 100%, 50%)')       // [0, 100, 50, 1]
colorString.get.hsl('hsl(360 100% 50%)')         // [0, 100, 50, 1]
colorString.get.hsl('hsla(360, 60%, 50%, 0.4)')  // [0, 60, 50, 0.4]
colorString.get.hsl('hsl(360 60% 50% / 0.4)')    // [0, 60, 50, 0.4]

colorString.get.hwb('hwb(60 3% 60%)')            // [60, 3, 60, 1]
colorString.get.hwb('hwb(60, 3%, 60%)')          // [60, 3, 60, 1]
colorString.get.hwb('hwb(60, 3%, 60%, 0.6)')     // [60, 3, 60, 0.6]

colorString.get.rgb('invalid color string')      // null
```

### Generation

```js
colorString.to.hex(255, 255, 255)     // "#FFFFFF"
colorString.to.hex(0, 0, 255, 0.4)    // "#0000FF66"
colorString.to.hex(0, 0, 255, 0.4)    // "#0000FF66"
colorString.to.rgb(255, 255, 255)     // "rgb(255, 255, 255)"
colorString.to.rgb(0, 0, 255, 0.4)    // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb(0, 0, 255, 0.4)    // "rgba(0, 0, 255, 0.4)"
colorString.to.rgb.percent(0, 0, 255) // "rgb(0%, 0%, 100%)"
colorString.to.keyword(255, 255, 0)   // "yellow"
colorString.to.hsl(360, 100, 100)     // "hsl(360, 100%, 100%)"
colorString.to.hwb(50, 3, 15)         // "hwb(50, 3%, 15%)"
```

## License

MIT

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