All files / app/codeCharta/util/color getReadableColorForBackground.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 912x   112x 112x 112x 112x 112x    
export const getReadableColorForBackground = (hex: string) => {
    // algorithm taken from https://24ways.org/2010/calculating-color-contrast/
    const r = Number.parseInt(hex.slice(1, 3), 16)
    const g = Number.parseInt(hex.slice(3, 5), 16)
    const b = Number.parseInt(hex.slice(5, 7), 16)
    const yiqRatio = (r * 299 + g * 587 + b * 114) / 1000
    return yiqRatio >= 128 ? "black" : "white"
}