rgba-convert
Convert rgba values from and to various format
Accepted formats
Format | Value |
---|---|
array | [r, g, b] or [r, g, b, a] |
object | {r, g, b} or {r, g, b, a} or {r} ... |
hex string | #rgb or #rgba or #rrggbb or #rrggbbaa |
css string | rgb(r, g, b) or rgba(r, g, b, a) |
integer | 4294967295 ≥ integer ≥ 0 |
css name | red or green ... only basic names |
On parse error, return black with full opacity
Install
npm i rgba-convert
API
rgba(data)
Convert as [r, g, b, a]
array
Note that rgba.arr()
is an alias of rgba()
const rgba = // [255, 0, 0, 255] // [255, 0, 0, 255] // [255, 0, 0, 102] // [0, 255, 0, 255] // [0, 255, 0, 128] // [255, 0, 0, 255] // [0, 255, 0, 128] // [0, 0, 255, 255] // [0, 0, 255, 128] // [0, 0, 255, 0] /*on parse error, fallback on black[0, 0, 0, 255]*/
rgba.obj(data)
Convert as {r, g, b, a}
object
const rgba = // {r:255, g:0, b:0, a:255}rgba // {r:255, g:0, b:0, a:255}rgba // {r:255, g:0, b:0, a:102}rgba // {r:0, g:255, b:0, a:255}rgba // {r:0, g:255, b:0, a:128}rgba // {r:255, g:0, b:0, a:255}rgba // {r:0, g:255, b:0, a:128}rgba // {r:0, g:0, b:255, a:255}rgba // {r:0, g:0, b:255, a:128}rgba // {r:0, g:0, b:255, a:0}rgba /*on parse error, fallback on black{r:0, g:0, b:0, a:255}*/rgbargbargbargbargba
rgba.css(data)
Convert as rgb(r, g, b)
or rgba(r, g, b, a)
string
const rgba = // 'rgb(255, 0, 0)'rgba // 'rgb(255, 0, 0)'rgba // 'rgba(255, 0, 0, 0.4)'rgba // 'rgb(0, 255, 0)'rgba // 'rgba(0, 255, 0, .5019607843137255)'rgba // 'rgb(255, 0, 0)'rgba // 'rgba(0, 255, 0, .5019607843137255)'rgba // 'rgba(0, 0, 255, 0)'rgba // 'rgb(0, 0, 255)'rgba // 'rgba(0, 0, 255, .5)'rgba // 'rgba(0, 0, 255, 0)''rgba /*on parse error, fallback on black'rgb(0, 0, 0)'*/rgbargbargbargbargba
rgba.hex(data)
Convert as #rgb
or #rgba
or #rrggbb
or #rrggbbaa
string
const rgba = // '#f00'rgba // '#f00'rgba // '#f006'rgba // '#0f0'rgba // '#00ff0080'rgba // '#f00'rgba // '#00ff0080'rgba // '#00f0'rgba // '#00f'rgba // '#0000ff80'rgba // '#00f0'rgba /*on parse error, fallback on black'#000'*/rgbargbargbargbargba
rgba.num(data)
Convert as 4294967295 ≥ integer
≥ 0 number
const rgba = // 4294901760rgba // 4294901760rgba // 1727987712rgba // 4278255360rgba // 2147548928rgba // 4294901760rgba // 2147548928rgba // 255rgba // 4278190335rgba // 2147483903rgba // 255rgba /*on parse error, fallback on black4278190080*/rgbargbargbargbargba
License
MIT