Skip to content

Instantly share code, notes, and snippets.

@einarlove
Created February 15, 2017 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarlove/69d31d030eeb634d96b2ddd2f93b181b to your computer and use it in GitHub Desktop.
Save einarlove/69d31d030eeb634d96b2ddd2f93b181b to your computer and use it in GitHub Desktop.
Takes a hex color and luminosity adjustment and outputs new color
/* eslint-disable no-bitwise */
export default (inputColor, amt) => {
let usePound = false
let color = inputColor
if (color[0] === '#') {
color = color.slice(1)
usePound = true
}
const num = parseInt(color, 16)
let r = (num >> 16) + amt
if (r > 255) r = 255
else if (r < 0) r = 0
let b = ((num >> 8) & 0x00FF) + amt
if (b > 255) b = 255
else if (b < 0) b = 0
let g = (num & 0x0000FF) + amt
if (g > 255) g = 255
else if (g < 0) g = 0
return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment