Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

css-color-converter

andyjansson176.6kMIT2.0.0

Converts CSS colors from one representation to another

css, color, conversions, converter

readme

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)

parameter type description
str string Supports named colors, hex, rgb/rgba, hsl/hsla

Returns instance if valid, null if invalid.

fromRgb([r, g, b])

parameter type description
r int red (0-255)
g int green (0-255)
b int blue (0-255)

Returns instance

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

parameter type description
r int red (0-255)
g int green (0-255)
b int blue (0-255)
a float alpha (0-1)

Returns instance

fromHsl([h, s, l])

parameter type description
h int hue (0-360)
s int saturation (0-100)
l int luminosity (0-100)

Returns instance

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

parameter type description
h int hue (0-360)
s int saturation (0-100)
l int luminosity (0-100)
a float alpha (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.

changelog

2.0.0 - 2020-09-24

  • Rewrite as ES module. Some breaking changes to the API surface has been made as a result.
  • Better adhere to CSS specification (#4).
  • Add support for CSS Color Module Level 4 syntax.
  • Update dependencies.
  • Add eslint.
  • Add babel.

1.1.1 - 2020-03-20

  • Fix alpha 0 incorrectly defaulting to 1 (#2). Thanks to jedwards1211.

1.1.0 - 2016-08-13

  • Add toHslaArray method.

1.0.2 - 2015-05-27

  • Fix typo causing rgba() to return incorrect results.

1.0.1 - 2015-05-27

  • Fix toHexString() not padding values.

1.0.0 - 2015-05-27

  • Initial release.