2021-07-28 13:15:52 +00:00
|
|
|
// https://github.com/cloudrac3r/cadencegq/blob/master/pug/mxid.pug
|
|
|
|
|
2022-02-15 11:48:25 +00:00
|
|
|
export function hashCode(str) {
|
2021-07-28 13:15:52 +00:00
|
|
|
let hash = 0;
|
|
|
|
let i;
|
|
|
|
let chr;
|
|
|
|
if (str.length === 0) {
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
for (i = 0; i < str.length; i += 1) {
|
|
|
|
chr = str.charCodeAt(i);
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
hash = ((hash << 5) - hash) + chr;
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
hash |= 0;
|
|
|
|
}
|
|
|
|
return Math.abs(hash);
|
|
|
|
}
|
2022-02-15 11:48:25 +00:00
|
|
|
|
|
|
|
export function cssColorMXID(userId) {
|
2021-07-28 13:15:52 +00:00
|
|
|
const colorNumber = hashCode(userId) % 8;
|
2022-02-15 11:48:25 +00:00
|
|
|
return `--mx-uc-${colorNumber + 1}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function colorMXID(userId) {
|
|
|
|
return `var(${cssColorMXID(userId)})`;
|
2021-07-28 13:15:52 +00:00
|
|
|
}
|