Skip to content

Commit

Permalink
fix(highlighter): improve luminance of color
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Jul 1, 2019
1 parent 9ea28c5 commit e5da464
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/handler/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ function toHexColor(color: Color): { red: number, green: number, blue: number }
}

function isDark(color: Color): boolean {
let { red, green, blue } = toHexColor(color)
let luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue
return luma < 40
// http://www.w3.org/TR/WCAG20/#relativeluminancedef
let rgb = [color.red, color.green, color.blue]
let lum = []
for (let i = 0; i < rgb.length; i++) {
let chan = rgb[i]
lum[i] = (chan <= 0.03928) ? chan / 12.92 : Math.pow(((chan + 0.055) / 1.055), 2.4)
}
let luma = 0.2126 * lum[0] + 0.7152 * lum[1] + 0.0722 * lum[2]
return luma <= 0.5
}

0 comments on commit e5da464

Please sign in to comment.