From 0f65b869fd2c81ef5aa7227ffe21caa7fb952afb Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Mon, 15 Apr 2013 16:10:00 -0400 Subject: [PATCH] Dimensions. --- lib/carto/functions.js | 14 +++++++------- lib/carto/tree/dimension.js | 5 +++++ test/dimension.text.js | 12 ++++++++++++ test/operation.test.js | 12 +++++------- 4 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 test/dimension.text.js diff --git a/lib/carto/functions.js b/lib/carto/functions.js index e8239b4..f799da5 100644 --- a/lib/carto/functions.js +++ b/lib/carto/functions.js @@ -68,42 +68,42 @@ tree.functions = { saturate: function (color, amount) { var hsl = color.toHSL(); - hsl.s += amount.value / 100; + hsl.s += amount.value; hsl.s = clamp(hsl.s); return hsla(hsl); }, desaturate: function (color, amount) { var hsl = color.toHSL(); - hsl.s -= amount.value / 100; + hsl.s -= amount.value; hsl.s = clamp(hsl.s); return hsla(hsl); }, lighten: function (color, amount) { var hsl = color.toHSL(); - hsl.l += amount.value / 100; + hsl.l += amount.value; hsl.l = clamp(hsl.l); return hsla(hsl); }, darken: function (color, amount) { var hsl = color.toHSL(); - hsl.l -= amount.value / 100; + hsl.l -= amount.value; hsl.l = clamp(hsl.l); return hsla(hsl); }, fadein: function (color, amount) { var hsl = color.toHSL(); - hsl.a += amount.value / 100; + hsl.a += amount.value; hsl.a = clamp(hsl.a); return hsla(hsl); }, fadeout: function (color, amount) { var hsl = color.toHSL(); - hsl.a -= amount.value / 100; + hsl.a -= amount.value; hsl.a = clamp(hsl.a); return hsla(hsl); }, @@ -127,7 +127,7 @@ tree.functions = { // http://sass-lang.com // mix: function (color1, color2, weight) { - var p = weight.value / 100.0; + var p = weight.value; var w = p * 2 - 1; var a = color1.toHSL().a - color2.toHSL().a; diff --git a/lib/carto/tree/dimension.js b/lib/carto/tree/dimension.js index 07232f2..acb9210 100644 --- a/lib/carto/tree/dimension.js +++ b/lib/carto/tree/dimension.js @@ -45,6 +45,11 @@ tree.Dimension.prototype = { this.unit = 'px'; } + if (this.unit == '%') { + this.value /= 100; + this.unit = null; + } + return this; }, toColor: function() { diff --git a/test/dimension.text.js b/test/dimension.text.js new file mode 100644 index 0000000..3ba7b92 --- /dev/null +++ b/test/dimension.text.js @@ -0,0 +1,12 @@ +var assert = require('assert'); +var tree = require('../lib/carto/tree.js'); +require('../lib/carto/tree/dimension'); + +describe('Dimension', function() { + it('should support percentages', function() { + assert.equal((new tree.Dimension(2, '%')).ev({}).value, 0.02); + assert.equal((new tree.Dimension(20, '%')).ev({}).value, 0.20); + assert.equal((new tree.Dimension(100, '%')).ev({}).value, 1); + assert.equal((new tree.Dimension(0, '%')).ev({}).value, 0); + }); +}); diff --git a/test/operation.test.js b/test/operation.test.js index 936d6b9..9b94aa2 100644 --- a/test/operation.test.js +++ b/test/operation.test.js @@ -12,7 +12,7 @@ describe('Operation', function() { var env = { ppi:72, error:function(err) { console.log(err.message); } }; var o = new tree.Operation("+", [ new tree.Dimension(2), new tree.Dimension(10, "%") ]); - assert.equal(o.ev(env).value, 2.2); + assert.equal(o.ev(env).value, 2.1); }); it('should work with units', function() { @@ -21,10 +21,10 @@ describe('Operation', function() { var o = new tree.Operation("+", [ new tree.Dimension(2.54, 'cm'), new tree.Dimension(0.0254, 'm') ]); assert.equal(o.ev(env).value, 144); - var o = new tree.Operation("+", [ new tree.Dimension(25.4, 'mm'), new tree.Dimension(72, 'pt') ]); + o = new tree.Operation("+", [ new tree.Dimension(25.4, 'mm'), new tree.Dimension(72, 'pt') ]); assert.equal(o.ev(env).value, 144); - var o = new tree.Operation("+", [ new tree.Dimension(72, 'pt'), new tree.Dimension(6, 'pc') ]); + o = new tree.Operation("+", [ new tree.Dimension(72, 'pt'), new tree.Dimension(6, 'pc') ]); assert.equal(o.ev(env).value, 144); }); @@ -34,12 +34,10 @@ describe('Operation', function() { var o = new tree.Operation("+", [ new tree.Dimension(2.54, 'cm'), new tree.Dimension(0.0254, 'm') ]); assert.equal(o.ev(env).value, 600); - var o = new tree.Operation("+", [ new tree.Dimension(25.4, 'mm'), new tree.Dimension(72, 'pt') ]); + o = new tree.Operation("+", [ new tree.Dimension(25.4, 'mm'), new tree.Dimension(72, 'pt') ]); assert.equal(o.ev(env).value, 600); - var o = new tree.Operation("+", [ new tree.Dimension(72, 'pt'), new tree.Dimension(6, 'pc') ]); + o = new tree.Operation("+", [ new tree.Dimension(72, 'pt'), new tree.Dimension(6, 'pc') ]); assert.equal(o.ev(env).value, 600); }); - - });