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

Color manipulation libraries are essential tools in web development that provide developers with the ability to create, manipulate, and convert colors in various formats. These libraries simplify the process of working with colors, allowing for operations such as color mixing, brightness adjustment, and conversion between color spaces (e.g., RGB, HEX, HSL). They are particularly useful in design systems, theming, and ensuring consistent color usage across applications. By leveraging these libraries, developers can enhance the visual appeal of their applications while maintaining accessibility and usability standards.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
color24,771,8394,81826.3 kB1418 days agoMIT
polished8,121,1887,6432.8 MB28a year agoMIT
tinycolor25,800,3385,138285 kB1032 years agoMIT
chroma-js966,99210,291396 kB655 months ago(BSD-3-Clause AND Apache-2.0)
Feature Comparison: color vs polished vs tinycolor2 vs chroma-js

Color Manipulation Capabilities

  • color:

    Color is a straightforward library that focuses on basic color manipulation tasks like conversion between color formats (HEX, RGB, HSL) and simple operations such as darkening or lightening colors. It is easy to use for simple applications without the need for advanced features.

  • polished:

    Polished is built specifically for styling in React applications. It offers a set of utility functions for color manipulation, including darken, lighten, and opacity adjustments, while also providing other CSS-related functions. It integrates seamlessly with styled-components and emotion, making it ideal for React developers.

  • tinycolor2:

    TinyColor2 is a lightweight library that provides basic color manipulation features, including color conversion, brightness adjustment, and color blending. It is known for its simplicity and performance, making it a good choice for projects where size and speed are critical.

  • chroma-js:

    Chroma-js excels in providing advanced color manipulation features, including color interpolation, scales, and conversions between various color formats. It supports a wide range of color spaces such as RGB, HSL, LAB, and more, making it suitable for complex color calculations.

Performance

  • color:

    Color is lightweight and performs well for basic operations, but it may not be as optimized for more complex tasks compared to other libraries. It is best suited for projects that do not require extensive color manipulation.

  • polished:

    Polished is designed for use in React applications and is optimized for performance when used with styled-components. It leverages the power of CSS-in-JS, ensuring that styles are applied efficiently without unnecessary re-renders.

  • tinycolor2:

    TinyColor2 is known for its small footprint and fast performance, making it ideal for applications where performance is a priority. It is particularly effective for simple color manipulations without the overhead of larger libraries.

  • chroma-js:

    Chroma-js is optimized for performance, especially when dealing with large datasets or complex color operations. Its efficient algorithms ensure that color manipulations are executed quickly, which is crucial for applications requiring real-time updates.

Ease of Use

  • color:

    Color is user-friendly and straightforward, making it easy for beginners to get started with basic color manipulations without much overhead.

  • polished:

    Polished is designed with React developers in mind, offering a familiar API for those already using styled-components. Its utility functions are intuitive and easy to integrate into existing projects.

  • tinycolor2:

    TinyColor2 is very easy to use, with a simple API that allows developers to perform color manipulations quickly. Its straightforward approach makes it accessible for developers of all skill levels.

  • chroma-js:

    Chroma-js has a steeper learning curve due to its extensive feature set and flexibility. However, once mastered, it provides powerful tools for advanced color manipulation.

Community and Support

  • color:

    Color has a smaller community compared to others, but it is still actively maintained and has sufficient documentation to assist users in common tasks.

  • polished:

    Polished benefits from the React ecosystem, with a robust community and extensive documentation. It is well-supported within the styled-components community, making it a reliable choice for React projects.

  • tinycolor2:

    TinyColor2 has a good community and is widely used in various projects. It is well-documented, and many developers share tips and tricks for using it effectively.

  • chroma-js:

    Chroma-js has a strong community and is widely used in data visualization projects, which means you can find plenty of resources, examples, and support online.

Integration

  • color:

    Color is also easily integrable into any JavaScript project, but it is particularly useful for simpler applications where advanced features are not required.

  • polished:

    Polished is specifically designed for use with React and CSS-in-JS libraries like styled-components and emotion, making it a great choice for React developers looking for seamless integration.

  • tinycolor2:

    TinyColor2 can be used in any JavaScript environment, making it flexible for various projects. It is particularly useful in scenarios where lightweight color manipulation is needed.

  • chroma-js:

    Chroma-js can be integrated into any JavaScript project, making it versatile for various applications, including data visualization and design tools.

How to Choose: color vs polished vs tinycolor2 vs chroma-js
  • chroma-js:

    Choose chroma-js if you need a powerful and flexible color manipulation library that supports a wide range of color spaces and provides advanced features like color scales and interpolation. It is ideal for data visualization and complex color operations.

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.