hsl-to-rgb-for-reals
hsl-to-rgb-for-reals is a single CommonJS function that converts an HSL color into three rounded RGB byte values. Give it hue in degrees from 0 up to but not including 360, plus saturation and lightness as fractions from 0 to 1, and it returns an array such as `[93, 121, 192]`. It has no dependencies, parser, alpha-channel support, formatting helpers, types, or input validation.
A frozen but understandable converter for code that already supplies clean numeric HSL values. New projects should usually choose color-convert or write this small formula locally with explicit validation and types.
Use it if
- You already have numeric HSL components and only need an RGB byte tuple
- You maintain CommonJS code and want a dependency-free converter with a tiny public surface
- You can validate and normalize hue, saturation, and lightness before calling it
- You are preserving an existing dependency and its exact rounded output
- You need to parse CSS colors or emit hex, rgb(), hsl(), alpha, or named colors: this package only converts three numeric arguments into an RGB array
- Your data uses CSS-style saturation and lightness percentages: the README requires fractions from 0 to 1, so 44 and 56 must become 0.44 and 0.56 before conversion
- You cannot guarantee valid ranges: the source performs no checks, and hue 360 falls outside its six branches and produces NaN components
- You need active maintenance, bundled TypeScript declarations, or native ESM: version 1.1.1 and the last repository push both date to September 2019, and the package is CommonJS without types
- You want a complete color toolkit: color-convert, tinycolor2, or chroma-js cover multiple spaces, parsing, formatting, and composition rather than this one conversion
Setup reality
npm install hsl-to-rgb-for-reals is the whole installation. There are no dependencies, peer dependencies, native extensions, credentials, configuration files, or build-time generation. CommonJS code gets the converter with require('hsl-to-rgb-for-reals'). Modern bundlers normally interoperate with that export, but the package has no ESM entry point, export map, or bundled TypeScript declaration, so strict projects may need a local declaration for `(hue: number, saturation: number, lightness: number) => [number, number, number]`. The bigger first-run surprise is the input convention. Hue is degrees in the half-open range 0 through less than 360, while saturation and lightness are fractions, not the percentage numbers shown in CSS. The README example therefore passes 0.44 for 44 percent. The function does no parsing, clamping, wrapping, or validation. Exactly 360 is not treated like 0, out-of-range values can yield nonsensical channels, and an undefined hue is silently special-cased to black even though other missing arguments are not handled safely. It returns a mutable three-element array of rounded bytes and has no alpha channel or formatter. If inputs come from users, CSS, or another color library, normalize and validate them in a wrapper before conversion. With no release or push since 2019, assume these module-format and validation gaps will remain.
Patterns
Convert fractional HSL to RGBconvert-hsl
const hslToRgb = require('hsl-to-rgb-for-reals');
const rgb = hslToRgb(223, 0.44, 0.56);
console.log(rgb); // [93, 121, 192]Saturation and lightness are fractions from 0 to 1, not CSS percentage numbers.
Convert HSL components read as percentagesconvert-css-components
const hslToRgb = require('hsl-to-rgb-for-reals');
function fromPercentages(hue, saturationPct, lightnessPct) {
return hslToRgb(hue, saturationPct / 100, lightnessPct / 100);
}
console.log(fromPercentages(223, 44, 56));This converts numeric components only; it does not parse an `hsl(...)` CSS string.
Format the result as a CSS rgb() valueformat-rgb-css
const hslToRgb = require('hsl-to-rgb-for-reals');
const [r, g, b] = hslToRgb(223, 0.44, 0.56);
const css = `rgb(${r} ${g} ${b})`;
console.log(css);The package returns an array and does not supply CSS formatting or alpha support.
Format converted channels as hexadecimalformat-hex
const hslToRgb = require('hsl-to-rgb-for-reals');
function toHex(h, s, l) {
return '#' + hslToRgb(h, s, l)
.map((channel) => channel.toString(16).padStart(2, '0'))
.join('');
}
console.log(toHex(223, 0.44, 0.56)); // #5d79c0Validate inputs first; out-of-range channels do not reliably produce two-digit hex.
Wrap arbitrary hue into the supported rangewrap-hue
const hslToRgb = require('hsl-to-rgb-for-reals');
function wrapHue(degrees) {
return ((degrees % 360) + 360) % 360;
}
const rgb = hslToRgb(wrapHue(420), 0.8, 0.5);Hue 360 is outside the documented half-open range and produces NaN without wrapping.
Reject invalid components before conversionvalidate-inputs
const hslToRgb = require('hsl-to-rgb-for-reals');
function convertChecked(h, s, l) {
if (![h, s, l].every(Number.isFinite)) throw new TypeError('HSL must be finite numbers');
if (h < 0 || h >= 360 || s < 0 || s > 1 || l < 0 || l > 1) {
throw new RangeError('HSL component out of range');
}
return hslToRgb(h, s, l);
}The package itself does not validate or clamp, apart from returning black when hue is undefined.
Convert an achromatic HSL colorconvert-grayscale
const hslToRgb = require('hsl-to-rgb-for-reals');
const middleGray = hslToRgb(0, 0, 0.5);
console.log(middleGray); // [128, 128, 128]When saturation is zero, hue has no visual effect, but it must still be within the implemented range.
Convert a palette of HSL tuplesconvert-palette
const hslToRgb = require('hsl-to-rgb-for-reals');
const hslPalette = [[0, 0.8, 0.5], [120, 0.8, 0.5], [240, 0.8, 0.5]];
const rgbPalette = hslPalette.map(([h, s, l]) => hslToRgb(h, s, l));
console.log(rgbPalette);Each call allocates a new mutable array; copy or freeze results if callers share them.
Add alpha when formatting CSSadd-alpha-css
const hslToRgb = require('hsl-to-rgb-for-reals');
function toRgbCss(h, s, l, alpha = 1) {
if (alpha < 0 || alpha > 1) throw new RangeError('alpha');
const [r, g, b] = hslToRgb(h, s, l);
return `rgb(${r} ${g} ${b} / ${alpha})`;
}
console.log(toRgbCss(223, 0.44, 0.56, 0.6));Alpha is formatting added by your wrapper; the converter has no alpha-channel API.
Alternatives
| Package | Registry | Pick it when |
|---|---|---|
| color-convert | npm | You need maintained conversions among many color spaces with rounded and raw variants |
| tinycolor2 | npm | You need color-string parsing, alpha, manipulation, and output formatting |
| chroma-js | npm | You need color scales, interpolation, contrast work, or perceptual color spaces |
| hsl-to-rgb | npm | You must match the older package this fork copied, after checking its export behavior in your toolchain |