Introduce color operations constant for use in single stat

Could be useful elsewhere
pull/2545/head
Alex P 2017-12-11 11:16:07 -08:00
parent cf9ab6d2a7
commit 4cc1bb2844
1 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,24 @@
const hexToRgb = hex => {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null
}
const averageRgbValues = valuesObject => {
const {r, g, b} = valuesObject
return (r + g + b) / 3
}
const trueNeutralGrey = 128
export const isBackgroundLight = backgroundColor => {
const averageBackground = averageRgbValues(hexToRgb(backgroundColor))
const isLight = averageBackground > trueNeutralGrey
return isLight
}