You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
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
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
Consider a list of strings you need to permanently assign a random color.
First you should turn the string into a hash.
```js
var string ="string"
var hash =0
for (var i =0; i <string.length; i++) {
hash =string.charCodeAt(i) + ((hash <<5) - hash);
hash = hash & hash;
}
console.log(hash) // -891985903
```
*`string.charCodeAt(i)` returns the UTF-16 code for the character at index `i`
* Bit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number.
*`hash << 5` is equivalent to `hash * Math.pow(2, 5)` (`hash * 32`), except the bit operator `<<` makes sure our result is a 32 bit number.
*`hash & hash` again, makes sure we only return a 32 bit number.
Now we have something to play around with.
### RGB
The simplest method is to turn our hash into an RGB string:
```js
String.prototype.toRGB=function() {
var hash =0;
if (this.length===0) return hash;
for (var i =0; i <this.length; i++) {
hash =this.charCodeAt(i) + ((hash <<5) - hash);
hash = hash & hash;
}
var rgb = [0, 0, 0];
for (var i =0; i <3; i++) {
var value = (hash >> (i *8)) &255;
rgb[i] = value;
}
return`rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
}
"string".toRGB() // rgb(17, 96, 213)
```
Or a hexadecimal string
```js
String.prototype.toHex=function() {
var hash =0;
if (this.length===0) return hash;
for (var i =0; i <this.length; i++) {
hash =this.charCodeAt(i) + ((hash <<5) - hash);
hash = hash & hash;
}
var color ='#';
for (var i =0; i <3; i++) {
var value = (hash >> (i *8)) &255;
color += ('00'+value.toString(16)).substr(-2);
}
return color;
}
"string".toHex() // #1160d5
```
The issue is this can potentially spit out *any* color value. Ideally we'd want to filter out values too similar to our background color, and some gray/bland colors.
### Color from array
What if we hand pick some colors, and assign each string one of those?