Skip to content

Instantly share code, notes, and snippets.

@SevInf
Last active August 27, 2020 13:22
Show Gist options
  • Save SevInf/ba4e3a13e832764a93ae260ddff9000b to your computer and use it in GitHub Desktop.
Save SevInf/ba4e3a13e832764a93ae260ddff9000b to your computer and use it in GitHub Desktop.
Some color conversion functions
// converts 255, 255, 255 -> #ffffff
export function rgbToHex(r, g, b) {
return '#' + to_hex(r) + to_hex(g) + to_hex(b);
}
function to_hex(color) {
return color.toString(16);
}
// converts 255, 255, 255 -> #ffffff
function hex2RGB(hex) {
const r = parseNumber(hex.slice(1, 3), 16)
const g = parseNumber(hex.slice(3, 5), 16)
const b = parseNumber(hex.slice(6, 8), 16)
return { r, g, b };
}
// computes brightness of the pixel
export function luminance(color) {
return
0.2126 * color.R +
0.7152 * color.G +
0.0722 * color.b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment