Created
July 26, 2017 16:57
-
-
Save imerkle/47072ad76d2504cce9cc7febe398f765 to your computer and use it in GitHub Desktop.
genarate color from string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getHashCode = str => { | |
let hash = 0; | |
for (let i = 0; i < str.length; i++) { | |
hash = str.charCodeAt(i) + ((hash << 5) - hash); | |
} | |
return hash; | |
}; | |
const getHSL = (number, saturation, lightness) => { | |
var shortened = Math.abs(number % 360); | |
return `hsl(${shortened}, ${saturation}%, ${lightness}%)`; | |
}; | |
export const getColorFromString = (str, lightness = 20, saturation = 90) => { | |
const col1 = getHSL(getHashCode(str), saturation, lightness); | |
return col1; | |
}; | |
export const getGradientColorFromString = (str, lightness = 20, saturation = 90) => { | |
const col1 = getHSL(getHashCode(str), saturation, lightness); | |
const col2 = getHSL(getHashCode(str.split("").reverse().join("")), saturation + 20, lightness + 20); | |
return `linear-gradient(to right,${col1}, ${col2})`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment