1
0

Fix labels randomly changing color after saving

This commit is contained in:
kolaente
2021-01-17 11:51:07 +01:00
parent 3313801174
commit b12869e509
10 changed files with 52 additions and 10 deletions

View File

@ -0,0 +1,13 @@
/**
* Returns the hex color code without the '#' if it has one.
*
* @param color
* @returns {string}
*/
export const colorFromHex = color => {
if (color.substring(0, 1) === '#') {
color = color.substring(1, 7)
}
return color
}

View File

@ -0,0 +1,11 @@
import {colorFromHex} from './colorFromHex'
test('hex', () => {
const color = '#ffffff'
expect(colorFromHex(color)).toBe('ffffff')
})
test('no hex', () => {
const color = 'ffffff'
expect(colorFromHex(color)).toBe('ffffff')
})

View File

@ -0,0 +1,18 @@
export const colorIsDark = color => {
if (color === '#' || color === '') {
return true // Defaults to dark
}
if (color.substring(0, 1) !== '#') {
color = '#' + color
}
let rgb = parseInt(color.substring(1, 7), 16) // convert rrggbb to decimal
let r = (rgb >> 16) & 0xff // extract red
let g = (rgb >> 8) & 0xff // extract green
let b = (rgb >> 0) & 0xff // extract blue
// luma will be a value 0..255 where 0 indicates the darkest, and 255 the brightest
let luma = 0.2126 * r + 0.7152 * g + 0.0722 * b // per ITU-R BT.709
return luma > 128
}

View File

@ -0,0 +1,16 @@
import {colorIsDark} from './colorIsDark'
test('dark color', () => {
const color = '#111111'
expect(colorIsDark(color)).toBe(false)
})
test('light color', () => {
const color = '#DDDDDD'
expect(colorIsDark(color)).toBe(true)
})
test('default dark', () => {
const color = ''
expect(colorIsDark(color)).toBe(true)
})