Created
March 30, 2017 07:11
-
-
Save rchanou/77de4b277d63739e4db8333cd9d780ab to your computer and use it in GitHub Desktop.
Get Hue From Hash
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
export const getHueFromHash = hash => { | |
let hue = 0; | |
for (var i in hash) { | |
hue += Math.pow(hash.charCodeAt(i), 2); | |
} | |
return hue % 360; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a pure function which generates an arbitrary but consistent value between 0-360 for a given string. I'm sure it could be improved to be more "evenly distributed".
This is useful for dynamically generating good-looking color schemes, which is one of the cool things you can do with the HSL(A) color format. For example, a list of items can be colored by ID this way. If they have different hues, but the same saturation and lightness, they will generally look pretty good together.
It's also ripe for optimization via memoization.