2.2.0 • Published 4 years ago

@magedragonfrog/css-color-parser v2.2.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

@magedragonfrog/css-color-parser

Parses color values specified in CSS style properties.
Implements a subset of the CSS Color Module - Level 4 specification.

Usage

First off you need to install the package from NPM using npm install --save @magedragonfrog\css-color-parser.
Once the package is installed you can import the parseColor function from @magedragonfrog\css-color-parser.
For example:

import { parseColor } from '@magedragonfrog\css-color-parser'

const result: RGBColor = parseColor('cadetblue');

console.log(`Red component is: ${result.r}, green component is: ${result.g}, blue component is: ${result.b}`);

//Prints: Red component is: 0.37, green component is: 0.62, blue component is: 0.63

parseColor always returns an RGBColor object that has three properties: r, g, b.
Each property value is the corresponding color component value between 0 and 1.

At the moment the alpha component is not supported and is ignored.
If a color is not recognized the function returns the RGBColor representation for the color white (i.e.: all the components are 0).

Color representations supported

For now only some representations are supported.
More will be added in the future.

Limitations

  • At the moment the alpha component is not supported and is ignored.
  • Not all color representations are supported