What is css-color-function?
The css-color-function npm package allows you to perform color manipulations using CSS-like syntax. It is useful for dynamically adjusting colors in your stylesheets, such as lightening, darkening, saturating, or desaturating colors.
What are css-color-function's main functionalities?
Lighten a color
This feature allows you to lighten a given color by a specified percentage. In this example, the color red is lightened by 10%.
const colorFunction = require('css-color-function');
const color = 'color(red lightness(+ 10%))';
const result = colorFunction.convert(color);
console.log(result); // Outputs: #ff3333
Darken a color
This feature allows you to darken a given color by a specified percentage. In this example, the color blue is darkened by 20%.
const colorFunction = require('css-color-function');
const color = 'color(blue lightness(- 20%))';
const result = colorFunction.convert(color);
console.log(result); // Outputs: #0000cc
Saturate a color
This feature allows you to increase the saturation of a given color by a specified percentage. In this example, the color green is saturated by 30%.
const colorFunction = require('css-color-function');
const color = 'color(green saturation(+ 30%))';
const result = colorFunction.convert(color);
console.log(result); // Outputs: #00ff00
Desaturate a color
This feature allows you to decrease the saturation of a given color by a specified percentage. In this example, the color purple is desaturated by 40%.
const colorFunction = require('css-color-function');
const color = 'color(purple saturation(- 40%))';
const result = colorFunction.convert(color);
console.log(result); // Outputs: #993399
Other packages similar to css-color-function
color
The 'color' npm package provides a comprehensive API for color manipulation and conversion. It supports a wide range of color models and offers methods for adjusting lightness, saturation, and other properties. Compared to css-color-function, 'color' offers a more extensive set of features and greater flexibility.
tinycolor2
The 'tinycolor2' npm package is a fast and lightweight color manipulation library. It supports various color formats and provides methods for adjusting colors, such as lightening, darkening, and mixing. While it offers similar functionalities to css-color-function, 'tinycolor2' is known for its performance and ease of use.
chroma-js
The 'chroma-js' npm package is a powerful library for color conversions and color scales. It supports a wide range of color spaces and provides advanced color manipulation methods. Compared to css-color-function, 'chroma-js' offers more advanced features and is suitable for complex color manipulations and visualizations.
css-color-function
A parser and converter for Tab Atkins's proposed color function in CSS.
Installation
$ npm install css-color-function
Example
var color = require('css-color-function');
color.convert('color(red tint(50%))');
color.parse('color(red blue(+ 30))');
API
color.convert(string)
Convert a color function CSS string
into an RGB color string.
color.parse(string)
Parse a color function CSS string
and return an AST.
License
The MIT License (MIT)
Copyright (c) 2013 Ian Storm Taylor <ian@segment.io>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.