Remove throws in function evaluation and handle color function arguments

correctly.
This commit is contained in:
Tom MacWright 2013-01-03 13:47:51 -05:00
parent ece3eb3b0e
commit 6f687ff9e3
6 changed files with 39 additions and 7 deletions

View File

@ -7,6 +7,7 @@ tree.functions = {
rgba: function (r, g, b, a) {
var rgb = [r, g, b].map(function (c) { return number(c); });
a = number(a);
if (rgb.some(isNaN) || isNaN(a)) return null;
return new tree.Color(rgb, a);
},
hsl: function (h, s, l) {
@ -15,9 +16,10 @@ tree.functions = {
hsla: function (h, s, l, a) {
h = (number(h) % 360) / 360;
s = number(s); l = number(l); a = number(a);
if ([h, s, l, a].some(isNaN)) return null;
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
var m1 = l * 2 - m2;
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s,
m1 = l * 2 - m2;
return this.rgba(hue(h + 1/3) * 255,
hue(h) * 255,
@ -154,8 +156,8 @@ tree.functions['agg-stack-blur'] = function(x, y) {
return new tree.ImageFilter('agg-stack-blur', [x, y]);
};
function hsla(hsla) {
return tree.functions.hsla(hsla.h, hsla.s, hsla.l, hsla.a);
function hsla(h) {
return tree.functions.hsla(h.h, h.s, h.l, h.a);
}
function number(n) {
@ -164,7 +166,7 @@ function number(n) {
} else if (typeof(n) === 'number') {
return n;
} else {
throw new Error('Color functions take numbers as parameters.');
return NaN;
}
}

View File

@ -84,7 +84,7 @@ carto.Renderer.prototype.render = function render(m, callback) {
var rulesets = _(m.Stylesheet).chain()
.map(function(s) {
if (typeof s == 'string') {
throw new Error("Stylesheet object is expected not a string: '" + s + "'");
callback(new Error("Stylesheet object is expected not a string: '" + s + "'"));
}
// Passing the environment from stylesheet to stylesheet,
// allows frames and effects to be maintained.

View File

@ -30,7 +30,17 @@ tree.Call.prototype = {
if (this.name in tree.functions) {
if (tree.functions[this.name].length === args.length) {
return tree.functions[this.name].apply(tree.functions, args);
var val = tree.functions[this.name].apply(tree.functions, args);
if (val == null) {
env.error({
message: 'incorrect arguments given to ' + this.name + '()',
index: this.index,
type: 'runtime',
filename: this.filename
});
return { is: 'undefined', value: 'undefined' };
}
return val;
} else {
env.error({
message: 'incorrect number of arguments for ' + this.name +

View File

@ -0,0 +1,15 @@
{
"srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
"Stylesheet": [
"color_functions.mss"
],
"Layer": [{
"id": "world",
"name": "world",
"srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
"Datasource": {
"file": "http://tilemill-data.s3.amazonaws.com/test_data/shape_demo.zip",
"type": "shape"
}
}]
}

View File

@ -0,0 +1,4 @@
@foo: 'bar';
#world {
polygon-fill: hsl(1, @foo, 3);
}

View File

@ -0,0 +1 @@
color_functions.mss:3:31 incorrect arguments given to hsl()