react-conventions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
28 lines (22 loc) • 574 B
JavaScript
const ColorLuminance = (hex, lum) => {
if (typeof hex === 'undefined' || !hex) {
return false
}
// Validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '')
if (hex.length < 3) {
return false
}
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2]
}
lum = lum || 0
let rgb = "#", c, i
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16)
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16)
rgb += ("00"+c).substr(c.length)
}
return rgb
}
export default ColorLuminance