2023-05-18 16:06:27 +02:00
|
|
|
import {
|
|
|
|
DEFAULT_ELEMENT_BACKGROUND_COLOR_INDEX,
|
|
|
|
DEFAULT_ELEMENT_STROKE_COLOR_INDEX,
|
|
|
|
getAllColorsSpecificShade,
|
|
|
|
} from "./colors";
|
2021-02-06 23:33:52 +05:30
|
|
|
import { AppState } from "./types";
|
2020-06-19 11:36:49 +01:00
|
|
|
|
2023-05-18 16:06:27 +02:00
|
|
|
const BG_COLORS = getAllColorsSpecificShade(
|
|
|
|
DEFAULT_ELEMENT_BACKGROUND_COLOR_INDEX,
|
|
|
|
);
|
|
|
|
const STROKE_COLORS = getAllColorsSpecificShade(
|
|
|
|
DEFAULT_ELEMENT_STROKE_COLOR_INDEX,
|
|
|
|
);
|
|
|
|
|
2021-02-06 23:33:52 +05:30
|
|
|
export const getClientColors = (clientId: string, appState: AppState) => {
|
|
|
|
if (appState?.collaborators) {
|
|
|
|
const currentUser = appState.collaborators.get(clientId);
|
|
|
|
if (currentUser?.color) {
|
|
|
|
return currentUser.color;
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 11:36:49 +01:00
|
|
|
// Naive way of getting an integer out of the clientId
|
|
|
|
const sum = clientId.split("").reduce((a, str) => a + str.charCodeAt(0), 0);
|
|
|
|
|
|
|
|
return {
|
2023-05-18 16:06:27 +02:00
|
|
|
background: BG_COLORS[sum % BG_COLORS.length],
|
|
|
|
stroke: STROKE_COLORS[sum % STROKE_COLORS.length],
|
2020-06-19 11:36:49 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-05-14 02:49:09 +09:00
|
|
|
/**
|
|
|
|
* 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();
|
2020-06-19 11:36:49 +01:00
|
|
|
};
|