What is color-string?
The color-string npm package is a library for parsing and generating CSS color strings. It allows users to work with various color formats, convert between them, and manipulate color values.
What are color-string's main functionalities?
Parsing Color Strings
This feature allows you to parse CSS color strings into an array of RGBA values.
const colorString = require('color-string');
const color = colorString.get.rgb('rgba(255, 204, 0, 0.7)');
console.log(color); // [255, 204, 0, 0.7]
Generating Color Strings
This feature enables you to generate CSS color strings from RGB(A) or HSL(A) values.
const colorString = require('color-string');
const rgbString = colorString.to.rgb([255, 204, 0]);
console.log(rgbString); // 'rgb(255, 204, 0)'
Converting Color Models
This feature allows you to convert color values between different color models, such as RGB to HSL.
const colorString = require('color-string');
const hslString = colorString.to.hsl([255, 204, 0]);
console.log(hslString); // 'hsl(45, 100%, 50%)'
Other packages similar to color-string
color
The 'color' package is a powerful library for color conversion and manipulation. It supports chaining and has more manipulation functions compared to color-string.
chroma-js
Chroma.js is a comprehensive library for all kinds of color conversions and color scales. It offers more advanced features like color scales, interpolation, and blending compared to color-string.
tinycolor2
TinyColor is a fast, small color manipulation and conversion library. It provides additional functionalities like color readability and random color generation, which are not present in color-string.
color-string
library for parsing and generating CSS color strings.
Install
With npm:
$ npm install color-string
Usage
Parsing
colorString.get('#FFF')
colorString.get('#FFFA')
colorString.get('#FFFFFFAA')
colorString.get('hsl(360, 100%, 50%)')
colorString.get('hsl(360 100% 50%)')
colorString.get('hwb(60, 3%, 60%)')
colorString.get.rgb('#FFF')
colorString.get.rgb('blue')
colorString.get.rgb('rgba(200, 60, 60, 0.3)')
colorString.get.rgb('rgba(200 60 60 / 0.3)')
colorString.get.rgb('rgba(200 60 60 / 30%)')
colorString.get.rgb('rgb(200, 200, 200)')
colorString.get.rgb('rgb(200 200 200)')
colorString.get.hsl('hsl(360, 100%, 50%)')
colorString.get.hsl('hsl(360 100% 50%)')
colorString.get.hsl('hsla(360, 60%, 50%, 0.4)')
colorString.get.hsl('hsl(360 60% 50% / 0.4)')
colorString.get.hwb('hwb(60, 3%, 60%)')
colorString.get.hwb('hwb(60, 3%, 60%, 0.6)')
colorString.get.rgb('invalid color string')
Generation
colorString.to.hex([255, 255, 255])
colorString.to.hex([0, 0, 255, 0.4])
colorString.to.hex([0, 0, 255], 0.4)
colorString.to.rgb([255, 255, 255])
colorString.to.rgb([0, 0, 255, 0.4])
colorString.to.rgb([0, 0, 255], 0.4)
colorString.to.rgb.percent([0, 0, 255])
colorString.to.keyword([255, 255, 0])
colorString.to.hsl([360, 100, 100])
colorString.to.hwb([50, 3, 15])
colorString.to.rgb(0, [0, 255], 0.4)
colorString.to.rgb([0, 0], [255], 0.4)
colorString.to.rgb([0], 0, [255, 0.4])