notes/node_modules/onecolor/lib/HSL.js

30 lines
941 B
JavaScript

module.exports = function HSL(color) {
color.use(require('./HSV'));
color.installColorSpace('HSL', ['hue', 'saturation', 'lightness', 'alpha'], {
hsv: function () {
// Algorithm adapted from http://wiki.secondlife.com/wiki/Color_conversion_scripts
var l = this._lightness * 2,
s = this._saturation * ((l <= 1) ? l : 2 - l),
saturation;
// Avoid division by zero when l + s is very small (approaching black):
if (l + s < 1e-9) {
saturation = 0;
} else {
saturation = (2 * s) / (l + s);
}
return new color.HSV(this._hue, saturation, (l + s) / 2, this._alpha);
},
rgb: function () {
return this.hsv().rgb();
},
fromRgb: function () { // Becomes one.color.RGB.prototype.hsv
return this.hsv().hsl();
}
});
};