Skip to content

Commit 771a0b3

Browse files
committed
added contrastText
1 parent 96c55dd commit 771a0b3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/chromath.js

+16
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,22 @@ Chromath.invert = function (color)
706706
return new Chromath("#" + innerColor);
707707
};
708708

709+
/*
710+
Method: Chromath.contrastText
711+
Choose a readable text color that has enough contrast
712+
adapted from http://24ways.org/2010/calculating-color-contrast/
713+
714+
Examples:
715+
> > Chromath.contrastText('rgb(0, 100, 255)').toString()
716+
> '#FF9B00'
717+
718+
*/
719+
Chromath.contrastText = function (color){
720+
var c = color.rgb();
721+
var yiq = ((c[0]*299)+(c[1]*587)+(c[2]*114))/1000;
722+
return (yiq >= 128) ? new Chromath('black') : Chromath('white');
723+
};
724+
709725
//Group: Static methods - color alteration
710726
/*
711727
Method: Chromath.tint

0 commit comments

Comments
 (0)