Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
The colord npm package is a tiny yet powerful tool for color conversions and manipulations. It provides functions for parsing colors, converting them between different color models, and manipulating their properties such as hue, saturation, lightness, and alpha channel.
Parsing and converting colors
This feature allows you to parse color strings and convert them to different color models. The example shows how to parse a hex color and convert it to an RGB object.
const { colord, extend } = require('colord');
const c = colord('#ff5733');
console.log(c.toRgb()); // { r: 255, g: 87, b: 51, a: 1 }
Manipulating color properties
This feature enables you to manipulate color properties such as alpha channel. The example demonstrates setting the alpha of an RGBA color and converting it to a string.
const { colord } = require('colord');
const color = colord('rgba(255, 87, 51, 1)');
console.log(color.alpha(0.5).toRgbString()); // 'rgba(255, 87, 51, 0.5)'
Mixing colors
This feature allows you to mix two colors together. The example mixes a color with white at a 50% ratio and outputs the hex value of the result.
const { colord } = require('colord');
const mix = colord('#ff5733').mix('#ffffff', 0.5).toHex();
console.log(mix); // '#ffaba9'
Generating readable colors
This feature helps in checking if a color is readable on another color, which is useful for accessibility. The example checks if the color '#ff5733' is readable on a white background.
const { colord, extend } = require('colord');
extend([require('colord/plugins/a11y')]);
const color = colord('#ff5733');
console.log(color.isReadable('#ffffff')); // true or false
Chroma.js is a powerful library for all kinds of color conversions and color scales. It is more feature-rich and has a larger footprint compared to colord, which is more lightweight.
TinyColor is a small, fast library for color manipulation and conversion in JavaScript. It offers similar functionalities to colord but with a different API design and slightly larger size.
The 'color' package is another tool for color conversion and manipulation. It provides a chainable API and is more established but is larger in size compared to colord.
Name | ops/sec (millions) | Size | Size (gzip) | Dependencies |
---|---|---|---|---|
colord | 1 692 690 | |||
tinycolor2 | 998 946 | |||
color | 736 610 |
Performance results were generated on a MBP 2019, 2,6 GHz Intel Core i7. To perform these tests, execute npm run benchmark
in the library folder.
rgb(256, -1, 999, 2)
)brightness
, isDark
, isLight
alpha
lighten
, darken
invert
FAQs
👑 A tiny yet powerful tool for high-performance color manipulations and conversions
The npm package colord receives a total of 7,914,458 weekly downloads. As such, colord popularity was classified as popular.
We found that colord demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.