Skip to content

Commit 7a6b2de

Browse files
committed
use deterministic string colors for other city names
1 parent 13c5556 commit 7a6b2de

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

app/components/map.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,22 @@ function getCityColor(team) {
9696
'Köln': '#6b5aac'
9797
};
9898

99-
return colorlist[team.event.city] ? colorlist[team.event.city] : randomColor();
99+
return colorlist[team.event.city] ? colorlist[team.event.city] : stringColor(team.event.city);
100100
}
101101

102-
function randomColor() {
103-
return '#' + Math.floor(Math.random() * 16777215).toString(16);
102+
function stringColor(string) {
103+
var hash = 0;
104+
if (string.length === 0) return hash;
105+
for (var i = 0; i < string.length; i++) {
106+
hash = string.charCodeAt(i) + ((hash << 5) - hash);
107+
hash = hash & hash;
108+
}
109+
var color = '#';
110+
for (var j = 0; j < 3; j++) {
111+
var value = (hash >> (j * 8)) & 255;
112+
color += ('00' + value.toString(16)).substr(-2);
113+
}
114+
return color;
104115
}
105116

106117
function colorGradientByWeight(color1, color2, weight) {

0 commit comments

Comments
 (0)