-
Notifications
You must be signed in to change notification settings - Fork 40
/
HSL.js
29 lines (24 loc) · 941 Bytes
/
HSL.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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();
}
});
};