Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Last active January 2, 2016 07:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderjeurissen/8272694 to your computer and use it in GitHub Desktop.
Save alexanderjeurissen/8272694 to your computer and use it in GitHub Desktop.
String to pastelcolor script. If added to your project you can simply use: "string to be converted".getPastel(); to convert the string to a pastel color.
String.prototype.getPastel = function (rgb) {
var radix = (!rgb) ? 16 : 10;
var hashCode = this.split("").reduce(function (a, b) {
a = ((a << 5) - a) + b.charCodeAt(0);
b = ((b << 5) - b) + a;
return a << b;
}, 0);
var rad = function (bitshift) {
return Math.round(
((((hashCode >> bitshift) & 0xFF) + 255) / 2)
).toString(radix);
};
return (radix === 10) ? "rgb(" + rad(24) + "," + rad(16) + "," + rad(8) + ")" : "#" + rad(24) + rad(16) + rad(8);
};
@alexanderjeurissen
Copy link
Author

usage:

"swag".getPastel(); => "#809b88"

"swag".getPastel(1); => "rgb(128,155,136}"

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