Skip to content

Commit

Permalink
Add isDark() and isLight() functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
avp committed Jan 5, 2014
1 parent ba57321 commit c8eb329
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spectra.js
Expand Up @@ -552,6 +552,22 @@
return this.desaturate(100);
};

/**
* If a color is dark then it's best to have white text on it.
* http://24ways.org/2010/calculating-color-contrast
*/
Spectra.fn.prototype.isDark = function() {
var yiq = ((this.red()*299)+(this.green()*587)+(this.blue()*144))/1000;
return yiq < 131.5;
};

/**
* If a color is light then it's best to have black text on it.
*/
Spectra.fn.prototype.isLight = function() {
return !this.isDark();
};

/**
* Returns the color that results from mixing percent of the other color into this color.
*/
Expand Down
3 changes: 3 additions & 0 deletions test/tester.js
Expand Up @@ -122,6 +122,9 @@ describe('Spectra', function() {
expect(color.fadeOut(5).alpha()).toBeCloseTo(0.55, 1);
expect(color.luma()).toBeCloseTo(77.5, 1);
expect(color.grayscale().hex()).toBe('#8c8c8c');
expect(color.isDark()).toBe(true);
expect(color.isLight()).toBe(false);
expect(Spectra('#000').isDark()).toBe(true);
});

it('Mix', function() {
Expand Down

0 comments on commit c8eb329

Please sign in to comment.