Dimensions.

This commit is contained in:
Tom MacWright 2013-04-15 16:10:00 -04:00
parent c21a763dc7
commit 0f65b869fd
4 changed files with 29 additions and 14 deletions

View File

@ -68,42 +68,42 @@ tree.functions = {
saturate: function (color, amount) { saturate: function (color, amount) {
var hsl = color.toHSL(); var hsl = color.toHSL();
hsl.s += amount.value / 100; hsl.s += amount.value;
hsl.s = clamp(hsl.s); hsl.s = clamp(hsl.s);
return hsla(hsl); return hsla(hsl);
}, },
desaturate: function (color, amount) { desaturate: function (color, amount) {
var hsl = color.toHSL(); var hsl = color.toHSL();
hsl.s -= amount.value / 100; hsl.s -= amount.value;
hsl.s = clamp(hsl.s); hsl.s = clamp(hsl.s);
return hsla(hsl); return hsla(hsl);
}, },
lighten: function (color, amount) { lighten: function (color, amount) {
var hsl = color.toHSL(); var hsl = color.toHSL();
hsl.l += amount.value / 100; hsl.l += amount.value;
hsl.l = clamp(hsl.l); hsl.l = clamp(hsl.l);
return hsla(hsl); return hsla(hsl);
}, },
darken: function (color, amount) { darken: function (color, amount) {
var hsl = color.toHSL(); var hsl = color.toHSL();
hsl.l -= amount.value / 100; hsl.l -= amount.value;
hsl.l = clamp(hsl.l); hsl.l = clamp(hsl.l);
return hsla(hsl); return hsla(hsl);
}, },
fadein: function (color, amount) { fadein: function (color, amount) {
var hsl = color.toHSL(); var hsl = color.toHSL();
hsl.a += amount.value / 100; hsl.a += amount.value;
hsl.a = clamp(hsl.a); hsl.a = clamp(hsl.a);
return hsla(hsl); return hsla(hsl);
}, },
fadeout: function (color, amount) { fadeout: function (color, amount) {
var hsl = color.toHSL(); var hsl = color.toHSL();
hsl.a -= amount.value / 100; hsl.a -= amount.value;
hsl.a = clamp(hsl.a); hsl.a = clamp(hsl.a);
return hsla(hsl); return hsla(hsl);
}, },
@ -127,7 +127,7 @@ tree.functions = {
// http://sass-lang.com // http://sass-lang.com
// //
mix: function (color1, color2, weight) { mix: function (color1, color2, weight) {
var p = weight.value / 100.0; var p = weight.value;
var w = p * 2 - 1; var w = p * 2 - 1;
var a = color1.toHSL().a - color2.toHSL().a; var a = color1.toHSL().a - color2.toHSL().a;

View File

@ -45,6 +45,11 @@ tree.Dimension.prototype = {
this.unit = 'px'; this.unit = 'px';
} }
if (this.unit == '%') {
this.value /= 100;
this.unit = null;
}
return this; return this;
}, },
toColor: function() { toColor: function() {

12
test/dimension.text.js Normal file
View File

@ -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);
});
});

View File

@ -12,7 +12,7 @@ describe('Operation', function() {
var env = { ppi:72, error:function(err) { console.log(err.message); } }; var env = { ppi:72, error:function(err) { console.log(err.message); } };
var o = new tree.Operation("+", [ new tree.Dimension(2), new tree.Dimension(10, "%") ]); 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() { 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') ]); var o = new tree.Operation("+", [ new tree.Dimension(2.54, 'cm'), new tree.Dimension(0.0254, 'm') ]);
assert.equal(o.ev(env).value, 144); 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); 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); 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') ]); var o = new tree.Operation("+", [ new tree.Dimension(2.54, 'cm'), new tree.Dimension(0.0254, 'm') ]);
assert.equal(o.ev(env).value, 600); 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); 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); assert.equal(o.ev(env).value, 600);
}); });
}); });