1 import type { ColorFormats } from 'tinycolor2';
2 import tinycolor from 'tinycolor2';
3 import { c } from 'ttag';
5 import type { SimpleMap } from '@proton/shared/lib/interfaces';
6 import randomIntFromInterval from '@proton/utils/randomIntFromInterval';
8 export const ACCENT_COLORS_MAP = {
9 purple: { color: '#8080FF', getName: () => c('color').t`purple` },
10 pink: { color: '#DB60D6', getName: () => c('color').t`pink` },
11 strawberry: { color: '#EC3E7C', getName: () => c('color').t`strawberry` },
12 carrot: { color: '#F78400', getName: () => c('color').t`carrot` },
13 sahara: { color: '#936D58', getName: () => c('color').t`sahara` },
14 enzian: { color: '#5252CC', getName: () => c('color').t`enzian` },
15 plum: { color: '#A839A4', getName: () => c('color').t`plum` },
16 cerise: { color: '#BA1E55', getName: () => c('color').t`cerise` },
17 copper: { color: '#C44800', getName: () => c('color').t`copper` },
18 soil: { color: '#54473F', getName: () => c('color').t`soil` },
19 slateblue: { color: '#415DF0', getName: () => c('color').t`slateblue` },
20 pacific: { color: '#179FD9', getName: () => c('color').t`pacific` },
21 reef: { color: '#1DA583', getName: () => c('color').t`reef` },
22 fern: { color: '#3CBB3A', getName: () => c('color').t`fern` },
23 olive: { color: '#B4A40E', getName: () => c('color').t`olive` },
24 cobalt: { color: '#273EB2', getName: () => c('color').t`cobalt` },
25 ocean: { color: '#0A77A6', getName: () => c('color').t`ocean` },
26 pine: { color: '#0F735A', getName: () => c('color').t`pine` },
27 forest: { color: '#258723', getName: () => c('color').t`forest` },
28 pickle: { color: '#807304', getName: () => c('color').t`pickle` },
31 export const ACCENT_COLORS = Object.values(ACCENT_COLORS_MAP).map(({ color }) => color);
33 const COLOR_NAME_MAP = Object.values(ACCENT_COLORS_MAP).reduce<SimpleMap<() => string>>((acc, { color, getName }) => {
39 export const getColorName = (color: string) => {
40 return COLOR_NAME_MAP[color.toUpperCase()]?.();
43 export const getRandomAccentColor = () => ACCENT_COLORS[randomIntFromInterval(0, ACCENT_COLORS.length - 1)];
45 // Euclidean distance between colors
46 const getColorDistance = (color1: ColorFormats.RGBA, color2: ColorFormats.RGBA): number => {
47 return Math.sqrt((color1.r - color2.r) ** 2 + (color2.g - color2.g) ** 2 + (color1.b - color2.b) ** 2);
50 export const getClosestProtonColor = (inputColor: string): string | undefined => {
51 const color = tinycolor(inputColor);
53 if (color.isValid()) {
54 const inputColorRGB = color.toRgb();
56 let closestDistance: number;
58 ACCENT_COLORS.forEach((protonColor) => {
59 const protonColorRGB = tinycolor(protonColor).toRgb();
60 const colorDistance = getColorDistance(protonColorRGB, inputColorRGB);
62 if (closestDistance === undefined || colorDistance < closestDistance) {
63 closestDistance = colorDistance;
64 closestColor = protonColor;
73 // List of CSS3 colors https://www.w3.org/TR/css-color-3/#svg-color
74 export const CSS3_COLORS = [
145 'lightgoldenrodyellow',