What is hsluv?
The hsluv npm package provides a human-friendly alternative to the HSL (Hue, Saturation, Lightness) color space. It aims to make it easier to work with colors in a way that is perceptually uniform, meaning that changes in lightness or saturation appear consistent to the human eye.
What are hsluv's main functionalities?
Convert HSLuv to RGB
This feature allows you to convert HSLuv color values to RGB color values. The code sample demonstrates converting an HSLuv color with hue 0, saturation 100, and lightness 50 to its RGB equivalent.
const hsluv = require('hsluv');
const rgb = hsluv.hsluvToRgb([0, 100, 50]);
console.log(rgb); // [1, 0, 0]
Convert RGB to HSLuv
This feature allows you to convert RGB color values to HSLuv color values. The code sample demonstrates converting an RGB color with values [1, 0, 0] to its HSLuv equivalent.
const hsluv = require('hsluv');
const hsluvColor = hsluv.rgbToHsluv([1, 0, 0]);
console.log(hsluvColor); // [0, 100, 50]
Convert HSLuv to Hex
This feature allows you to convert HSLuv color values to Hex color values. The code sample demonstrates converting an HSLuv color with hue 0, saturation 100, and lightness 50 to its Hex equivalent.
const hsluv = require('hsluv');
const hex = hsluv.hsluvToHex([0, 100, 50]);
console.log(hex); // '#ff0000'
Convert Hex to HSLuv
This feature allows you to convert Hex color values to HSLuv color values. The code sample demonstrates converting a Hex color '#ff0000' to its HSLuv equivalent.
const hsluv = require('hsluv');
const hsluvColor = hsluv.hexToHsluv('#ff0000');
console.log(hsluvColor); // [0, 100, 50]
Other packages similar to hsluv
chroma-js
Chroma.js is a JavaScript library for color conversions and color scales. It supports a wide range of color spaces including RGB, HSL, and LAB. Compared to hsluv, chroma-js offers more extensive functionality for color manipulation and generation, but it does not specifically focus on the perceptual uniformity that HSLuv provides.
color
The color package is a JavaScript library for color conversion and manipulation. It supports various color models such as RGB, HSL, and CMYK. While it provides a broad range of color manipulation features, it does not focus on the perceptual uniformity that HSLuv aims to achieve.
tinycolor2
TinyColor is a small color manipulation and conversion library. It supports multiple color formats including RGB, HSL, and Hex. TinyColor is lightweight and easy to use, but it does not offer the perceptual uniformity features that HSLuv provides.
HSLuv - Human-friendly HSL
Installation
Client-side: download the latest hsluv.min.js from the
releases page.
Once this module is loaded in the browser, you can access it via the
global window.hsluv
.
Server-side: npm install hsluv
.
Usage
Most functions take tuples, ie: Javascript arrays with three elements. Make sure you are using
func([a,b,c])
and not func(a,b,c)
. If you are getting NaN for output then this is probably
the problem.
hsluvToHex([hue, saturation, lightness])
hue is a number between 0 and 360, saturation and lightness are
numbers between 0 and 100. This function returns the resulting color as
a hex string.
hsluvToRgb([hue, saturation, lightness])
Like above, but returns an array of 3 numbers between 0 and 1, for the
r, g, and b channel.
hexToHsluv(hex)
Takes a hex string and returns the HSLuv color as array that contains
the hue (0-360), saturation (0-100) and lightness (0-100) channel.
Note: The result can have rounding errors. For example saturation can
be 100.00000000000007
rgbToHsluv([red, green, blue])
Like above, but red, green and blue are passed as numbers between
0 and 1.
Use hpluvToHex, hpluvToRgb, hexToHpluv and rgbToHpluv for
the pastel variant (HPLuv). Note that HPLuv does not contain all the colors
of RGB, so converting arbitrary RGB to it may generate invalid HPLuv colors.
Also available for Stylus. See
here.