Skip to content

Instantly share code, notes, and snippets.

@rchanou
Created March 30, 2017 07:11
Show Gist options
  • Save rchanou/77de4b277d63739e4db8333cd9d780ab to your computer and use it in GitHub Desktop.
Save rchanou/77de4b277d63739e4db8333cd9d780ab to your computer and use it in GitHub Desktop.
Get Hue From Hash
export const getHueFromHash = hash => {
let hue = 0;
for (var i in hash) {
hue += Math.pow(hash.charCodeAt(i), 2);
}
return hue % 360;
};
@rchanou
Copy link
Author

rchanou commented Apr 1, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment