Added min and max builtins.

This commit is contained in:
Kevin Gillette
2013-06-14 01:06:16 -06:00
committed by Luke Page
parent d2255e64f6
commit 6df4e48976
3 changed files with 47 additions and 0 deletions

View File

@@ -266,6 +266,41 @@ tree.functions = {
throw { type: "Argument", message: "argument must be a number" };
}
},
_minmax: function (name, args) {
args = Array.prototype.slice.call(args);
switch(args.length) {
case 0: throw { type: "Argument", message: "one or more arguments required" };
case 1: return args[0];
}
var isMin = name == 'min';
function f (a, b) {
if(!(a instanceof tree.Dimension && b instanceof tree.Dimension)) {
return null;
}
var ua = a.unify();
var ub = b.unify();
if(ua.unit.compare(ub.unit) != 0) {
return null;
}
var val = ua.compare(ub);
if(isMin && val > 0 || !isMin && val < 0) {
return b;
}
return a;
}
var val = args.reduce(f);
if(val !== null) {
return val;
}
return new(tree.Anonymous)(name +
"(" + args.map(function (a) { return a.toCSS(); }).join(', ') + ")");
},
min: function () {
return this._minmax('min', arguments);
},
max: function () {
return this._minmax('max', arguments);
},
argb: function (color) {
return new(tree.Anonymous)(color.toARGB());

View File

@@ -81,6 +81,12 @@
pow: 64px;
pow: 64;
pow: 27;
min: min(3, 3em);
min: min(3pt, 3em);
min: 1%;
min: min(1%, 5px);
min: 3pt;
max: 3;
percentage: 20%;
color: #ff0011;
tint: #898989;

View File

@@ -87,6 +87,12 @@
pow: pow(8px, 2);
pow: pow(4, 3);
pow: pow(3, 3em);
min: min(3, 3em);
min: min(3pt, 3em);
min: min(1%, 5%);
min: min(1%, 5px);
min: min(1pc, 3pt);
max: max(1, 3);
percentage: percentage((10px / 50));
color: color("#ff0011");
tint: tint(#777777, 13);