# hex-rgb

> Convert HEX color to RGBA

Latest version **5.0.0** (published 2021-05-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install hex-rgb
pnpm add hex-rgb
yarn add hex-rgb
bun add hex-rgb
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2021-05-03 |
| First published | 2014-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/hex-rgb) |
| Module format | ESM |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 133 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | hex, rgb, rgba, color, colour, convert, conversion, converter, css |

## Links

- npm: https://www.npmjs.com/package/hex-rgb
- Repository: https://github.com/sindresorhus/hex-rgb
- Homepage: https://github.com/sindresorhus/hex-rgb#readme
- Issues: https://github.com/sindresorhus/hex-rgb/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/hex-rgb

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

- 5.0.0 (latest) — 2021-05-03
- 4.3.0 — 2021-04-07
- 4.2.0 — 2020-09-06
- 4.1.0 — 2019-04-15
- 4.0.0 — 2019-02-10
- 3.0.0 — 2018-04-03
- 2.0.0 — 2017-12-30
- 1.0.0 — 2014-07-27

## README

# hex-rgb

> Convert HEX color to RGBA

## Install

```
$ npm install hex-rgb
```

## Usage

```js
import hexRgb from 'hex-rgb';

hexRgb('4183c4');
//=> {red: 65, green: 131, blue: 196, alpha: 1}

hexRgb('#4183c4');
//=> {red: 65, green: 131, blue: 196, alpha: 1}

hexRgb('#fff');
//=> {red: 255, green: 255, blue: 255, alpha: 1}

hexRgb('#22222299');
//=> {red: 34, green: 34, blue: 34, alpha: 0.6}

hexRgb('#0006');
//=> {red: 0, green: 0, blue: 0, alpha: 0.4}

hexRgb('#cd2222cc');
//=> {red: 205, green: 34, blue: 34, alpha: 0.8}

hexRgb('#cd2222cc', {format: 'array'});
//=> [205, 34, 34, 0.8]

hexRgb('#cd2222cc', {format: 'css'});
//=> 'rgb(205 34 34 / 80%)'

hexRgb('#000', {format: 'css'});
//=> 'rgb(0 0 0)'

hexRgb('#22222299', {alpha: 1});
//=> {red: 34, green: 34, blue: 34, alpha: 1}

hexRgb('#fff', {alpha: 0.5});
//=> {red: 255, green: 255, blue: 255, alpha: 0.5}
```

## API

### hexRgb(hex, options?)

#### hex

Type: `string`

The color in HEX format. Leading `#` is optional.

#### options

Type: `object`

##### format

Type: `string`\
Values: `'object' | 'array' | 'css'`\
Defaults: `'object'`

The RGB output format.

Note that when using the `css` format, the value of the alpha channel is rounded to two decimal places.

##### alpha

Type: `number`

Set the alpha of the color.

This overrides any existing alpha component in the Hex color string. For example, the `99` in `#22222299`.

The number must be in the range 0 to 1.

## Related

See [rgb-hex](https://github.com/sindresorhus/rgb-hex) for the inverse.

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