This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
2020-06-16 22:20:37 +00:00
|
|
|
export const colorIsDark = color => {
|
2021-07-06 20:22:57 +00:00
|
|
|
if (typeof color === 'undefined') {
|
|
|
|
return true // Defaults to dark
|
|
|
|
}
|
|
|
|
|
2020-06-16 22:20:37 +00:00
|
|
|
if (color === '#' || color === '') {
|
|
|
|
return true // Defaults to dark
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:01:02 +00:00
|
|
|
if (color.substring(0, 1) !== '#') {
|
|
|
|
color = '#' + color
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:07:34 +00:00
|
|
|
const rgb = parseInt(color.substring(1, 7), 16) // convert rrggbb to decimal
|
|
|
|
const r = (rgb >> 16) & 0xff // extract red
|
|
|
|
const g = (rgb >> 8) & 0xff // extract green
|
|
|
|
const b = (rgb >> 0) & 0xff // extract blue
|
2020-06-16 22:20:37 +00:00
|
|
|
|
|
|
|
// luma will be a value 0..255 where 0 indicates the darkest, and 255 the brightest
|
2022-02-15 12:07:34 +00:00
|
|
|
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b // per ITU-R BT.709
|
2020-06-16 22:20:37 +00:00
|
|
|
return luma > 128
|
|
|
|
}
|