From 4b113be27e9fa40215b151f72f510e352a4bfcfc Mon Sep 17 00:00:00 2001 From: seven-phases-max Date: Sun, 24 Nov 2013 04:46:27 +0400 Subject: [PATCH] improved alpha channel handling for math ops, removed 'can't substract or divide a color from a number' constraint --- lib/less/tree/color.js | 12 ++++-------- lib/less/tree/operation.js | 13 +++++-------- test/less/errors/color-operation-error.less | 3 --- test/less/errors/color-operation-error.txt | 2 -- 4 files changed, 9 insertions(+), 21 deletions(-) delete mode 100644 test/less/errors/color-operation-error.less delete mode 100644 test/less/errors/color-operation-error.txt diff --git a/lib/less/tree/color.js b/lib/less/tree/color.js index 2e45db11d..469c4c89f 100644 --- a/lib/less/tree/color.js +++ b/lib/less/tree/color.js @@ -70,16 +70,12 @@ tree.Color.prototype = { // we create a new Color node to hold the result. // operate: function (env, op, other) { - var result = []; - - if (! (other instanceof tree.Color)) { - other = other.toColor(); - } - + var rgb = []; + var alpha = this.alpha * (1 - other.alpha) + other.alpha; for (var c = 0; c < 3; c++) { - result[c] = tree.operate(env, op, this.rgb[c], other.rgb[c]); + rgb[c] = tree.operate(env, op, this.rgb[c], other.rgb[c]); } - return new(tree.Color)(result, this.alpha + other.alpha); + return new(tree.Color)(rgb, alpha); }, toRGB: function () { diff --git a/lib/less/tree/operation.js b/lib/less/tree/operation.js index ec806e29d..8f1dd6511 100644 --- a/lib/less/tree/operation.js +++ b/lib/less/tree/operation.js @@ -12,17 +12,14 @@ tree.Operation.prototype = { }, eval: function (env) { var a = this.operands[0].eval(env), - b = this.operands[1].eval(env), - temp; + b = this.operands[1].eval(env); if (env.isMathOn()) { if (a instanceof tree.Dimension && b instanceof tree.Color) { - if (this.op === '*' || this.op === '+') { - temp = b, b = a, a = temp; - } else { - throw { type: "Operation", - message: "Can't substract or divide a color from a number" }; - } + a = a.toColor(); + } + if (b instanceof tree.Dimension && a instanceof tree.Color) { + b = b.toColor(); } if (!a.operate) { throw { type: "Operation", diff --git a/test/less/errors/color-operation-error.less b/test/less/errors/color-operation-error.less deleted file mode 100644 index 7c60c0047..000000000 --- a/test/less/errors/color-operation-error.less +++ /dev/null @@ -1,3 +0,0 @@ -.a { - prop: (3 / #fff); -} \ No newline at end of file diff --git a/test/less/errors/color-operation-error.txt b/test/less/errors/color-operation-error.txt deleted file mode 100644 index 1b3f889fc..000000000 --- a/test/less/errors/color-operation-error.txt +++ /dev/null @@ -1,2 +0,0 @@ -OperationError: Can't substract or divide a color from a number in {path}color-operation-error.less on line null, column 0: -1 prop: (3 / #fff);