-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
83 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** ! | ||
* (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com | ||
* Derived from work by Brian Suda, https://24ways.org/2010/calculating-color-contrast/ | ||
*/ | ||
|
||
/** | ||
* Determine if color is light or dark | ||
* @param {String} hexcolor Hex color value (#000 or #000000) | ||
* @returns {String} The contrasting color (black or white) | ||
*/ | ||
export default function (hexcolor) { | ||
// Remove leading '#' | ||
let color = hexcolor.slice(1) | ||
|
||
// If a three-character hexcode, make six-character | ||
if (color.length === 3) { | ||
color = color.split('').map(value => value + value).join('') | ||
} | ||
|
||
// Convert to RGB value | ||
const r = parseInt(color.slice(0, 2), 16) | ||
const g = parseInt(color.slice(2, 4), 16) | ||
const b = parseInt(color.slice(4, 6), 16) | ||
|
||
// Get YIQ ratio | ||
const yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000 | ||
|
||
console.log(yiq) | ||
|
||
// Check contrast | ||
return yiq >= 128 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters