dispatch/client/js/utils/color.js

23 lines
475 B
JavaScript
Raw Normal View History

2018-10-07 23:34:53 +00:00
import { hsluvToHex } from 'hsluv';
2020-05-06 04:50:53 +00:00
import fnv1a from '@sindresorhus/fnv1a';
2018-10-06 22:25:56 +00:00
const colors = [];
for (let i = 0; i < 72; i++) {
2018-10-07 23:34:53 +00:00
colors[i] = hsluvToHex([i * 5, 40, 50]);
colors[i + 72] = hsluvToHex([i * 5, 70, 50]);
colors[i + 144] = hsluvToHex([i * 5, 100, 50]);
2018-10-06 22:25:56 +00:00
}
const cache = {};
2018-10-07 23:34:53 +00:00
export default function stringToRGB(str) {
2018-10-06 22:25:56 +00:00
if (cache[str]) {
return cache[str];
}
2018-10-07 23:34:53 +00:00
const color = colors[fnv1a(str) % colors.length];
2018-10-06 22:25:56 +00:00
cache[str] = color;
return color;
}