Last active
January 2, 2016 07:49
-
-
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.
This file contains hidden or 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
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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
"swag".getPastel(); => "#809b88"
"swag".getPastel(1); => "rgb(128,155,136}"