2.0.0 • Published 4 years ago

css-color-converter v2.0.0

Weekly downloads
15,679
License
MIT
Repository
github
Last release
4 years ago

css-color-converter Build Status

Converts CSS colors from one representation to another

Installation

npm install css-color-converter

Usage

Example usage:

import { fromString } from 'css-color-converter';

fromString('rgb(255, 255, 255)').toHslString(); // hsl(0, 0%, 100%)
fromString('rgba(255, 255, 255, 0.5)').toHslString(); // hsla(0, 0%, 100%, 0.5)
fromString('blue').toRgbString(); // rgb(0, 0, 255)
fromString('red').toHexString(); // #ff0000

Functions

fromString(str)

parametertypedescription
strstringSupports named colors, hex, rgb/rgba, hsl/hsla

Returns instance if valid, null if invalid.

fromRgb([r, g, b])

parametertypedescription
rintred (0-255)
gintgreen (0-255)
bintblue (0-255)

Returns instance

fromRgba([r, g, b, a])

parametertypedescription
rintred (0-255)
gintgreen (0-255)
bintblue (0-255)
afloatalpha (0-1)

Returns instance

fromHsl([h, s, l])

parametertypedescription
hinthue (0-360)
sintsaturation (0-100)
lintluminosity (0-100)

Returns instance

fromHsla([h, s, l, a])

parametertypedescription
hinthue (0-360)
sintsaturation (0-100)
lintluminosity (0-100)
afloatalpha (0-1)

Returns instance

Methods

toRgbString()

Returns rgb() or rgba(), depending on the alpha.

toHslString()

Returns hsl() or hsla(), depending on the alpha.

toHexString()

Returns 6-digit or 8-digit hex, depending on the alpha.

toRgbaArray()

Returns [r, g, b, a] array.