mirror of
https://github.com/less/less.js.git
synced 2026-02-08 22:15:04 -05:00
More consistent named colour variables.
This commit is contained in:
@@ -310,21 +310,19 @@ tree.functions = {
|
||||
percentage: function (n) {
|
||||
return new(tree.Dimension)(n.value * 100, '%');
|
||||
},
|
||||
color: function (n) {
|
||||
if (n instanceof tree.Quoted) {
|
||||
var colorCandidate = n.value,
|
||||
returnColor;
|
||||
returnColor = tree.Color.fromKeyword(colorCandidate);
|
||||
if (returnColor) {
|
||||
return returnColor;
|
||||
}
|
||||
if (/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/.test(colorCandidate)) {
|
||||
return new(tree.Color)(colorCandidate.slice(1));
|
||||
}
|
||||
throw { type: "Argument", message: "argument must be a color keyword or 3/6 digit hex e.g. #FFF" };
|
||||
} else {
|
||||
throw { type: "Argument", message: "argument must be a string" };
|
||||
color: function(c) {
|
||||
if ((c instanceof tree.Quoted) &&
|
||||
(/^#([a-f0-9]{6}|[a-f0-9]{3})$/i.test(c.value))) {
|
||||
return new(tree.Color)(c.value.slice(1));
|
||||
}
|
||||
if ((c instanceof tree.Color) || (c = tree.Color.fromKeyword(c.value))) {
|
||||
c.keyword = undefined;
|
||||
return c;
|
||||
}
|
||||
throw {
|
||||
type: "Argument",
|
||||
message: "argument must be a color keyword or 3/6 digit hex e.g. #FFF"
|
||||
};
|
||||
},
|
||||
iscolor: function (n) {
|
||||
return this._isa(n, tree.Color);
|
||||
|
||||
@@ -793,15 +793,9 @@ less.Parser = function Parser(env) {
|
||||
// black border-collapse
|
||||
//
|
||||
keyword: function () {
|
||||
var k;
|
||||
|
||||
k = $re(/^[_A-Za-z-][_A-Za-z0-9-]*/);
|
||||
var k = $(/^[_A-Za-z-][_A-Za-z0-9-]*/);
|
||||
if (k) {
|
||||
var color = tree.Color.fromKeyword(k);
|
||||
if (color) {
|
||||
return color;
|
||||
}
|
||||
return new(tree.Keyword)(k);
|
||||
return tree.Color.fromKeyword(k) || new(tree.Keyword)(k);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1960,4 +1954,4 @@ less.Parser.serializeVars = function(vars) {
|
||||
}
|
||||
|
||||
return s;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -23,8 +23,6 @@ tree.Color = function (rgb, a) {
|
||||
this.alpha = typeof(a) === 'number' ? a : 1;
|
||||
};
|
||||
|
||||
var transparentKeyword = "transparent";
|
||||
|
||||
tree.Color.prototype = {
|
||||
type: "Color",
|
||||
eval: function () { return this; },
|
||||
@@ -34,35 +32,39 @@ tree.Color.prototype = {
|
||||
output.add(this.toCSS(env));
|
||||
},
|
||||
toCSS: function (env, doNotCompress) {
|
||||
var compress = env && env.compress && !doNotCompress,
|
||||
alpha = tree.fround(env, this.alpha);
|
||||
var compress = env && env.compress && !doNotCompress, color, alpha;
|
||||
|
||||
// `keyword` is set if this color was originally
|
||||
// converted from a named color string so we need
|
||||
// to respect this and try to output named color too.
|
||||
if (this.keyword) {
|
||||
return this.keyword;
|
||||
}
|
||||
|
||||
// If we have some transparency, the only way to represent it
|
||||
// is via `rgba`. Otherwise, we use the hex representation,
|
||||
// which has better compatibility with older browsers.
|
||||
// Values are capped between `0` and `255`, rounded and zero-padded.
|
||||
alpha = tree.fround(env, this.alpha);
|
||||
if (alpha < 1) {
|
||||
if (alpha === 0 && this.isTransparentKeyword) {
|
||||
return transparentKeyword;
|
||||
}
|
||||
return "rgba(" + this.rgb.map(function (c) {
|
||||
return clamp(Math.round(c), 255);
|
||||
}).concat(clamp(alpha, 1))
|
||||
.join(',' + (compress ? '' : ' ')) + ")";
|
||||
} else {
|
||||
var color = this.toRGB();
|
||||
}
|
||||
|
||||
color = this.toRGB();
|
||||
|
||||
if (compress) {
|
||||
var splitcolor = color.split('');
|
||||
if (compress) {
|
||||
var splitcolor = color.split('');
|
||||
|
||||
// Convert color to short format
|
||||
if (splitcolor[1] === splitcolor[2] && splitcolor[3] === splitcolor[4] && splitcolor[5] === splitcolor[6]) {
|
||||
color = '#' + splitcolor[1] + splitcolor[3] + splitcolor[5];
|
||||
}
|
||||
// Convert color to short format
|
||||
if (splitcolor[1] === splitcolor[2] && splitcolor[3] === splitcolor[4] && splitcolor[5] === splitcolor[6]) {
|
||||
color = '#' + splitcolor[1] + splitcolor[3] + splitcolor[5];
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
return color;
|
||||
},
|
||||
|
||||
//
|
||||
@@ -152,16 +154,17 @@ tree.Color.prototype = {
|
||||
};
|
||||
|
||||
tree.Color.fromKeyword = function(keyword) {
|
||||
keyword = keyword.toLowerCase();
|
||||
|
||||
if (tree.colors.hasOwnProperty(keyword)) {
|
||||
// detect named color
|
||||
return new(tree.Color)(tree.colors[keyword].slice(1));
|
||||
var c, key = keyword.toLowerCase();
|
||||
if (tree.colors.hasOwnProperty(key)) {
|
||||
c = new(tree.Color)(tree.colors[key].slice(1));
|
||||
}
|
||||
if (keyword === transparentKeyword) {
|
||||
var transparent = new(tree.Color)([0, 0, 0], 0);
|
||||
transparent.isTransparentKeyword = true;
|
||||
return transparent;
|
||||
else if (key === "transparent") {
|
||||
c = new(tree.Color)([0, 0, 0], 0);
|
||||
}
|
||||
|
||||
if (c) {
|
||||
c.keyword = keyword;
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user