Skip to content

Native HCG support implementation #2921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions dist/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,35 @@ colorFunctions = {
a);
},

hcg: function(h, c, gr) {
return colorFunctions.hcga(h, c, gr, 1.0);
},

hcga: function(h, c, gr, a) {
h = (number(h) % 360) / 360 * 6;
c = number(c); gr = number(gr);

if (c <= 0) {
return [gr * 255, gr * 255, gr * 255];
}

var i = Math.floor(h),
f = h - i,
q = c * (1 - f), t = c * f,
mod = i % 6,
r = [c, q, 0, 0, t, c][mod],
g = [t, c, c, q, 0, 0][mod],
b = [0, 0, t, c, c, q][mod],
m = (1 - c) * gr;

return colorFunctions.rgba(
(r + m) * 255,
(g + m) * 255,
(b + m) * 255,
a
);
},

hsv: function(h, s, v) {
return colorFunctions.hsva(h, s, v, 1.0);
},
Expand Down Expand Up @@ -5784,6 +5813,28 @@ Color.prototype.toHSL = function () {
}
return { h: h * 360, s: s, l: l, a: a };
};
Color.prototype.toHCG = 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 c = (max - min), gr = 0, h = 0;

if (c < 1) { gr = min / (1 - c); }

if (c > 0) {
switch (max) {
case r: h = (g - b) / c + (g < b ? 6 : 0); break;
case g: h = (b - r) / c + 2; break;
case b: h = (r - g) / c + 4; break;
}
h /= 6;
}

return {h: h * 360, c: c, g: gr, a: a};
};
//Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
Color.prototype.toHSV = function () {
var r = this.rgb[0] / 255,
Expand Down Expand Up @@ -5872,6 +5923,7 @@ var Node = require("./node"),
var Comment = function (value, isLineComment, index, currentFileInfo) {
this.value = value;
this.isLineComment = isLineComment;
this.index = index;
this.currentFileInfo = currentFileInfo;
this.allowRoot = true;
};
Expand Down
10 changes: 5 additions & 5 deletions dist/less.min.js

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions lib/less/functions/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,35 @@ colorFunctions = {
a);
},

hcg: function(h, c, gr) {
return colorFunctions.hcga(h, c, gr, 1.0);
},

hcga: function(h, c, gr, a) {
h = (number(h) % 360) / 360 * 6;
c = number(c); gr = number(gr);

if (c <= 0) {
return [gr * 255, gr * 255, gr * 255];
}

var i = Math.floor(h),
f = h - i,
q = c * (1 - f), t = c * f,
mod = i % 6,
r = [c, q, 0, 0, t, c][mod],
g = [t, c, c, q, 0, 0][mod],
b = [0, 0, t, c, c, q][mod],
m = (1 - c) * gr;

return colorFunctions.rgba(
(r + m) * 255,
(g + m) * 255,
(b + m) * 255,
a
);
},

hsv: function(h, s, v) {
return colorFunctions.hsva(h, s, v, 1.0);
},
Expand Down
22 changes: 22 additions & 0 deletions lib/less/tree/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ Color.prototype.toHSL = function () {
}
return { h: h * 360, s: s, l: l, a: a };
};
Color.prototype.toHCG = function () {
Copy link
Member

@seven-phases-max seven-phases-max Jul 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does not seem to be used anywhere so what is it its purpose?

Copy link
Author

@ghost ghost Jul 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For HCG planned modifiers (for example "purity", "absorbtion"). But I thinking about names, that will not conflict with HSL/HSV/HWB. Problem of HCG: they describes material property.

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 c = (max - min), gr = 0, h = 0;

if (c < 1) { gr = min / (1 - c); }

if (c > 0) {
switch (max) {
case r: h = (g - b) / c + (g < b ? 6 : 0); break;
case g: h = (b - r) / c + 2; break;
case b: h = (r - g) / c + 4; break;
}
h /= 6;
}

return {h: h * 360, c: c, g: gr, a: a};
};
//Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
Color.prototype.toHSV = function () {
var r = this.rgb[0] / 255,
Expand Down
2 changes: 2 additions & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
fade-out2-relative: rgba(255, 0, 0, 0.25);
hsv: #4d2926;
hsva: rgba(77, 40, 38, 0.2);
hcg: #4d2a27;
hcga: rgba(77, 42, 39, 0.2);
mix: #ff3300;
mix-0: #ffff00;
mix-100: #ff0000;
Expand Down
9 changes: 6 additions & 3 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@

fade-out: fadeout(red, 5%); // support fadeOut and fadeout
fade-in: fadein(fadeout(red, 10%), 5%);
fade-out-relative: fadeout(red, 5%,relative);
fade-out-relative: fadeout(red, 5%,relative);
fade-in-relative: fadein(fadeout(red, 10%, relative), 5%, relative);
fade-out2: fadeout(fadeout(red, 50%), 50%);
fade-out2: fadeout(fadeout(red, 50%), 50%);
fade-out2-relative: fadeout(fadeout(red, 50%, relative), 50%, relative);

hsv: hsv(5, 50%, 30%);
hsva: hsva(3, 50%, 30%, 0.2);

hcg: hcg(5, 15%, 18%);
hcga: hcga(5, 15%, 18%, 0.2);

mix: mix(#ff0000, #ffff00, 80);
mix-0: mix(#ff0000, #ffff00, 0);
mix-100: mix(#ff0000, #ffff00, 100);
Expand Down