aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/palette.ts
blob: fa99364ff927bb8d52fa8d5e552241617d8eb12b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import Color from "color";
import { BehaviorSubject, Observable } from "rxjs";

import refreshAnimation from "./utilities/refreshAnimation";

function lightenBy(color: Color, ratio: number): Color {
  const lightness = color.lightness();
  return color.lightness(lightness + (100 - lightness) * ratio);
}

function darkenBy(color: Color, ratio: number): Color {
  const lightness = color.lightness();
  return color.lightness(lightness - lightness * ratio);
}

export interface PaletteColor {
  color: string;
  l1: string;
  l2: string;
  l3: string;
  d1: string;
  d2: string;
  d3: string;
  f1: string;
  f2: string;
  f3: string;
  r1: string;
  r2: string;
  r3: string;
  [key: string]: string;
}

const paletteColorList = [
  "primary",
  "primary-enhance",
  "secondary",
  "text-primary",
  "text-on-primary",
  "danger",
  "success",
] as const;

export type PaletteColorType = typeof paletteColorList[number];

export type Palette = Record<PaletteColorType, PaletteColor>;

export function generatePaletteColor(color: string): PaletteColor {
  const c = Color(color);
  const light = c.lightness() > 60;
  const l1 = lightenBy(c, 0.1).rgb().toString();
  const l2 = lightenBy(c, 0.2).rgb().toString();
  const l3 = lightenBy(c, 0.3).rgb().toString();
  const d1 = darkenBy(c, 0.1).rgb().toString();
  const d2 = darkenBy(c, 0.2).rgb().toString();
  const d3 = darkenBy(c, 0.3).rgb().toString();
  const f1 = light ? l1 : d1;
  const f2 = light ? l2 : d2;
  const f3 = light ? l3 : d3;
  const r1 = light ? d1 : l1;
  const r2 = light ? d2 : l2;
  const r3 = light ? d3 : l3;

  return {
    color: c.rgb().toString(),
    l1,
    l2,
    l3,
    d1,
    d2,
    d3,
    f1,
    f2,
    f3,
    r1,
    r2,
    r3,
  };
}

export function generatePalette(options: {
  primary: string;
  primaryEnhance?: string;
  secondary?: string;
}): Palette {
  const { primary, primaryEnhance, secondary } = options;
  const p = Color(primary);
  const pe =
    primaryEnhance == null
      ? lightenBy(p, 0.3).saturate(0.3)
      : Color(primaryEnhance);
  const s = secondary == null ? p.rotate(90) : Color(secondary);

  return {
    primary: generatePaletteColor(p.toString()),
    "primary-enhance": generatePaletteColor(pe.toString()),
    secondary: generatePaletteColor(s.toString()),
    "text-primary": generatePaletteColor("#111111"),
    "text-on-primary": generatePaletteColor(
      p.lightness() > 60 ? "black" : "white"
    ),
    danger: generatePaletteColor("red"),
    success: generatePaletteColor("green"),
  };
}

export function generatePaletteCSS(palette: Palette): string {
  const colors: [string, string][] = [];
  for (const colorType of paletteColorList) {
    const paletteColor = palette[colorType];
    for (const variant in paletteColor) {
      let key = `--tl-${colorType}`;
      if (variant !== "color") key += `-${variant}`;
      key += "-color";
      colors.push([key, paletteColor[variant]]);
    }
  }

  return `:root {${colors
    .map(([key, color]) => `${key} : ${color};`)
    .join("")}}`;
}

const paletteSubject: BehaviorSubject<Palette | null> =
  new BehaviorSubject<Palette | null>(
    // generatePalette({ primary: "rgb(0, 123, 255)" })
    null
  );

export const palette$: Observable<Palette | null> =
  paletteSubject.asObservable();

palette$.subscribe((palette) => {
  const styleTagId = "timeline-palette-css";
  if (palette != null) {
    let styleTag = document.getElementById(styleTagId);
    if (styleTag == null) {
      styleTag = document.createElement("style");
      styleTag.id = styleTagId;
      document.head.append(styleTag);
    }
    styleTag.innerHTML = generatePaletteCSS(palette);
  } else {
    const styleTag = document.getElementById(styleTagId);
    if (styleTag != null) {
      styleTag.parentElement?.removeChild(styleTag);
    }
  }

  refreshAnimation();
});

export function setPalette(palette: Palette): () => void {
  const old = paletteSubject.value;

  paletteSubject.next(palette);

  return () => {
    paletteSubject.next(old);
  };
}