diff --git a/lib/less/functions.js b/lib/less/functions.js index 06361aa3..84e16588 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -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]); }, diff --git a/lib/less/tree/color.js b/lib/less/tree/color.js index 5e20a71d..f2d65c31 100644 --- a/lib/less/tree/color.js +++ b/lib/less/tree/color.js @@ -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) { diff --git a/test/css/functions.css b/test/css/functions.css index 3268d780..0acd6549 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -52,6 +52,9 @@ hue: 98; saturation: 12%; lightness: 95%; + hsvhue: 98; + hsvsaturation: 12%; + hsvvalue: 95%; red: 255; green: 255; blue: 255; diff --git a/test/less/functions.less b/test/less/functions.less index 9e509e9e..00d21ff6 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -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);