Add contrast function following the same format as sass. Added tests.

Added luma calculation.
This commit is contained in:
Marcus Bointon
2011-11-24 16:46:23 +01:00
committed by Luke Page
parent 7fc6275ba8
commit bb0886fcc9
3 changed files with 68 additions and 0 deletions

View File

@@ -44,6 +44,12 @@ tree.functions = {
alpha: function (color) {
return new(tree.Dimension)(color.toHSL().a);
},
luma: function (color) {
return new(tree.Dimension)(Math.round((0.2126 * (color.rgb[0]/255) +
0.7152 * (color.rgb[1]/255) +
0.0722 * (color.rgb[2]/255))
* color.alpha * 100), '%');
},
saturate: function (color, amount) {
var hsl = color.toHSL();
@@ -127,6 +133,24 @@ tree.functions = {
greyscale: function (color) {
return this.desaturate(color, new(tree.Dimension)(100));
},
contrast: function (color, dark, light, threshold) {
if (typeof light === 'undefined') {
light = this.rgba(255, 255, 255, 1.0);
}
if (typeof dark === 'undefined') {
dark = this.rgba(0, 0, 0, 1.0);
}
if (typeof threshold === 'undefined') {
threshold = 0.43;
} else {
threshold = threshold.value;
}
if (((0.2126 * (color.rgb[0]/255) + 0.7152 * (color.rgb[1]/255) + 0.0722 * (color.rgb[2]/255)) * color.alpha) < threshold) {
return light;
} else {
return dark;
}
},
e: function (str) {
return new(tree.Anonymous)(str instanceof tree.JavaScript ? str.evaluated : str);
},