You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
2 years ago
|
function hashToInteger(id: string) {
|
||
|
let hash = 0;
|
||
|
if (id.length === 0) {
|
||
|
return hash;
|
||
4 years ago
|
}
|
||
2 years ago
|
for (let i = 0; i < id.length; i++) {
|
||
|
const char = id.charCodeAt(i);
|
||
|
hash = (hash << 5) - hash + char;
|
||
|
}
|
||
|
return hash;
|
||
|
}
|
||
|
|
||
|
export const getClientColor = (
|
||
|
/**
|
||
|
* any uniquely identifying key, such as user id or socket id
|
||
|
*/
|
||
|
id: string,
|
||
|
) => {
|
||
|
// to get more even distribution in case `id` is not uniformly distributed to
|
||
|
// begin with, we hash it
|
||
|
const hash = Math.abs(hashToInteger(id));
|
||
|
// we want to get a multiple of 10 number in the range of 0-360 (in other
|
||
|
// words a hue value of step size 10). There are 37 such values including 0.
|
||
|
const hue = (hash % 37) * 10;
|
||
|
const saturation = 100;
|
||
|
const lightness = 83;
|
||
5 years ago
|
|
||
2 years ago
|
return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
|
||
5 years ago
|
};
|
||
|
|
||
2 years ago
|
/**
|
||
|
* returns first char, capitalized
|
||
|
*/
|
||
|
export const getNameInitial = (name?: string | null) => {
|
||
|
// first char can be a surrogate pair, hence using codePointAt
|
||
|
const firstCodePoint = name?.trim()?.codePointAt(0);
|
||
|
return (
|
||
|
firstCodePoint ? String.fromCodePoint(firstCodePoint) : "?"
|
||
|
).toUpperCase();
|
||
5 years ago
|
};
|