Merge pull request #1147 from Synchro/hsv

HSV support for #1143
This commit is contained in:
Marcus Bointon
2013-01-23 00:17:04 -08:00
4 changed files with 44 additions and 0 deletions

View File

@@ -71,6 +71,15 @@ tree.functions = {
lightness: function (color) {
return new(tree.Dimension)(Math.round(color.toHSL().l * 100), '%');
},
hsvhue: function(color) {
return new(tree.Dimension)(Math.round(color.toHSV().h));
},
hsvsaturation: function (color) {
return new(tree.Dimension)(Math.round(color.toHSV().s * 100), '%');
},
hsvvalue: function (color) {
return new(tree.Dimension)(Math.round(color.toHSV().v * 100), '%');
},
red: function (color) {
return new(tree.Dimension)(color.rgb[0]);
},

View File

@@ -101,6 +101,35 @@ tree.Color.prototype = {
}
return { h: h * 360, s: s, l: l, a: a };
},
//Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
toHSV: function () {
var r = this.rgb[0] / 255,
g = this.rgb[1] / 255,
b = this.rgb[2] / 255,
a = this.alpha;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
if (max === 0) {
s = 0;
} else {
s = d / max;
}
if (max === min) {
h = 0;
} else {
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return { h: h * 360, s: s, v: v, a: a };
},
toARGB: function () {
var argb = [Math.round(this.alpha * 255)].concat(this.rgb);
return '#' + argb.map(function (i) {

View File

@@ -52,6 +52,9 @@
hue: 98;
saturation: 12%;
lightness: 95%;
hsvhue: 98;
hsvsaturation: 12%;
hsvvalue: 95%;
red: 255;
green: 255;
blue: 255;

View File

@@ -58,6 +58,9 @@
hue: hue(hsl(98, 12%, 95%));
saturation: saturation(hsl(98, 12%, 95%));
lightness: lightness(hsl(98, 12%, 95%));
hsvhue: hsvhue(hsv(98, 12%, 95%));
hsvsaturation: hsvsaturation(hsv(98, 12%, 95%));
hsvvalue: hsvvalue(hsv(98, 12%, 95%));
red: red(#f00);
green: green(#0f0);
blue: blue(#00f);