hex-rgb
Convert HEX color to RGBA
Last updated 24 days ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install hex-rgb 
SYNC missed versions from official npm registry.

hex-rgb Build Status

Convert HEX color to RGBA

Install

$ npm install hex-rgb

Usage

const hexRgb = require('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)'

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.

Related

See rgb-hex for the inverse.

Current Tags

  • 4.2.0                                ...           latest (24 days ago)

6 Versions

  • 4.2.0                                ...           24 days ago
  • 4.1.0                                ...           a year ago
  • 4.0.0                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 152
This Month 0
Last Day 34
Last Week 439
Last Month 1,328
Dependencies (0)
None
Dev Dependencies (3)
Dependents (142)

Copyright 2014 - 2017 © taobao.org |