feat: improve colorIsDark helper
(also improve contrast of white label text)
This commit is contained in:
parent
72925fb938
commit
297d283090
2 changed files with 9 additions and 5 deletions
|
@ -1,4 +1,4 @@
|
||||||
export const colorIsDark = color => {
|
export function colorIsDark(color) {
|
||||||
if (typeof color === 'undefined') {
|
if (typeof color === 'undefined') {
|
||||||
return true // Defaults to dark
|
return true // Defaults to dark
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,11 @@ export const colorIsDark = color => {
|
||||||
const g = (rgb >> 8) & 0xff // extract green
|
const g = (rgb >> 8) & 0xff // extract green
|
||||||
const b = (rgb >> 0) & 0xff // extract blue
|
const b = (rgb >> 0) & 0xff // extract blue
|
||||||
|
|
||||||
// luma will be a value 0..255 where 0 indicates the darkest, and 255 the brightest
|
// this is a quick and dirty implementation of the WCAG 3.0 APCA color contrast formula
|
||||||
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b // per ITU-R BT.709
|
// see: https://gist.github.com/Myndex/e1025706436736166561d339fd667493#andys-shortcut-to-luminance--lightness
|
||||||
return luma > 128
|
const Ys = Math.pow(r/255.0,2.2) * 0.2126 +
|
||||||
|
Math.pow(g/255.0,2.2) * 0.7152 +
|
||||||
|
Math.pow(b/255.0,2.2) * 0.0722
|
||||||
|
|
||||||
|
return Math.pow(Ys,0.678) >= 0.5
|
||||||
}
|
}
|
|
@ -15,7 +15,7 @@ export default class LabelModel extends AbstractModel {
|
||||||
if (this.hexColor.substring(0, 1) !== '#') {
|
if (this.hexColor.substring(0, 1) !== '#') {
|
||||||
this.hexColor = '#' + this.hexColor
|
this.hexColor = '#' + this.hexColor
|
||||||
}
|
}
|
||||||
this.textColor = colorIsDark(this.hexColor) ? '#4a4a4a' : '#e5e5e5'
|
this.textColor = colorIsDark(this.hexColor) ? '#4a4a4a' : '#fff'
|
||||||
this.createdBy = new UserModel(this.createdBy)
|
this.createdBy = new UserModel(this.createdBy)
|
||||||
|
|
||||||
this.created = new Date(this.created)
|
this.created = new Date(this.created)
|
||||||
|
|
Loading…
Reference in a new issue