Skip to content

Instantly share code, notes, and snippets.

@imerkle
Created July 26, 2017 16:57
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 imerkle/47072ad76d2504cce9cc7febe398f765 to your computer and use it in GitHub Desktop.
Save imerkle/47072ad76d2504cce9cc7febe398f765 to your computer and use it in GitHub Desktop.
genarate color from string
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