UNPKG

color-model

Version:

Operate colors in popular color models and convert between them

109 lines (95 loc) 2.65 kB
module.exports = (function() { return Hsl; })(); var _r = require('./component'); eval('var Component = _r'); var _r = require('./rgb'); eval('var Rgb = _r'); /** * Hue, saturation, lightness color space * @extends AbstractModel * @param {Number} h * @param {Number} s * @param {Number} l */ function Hsl(h, s, l) { this._name = 'hsl'; this._components = ['hue', 'saturation', 'lightness']; this._hue = new Component('hue', 0, 360); this._hue.set(h); this._saturation = new Component('saturation', 0, 1 ); this._saturation.set(s); this._lightness = new Component('lightness', 0, 1 ); this._lightness.set(l); }; require('util').inherits(Hsl, require('./abstract-model')); 'code' ? 'completion' : Hsl.prototype = new AbstractModel; /** * @param {Number} value from 0 to 360 * @returns {Hsl} */ Hsl.prototype.hue = function (value) { return this._component('hue', arguments); }; /** * @param {Number} value from 0 to 1 * @returns {Hsl} */ Hsl.prototype.saturation = function (value) { return this._component('saturation', arguments); }; /** * @param {Number} value from 0 to 1 * @returns {Hsl} */ Hsl.prototype.lightness = function (value) { return this._component('lightness', arguments); }; /** * @returns {Xyz} */ Hsl.prototype.toXyz = function () { return this.toRgb().toXyz(); }; /** * @returns {Rgb} */ Hsl.prototype.toRgb = function () { var lightness = this._lightness.get(), saturation = this._saturation.get(); if (saturation == 0) { var light = 0; if (lightness < 0) { light = 0; } else if (lightness >= 1) { light = 255; } else { light = (lightness * (1 << 16)) >> 8; } return new Rgb(light, light, light); } var hue = this._hue.get() / this._hue._to, temp2 = (lightness < 0.5) ? (lightness * (saturation + 1)) : (lightness + saturation) - (lightness * saturation), temp1 = 2 * lightness - temp2; return new Rgb( this._calcHue(temp1, temp2, hue + 1 / 3), this._calcHue(temp1, temp2, hue), this._calcHue(temp1, temp2, hue - 1 / 3) ); }; /** * @param {Number} temp1 * @param {Number} temp2 * @param {Number} hue * @returns {Number} */ Hsl.prototype._calcHue = function (temp1, temp2, hue) { if (hue < 0) { ++hue; } else if (hue > 1) { --hue; } result = temp1; if (hue * 6 < 1) { result = temp1 + (temp2 - temp1) * hue * 6; } else if (hue * 2 < 1) { result = temp2; } else if (hue * 3 < 2) { result = temp1 + (temp2 - temp1) * (2/3 - hue) * 6; } return (result * 255.99999999999997) >> 0; };