Better implementation of luma

This commit is contained in:
Synchro
2013-01-23 09:30:15 +01:00
parent 783f5d895a
commit 8f1bc4badf
4 changed files with 11 additions and 5 deletions

View File

@@ -84,10 +84,7 @@ tree.functions = {
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), '%');
return new(tree.Dimension)(Math.round(color.luma() * color.alpha * 100), '%');
},
saturate: function (color, amount) {
var hsl = color.toHSL();
@@ -184,12 +181,18 @@ tree.functions = {
if (typeof dark === 'undefined') {
dark = this.rgba(0, 0, 0, 1.0);
}
//Figure out which is actually light and dark!
if (dark.luma() > light.luma()) {
var t = light;
light = dark;
dark = t;
}
if (typeof threshold === 'undefined') {
threshold = 0.43;
} else {
threshold = number(threshold);
}
if (((0.2126 * (color.rgb[0]/255) + 0.7152 * (color.rgb[1]/255) + 0.0722 * (color.rgb[2]/255)) * color.alpha) < threshold) {
if ((color.luma() * color.alpha) < threshold) {
return light;
} else {
return dark;

View File

@@ -24,6 +24,7 @@ tree.Color = function (rgb, a) {
};
tree.Color.prototype = {
eval: function () { return this },
luma: function () { return (0.2126 * this.rgb[0] / 255) + (0.7152 * this.rgb[1] / 255) + (0.0722 * this.rgb[2] / 255); },
//
// If we have some transparency, the only way to represent it

View File

@@ -34,6 +34,7 @@
contrast-cyan: #000000;
contrast-light: #111111;
contrast-dark: #eeeeee;
contrast-wrongorder: #111111;
contrast-light-thresh: #111111;
contrast-dark-thresh: #eeeeee;
contrast-high-thresh: #eeeeee;

View File

@@ -38,6 +38,7 @@
contrast-cyan: contrast(#00ffff);
contrast-light: contrast(#fff, #111111, #eeeeee);
contrast-dark: contrast(#000, #111111, #eeeeee);
contrast-wrongorder: contrast(#fff, #eeeeee, #111111, 0.5);
contrast-light-thresh: contrast(#fff, #111111, #eeeeee, 0.5);
contrast-dark-thresh: contrast(#000, #111111, #eeeeee, 0.5);
contrast-high-thresh: contrast(#555, #111111, #eeeeee, 0.6);