color vs polished vs tinycolor2 vs chroma-js
Color Manipulation Libraries Comparison
3 Years
colorpolishedtinycolor2chroma-jsSimilar Packages:
What's Color Manipulation Libraries?

Color manipulation libraries in JavaScript provide developers with tools to create, modify, and manipulate colors programmatically. These libraries offer a wide range of functionalities, including color conversion (e.g., RGB to HEX), color blending, generating color palettes, and performing color calculations (e.g., finding complementary colors). They are particularly useful in web development for tasks such as dynamic styling, creating design tools, implementing color pickers, and ensuring accessibility by calculating color contrast. By leveraging these libraries, developers can handle colors more efficiently and creatively in their applications. chroma-js is a powerful and versatile color manipulation library that excels in color interpolation, blending, and generating color scales. It supports various color formats and provides a rich set of functions for advanced color calculations. color is a simple and lightweight color manipulation library that focuses on providing a clean API for color conversion, manipulation, and blending. It supports multiple color models and is designed for ease of use, making it a great choice for projects that require straightforward color handling. polished is a functional CSS toolkit that provides a collection of utility functions for color manipulation, typography, spacing, and more. It is designed to work seamlessly with styled-components and other CSS-in-JS libraries, offering a modular approach to styling that promotes reusability and consistency. tinycolor2 is a fast and lightweight color manipulation library that offers a wide range of features for color conversion, manipulation, and analysis. It supports various color formats, provides functions for calculating color contrast, and includes utilities for generating color palettes, making it a versatile tool for both design and development.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
color31,146,480
4,87526.3 kB186 months agoMIT
polished7,512,846
7,6582.8 MB282 years agoMIT
tinycolor26,096,811
5,185285 kB1033 years agoMIT
chroma-js1,133,204
10,433396 kB6510 months ago(BSD-3-Clause AND Apache-2.0)
Feature Comparison: color vs polished vs tinycolor2 vs chroma-js

Color Manipulation

  • color:

    color offers basic color manipulation features such as blending, darkening, lightening, and adjusting color opacity. It supports multiple color models and provides a simple API for performing common color manipulations.

  • polished:

    polished provides a range of color manipulation utilities, including functions for darkening, lightening, mixing colors, and generating color contrasts. It is designed to work seamlessly with CSS-in-JS and promotes reusable styling functions.

  • tinycolor2:

    tinycolor2 offers a wide range of color manipulation features, including blending, darkening, lightening, and adjusting transparency. It also provides utilities for color contrast analysis and generating color palettes, making it versatile for both design and development.

  • chroma-js:

    chroma-js provides advanced color manipulation capabilities, including interpolation, blending, and creating color scales. It supports multiple color models (RGB, HSL, LAB) and allows for complex manipulations, making it suitable for data visualization and design applications.

Color Conversion

  • color:

    color provides straightforward color conversion between various formats, including RGB, HEX, HSL, and CMYK. Its simple API makes it easy to convert colors as needed without any complex setup.

  • polished:

    polished includes color conversion utilities as part of its toolkit, allowing for easy conversion between different color formats. However, its primary focus is on providing a wide range of styling utilities rather than being a dedicated color conversion library.

  • tinycolor2:

    tinycolor2 excels at color conversion, supporting multiple formats such as RGB, HEX, HSL, and more. It provides fast and accurate conversions, making it reliable for applications that require quick color format changes.

  • chroma-js:

    chroma-js supports extensive color conversion between different color models (RGB, HSL, LAB, HEX) and allows for smooth transitions between colors. It is particularly powerful for applications that require precise color calculations and conversions.

Performance

  • color:

    color is lightweight and performs well for most color manipulation tasks. Its simplicity and small size make it a good choice for projects where performance is a priority but advanced features are not required.

  • polished:

    polished is designed to be efficient, but its performance depends on how the utility functions are used. Since it is a CSS-in-JS toolkit, performance may vary based on the implementation and usage patterns in the styling code.

  • tinycolor2:

    tinycolor2 is known for its fast performance, particularly in color manipulation and conversion tasks. It is optimized for speed, making it ideal for applications that require quick color calculations and real-time interactions.

  • chroma-js:

    chroma-js is optimized for performance, especially when handling large color datasets or performing complex calculations. Its efficient algorithms make it suitable for real-time applications and data visualization tools.

Ease of Use: Code Examples

  • color:

    color Example

    import Color from 'color';
    
    // Color conversion
    const hexColor = Color('#ff0000');
    const rgbColor = hexColor.rgb().string();
    const hslColor = hexColor.hsl().string();
    console.log(rgbColor); // Outputs: rgb(255, 0, 0)
    console.log(hslColor); // Outputs: hsl(0, 100%, 50%)
    
    // Color manipulation
    const lightenedColor = hexColor.lighten(0.2).hex();
    const darkenedColor = hexColor.darken(0.2).hex();
    console.log(lightenedColor); // Outputs a lighter shade of red
    console.log(darkenedColor); // Outputs a darker shade of red
    
  • polished:

    polished Example

    import { darken, lighten, mix } from 'polished';
    
    // Color manipulation
    const darkenedColor = darken(0.2, '#ff0000');
    const lightenedColor = lighten(0.2, '#ff0000');
    const mixedColor = mix(0.5, '#ff0000', '#0000ff');
    
    console.log(darkenedColor); // Outputs a darker shade of red
    console.log(lightenedColor); // Outputs a lighter shade of red
    console.log(mixedColor); // Outputs a color that is a mix of red and blue
    
  • tinycolor2:

    tinycolor2 Example

    import tinycolor from 'tinycolor2';
    
    // Color manipulation
    const color = tinycolor('#ff0000');
    const lightened = color.lighten(20).toString();
    const darkened = color.darken(20).toString();
    const blended = color.mix('#0000ff', 0.5).toString();
    
    console.log(lightened); // Outputs a lighter shade of red
    console.log(darkened); // Outputs a darker shade of red
    console.log(blended); // Outputs a color that is a blend of red and blue
    
  • chroma-js:

    chroma-js Example

    import chroma from 'chroma-js';
    
    // Color interpolation
    const color1 = '#ff0000';
    const color2 = '#0000ff';
    const interpolatedColor = chroma.interpolate(color1, color2)(0.5);
    console.log(interpolatedColor); // Outputs a color between red and blue
    
    // Creating a color scale
    const scale = chroma.scale(['#f00', '#0f0', '#00f']).mode('lab');
    const colorScale = scale.colors(5);
    console.log(colorScale); // Outputs an array of colors in the scale
    
How to Choose: color vs polished vs tinycolor2 vs chroma-js
  • color:

    Choose color if you want a simple and lightweight library for color conversion and manipulation. It is perfect for projects that need a straightforward API without the overhead of additional features.

  • polished:

    Choose polished if you are working with CSS-in-JS and need a comprehensive toolkit for color manipulation and other styling utilities. It is especially useful for projects that prioritize modularity and reusability in their styling approach.

  • tinycolor2:

    Choose tinycolor2 if you need a fast and efficient library for color manipulation with a focus on performance. It is great for applications that require quick color calculations, contrast analysis, and palette generation.

  • chroma-js:

    Choose chroma-js if you need advanced color manipulation features, such as interpolation, blending, and generating color scales. It is ideal for data visualization, design tools, and applications that require precise color calculations.

README for color

color

JavaScript library for immutable color conversion and manipulation with support for CSS color strings.

const color = Color('#7743CE').alpha(0.5).lighten(0.5);
console.log(color.hsl().string());  // 'hsla(262, 59%, 81%, 0.5)'

console.log(color.cmyk().round().array());  // [ 16, 25, 0, 8, 0.5 ]

console.log(color.ansi256().object());  // { ansi256: 183, alpha: 0.5 }

Install

npm install color

Usage

import Color from 'color';

Constructors

// string constructor
const color = Color('rgb(255, 255, 255)')                       // { model: 'rgb', color: [ 255, 255, 255 ], valpha: 1 }
const color = Color('hsl(194, 53%, 79%)')                       // { model: 'hsl', color: [ 195, 53, 79 ], valpha: 1 }
const color = Color('hsl(194, 53%, 79%, 0.5)')                  // { model: 'hsl', color: [ 195, 53, 79 ], valpha: 0.5 }
const color = Color('#FF0000')                                  // { model: 'rgb', color: [ 255, 0, 0 ], valpha: 1 }
const color = Color('#FF000033')                                // { model: 'rgb', color: [ 255, 0, 0 ], valpha: 0.2 }
const color = Color('lightblue')                                // { model: 'rgb', color: [ 173, 216, 230 ], valpha: 1 }
const color = Color('purple')                                   // { model: 'rgb', color: [ 128, 0, 128 ], valpha: 1 }

// rgb
const color = Color({r: 255, g: 255, b: 255})                   // { model: 'rgb', color: [ 255, 255, 255 ], valpha: 1 }
const color = Color({r: 255, g: 255, b: 255, alpha: 0.5})       // { model: 'rgb', color: [ 255, 255, 255 ], valpha: 0.5 }
const color = Color.rgb(255, 255, 255)                          // { model: 'rgb', color: [ 255, 255, 255 ], valpha: 1 }
const color = Color.rgb(255, 255, 255, 0.5)                     // { model: 'rgb', color: [ 255, 255, 255 ], valpha: 0.5 }
const color = Color.rgb(0xFF, 0x00, 0x00, 0.5)                  // { model: 'rgb', color: [ 255, 0, 0 ], valpha: 0.5 }
const color = Color.rgb([255, 255, 255])                        // { model: 'rgb', color: [ 255, 255, 255 ], valpha: 1 }
const color = Color.rgb([0xFF, 0x00, 0x00, 0.5])                // { model: 'rgb', color: [ 255, 0, 0 ], valpha: 0.5 }

// hsl
const color = Color({h: 194, s: 53, l: 79})                     // { model: 'hsl', color: [ 195, 53, 79 ], valpha: 1 }
const color = Color({h: 194, s: 53, l: 79, alpha: 0.5})         // { model: 'hsl', color: [ 195, 53, 79 ], valpha: 0.5 }
const color = Color.hsl(194, 53, 79)                            // { model: 'hsl', color: [ 195, 53, 79 ], valpha: 1 }

// hsv
const color = Color({h: 195, s: 25, v: 99})                     // { model: 'hsv', color: [ 195, 25, 99 ], valpha: 1 }
const color = Color({h: 195, s: 25, v: 99, alpha: 0.5})         // { model: 'hsv', color: [ 195, 25, 99 ], valpha: 0.5 }
const color = Color.hsv(195, 25, 99)                            // { model: 'hsv', color: [ 195, 25, 99 ], valpha: 1 }
const color = Color.hsv([195, 25, 99])                          // { model: 'hsv', color: [ 195, 25, 99 ], valpha: 1 }

// cmyk
const color = Color({c: 0, m: 100, y: 100, k: 0})               // { model: 'cmyk', color: [ 0, 100, 100, 0 ], valpha: 1 }
const color = Color({c: 0, m: 100, y: 100, k: 0, alpha: 0.5})   // { model: 'cmyk', color: [ 0, 100, 100, 0 ], valpha: 0.5 }
const color = Color.cmyk(0, 100, 100, 0)                        // { model: 'cmyk', color: [ 0, 100, 100, 0 ], valpha: 1 }
const color = Color.cmyk(0, 100, 100, 0, 0.5)                   // { model: 'cmyk', color: [ 0, 100, 100, 0 ], valpha: 0.5 }

// hwb
const color = Color({h: 180, w: 0, b: 0})                       // { model: 'hwb', color: [ 180, 0, 0 ], valpha: 1 }
const color = Color.hwb(180, 0, 0)                              // { model: 'hwb', color: [ 180, 0, 0 ], valpha: 1 }

// lch
const color = Color({l: 53, c: 105, h: 40})                     // { model: 'lch', color: [ 53, 105, 40 ], valpha: 1 }
const color = Color.lch(53, 105, 40)                            // { model: 'lch', color: [ 53, 105, 40 ], valpha: 1 }

// lab
const color = Color({l: 53, a: 80, b: 67})                      // { model: 'lab', color: [ 53, 80, 67 ], valpha: 1 }
const color = Color.lab(53, 80, 67)                             // { model: 'lab', color: [ 53, 80, 67 ], valpha: 1 }

// hcg
const color = Color({h: 0, c: 100, g: 0})                       // { model: 'hcg', color: [ 0, 100, 0 ], valpha: 1 }
const color = Color.hcg(0, 100, 0)                              // { model: 'hcg', color: [ 0, 100, 0 ], valpha: 1 }

// ansi16
const color = Color.ansi16(91)                                  // { model: 'ansi16', color: [ 91 ], valpha: 1 }
const color = Color.ansi16(91, 0.5)                             // { model: 'ansi16', color: [ 91 ], valpha: 0.5 }

// ansi256
const color = Color.ansi256(196)                                // { model: 'ansi256', color: [ 196 ], valpha: 1 }
const color = Color.ansi256(196, 0.5)                           // { model: 'ansi256', color: [ 196 ], valpha: 0.5 }

// apple
const color = Color.apple(65535, 65535, 65535)                  // { model: 'apple', color: [ 65535, 65535, 65535 ], valpha: 1 }
const color = Color.apple([65535, 65535, 65535])                // { model: 'apple', color: [ 65535, 65535, 65535 ], valpha: 1 }


Set the values for individual channels with alpha, red, green, blue, hue, saturationl (hsl), saturationv (hsv), lightness, whiteness, blackness, cyan, magenta, yellow, black

String constructors are handled by color-string

Getters

color.hsl()

Convert a color to a different space (hsl(), cmyk(), etc.).

color.object() // {r: 255, g: 255, b: 255}

Get a hash of the color value. Reflects the color's current model (see above).

color.rgb().array() // [255, 255, 255]

Get an array of the values with array(). Reflects the color's current model (see above).

color.rgbNumber() // 16777215 (0xffffff)

Get the rgb number value.

color.hex() // #ffffff

Get the hex value. (NOTE: .hex() does not return alpha values; use .hexa() for an RGBA representation)

color.red() // 255

Get the value for an individual channel.

CSS Strings

color.hsl().string() // 'hsl(320, 50%, 100%)'

Calling .string() with a number rounds the numbers to that decimal place. It defaults to 1.

Luminosity

color.luminosity(); // 0.412

The WCAG luminosity of the color. 0 is black, 1 is white.

color.contrast(Color("blue")) // 12

The WCAG contrast ratio to another color, from 1 (same color) to 21 (contrast b/w white and black).

color.isLight()  // true
color.isDark()   // false

Get whether the color is "light" or "dark", useful for deciding text color.

Manipulation

color.negate()         // rgb(0, 100, 255) -> rgb(255, 155, 0)

color.lighten(0.5)     // hsl(100, 50%, 50%) -> hsl(100, 50%, 75%)
color.lighten(0.5)     // hsl(100, 50%, 0)   -> hsl(100, 50%, 0)
color.darken(0.5)      // hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)
color.darken(0.5)      // hsl(100, 50%, 0)   -> hsl(100, 50%, 0)

color.lightness(50)    // hsl(100, 50%, 10%) -> hsl(100, 50%, 50%)

color.saturate(0.5)    // hsl(100, 50%, 50%) -> hsl(100, 75%, 50%)
color.desaturate(0.5)  // hsl(100, 50%, 50%) -> hsl(100, 25%, 50%)
color.grayscale()      // #5CBF54 -> #969696

color.whiten(0.5)      // hwb(100, 50%, 50%) -> hwb(100, 75%, 50%)
color.blacken(0.5)     // hwb(100, 50%, 50%) -> hwb(100, 50%, 75%)

color.fade(0.5)        // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
color.opaquer(0.5)     // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 1.0)

color.rotate(180)      // hsl(60, 20%, 20%) -> hsl(240, 20%, 20%)
color.rotate(-90)      // hsl(60, 20%, 20%) -> hsl(330, 20%, 20%)

color.mix(Color("yellow"))        // cyan -> rgb(128, 255, 128)
color.mix(Color("yellow"), 0.3)   // cyan -> rgb(77, 255, 179)

// chaining
color.green(100).grayscale().lighten(0.6)

Propers

The API was inspired by color-js. Manipulation functions by CSS tools like Sass, LESS, and Stylus.