forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createColors.ts
324 lines (278 loc) · 8.34 KB
/
createColors.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { merge } from 'lodash';
import { alpha, darken, emphasize, getContrastRatio, lighten } from './colorManipulator';
import { palette } from './palette';
import { DeepPartial, ThemeRichColor } from './types';
/** @internal */
export type ThemeColorsMode = 'light' | 'dark';
/** @internal */
export interface ThemeColorsBase<TColor> {
mode: ThemeColorsMode;
primary: TColor;
secondary: TColor;
info: TColor;
error: TColor;
success: TColor;
warning: TColor;
text: {
primary: string;
secondary: string;
disabled: string;
link: string;
/** Used for auto white or dark text on colored backgrounds */
maxContrast: string;
};
background: {
/** Dashboard and body background */
canvas: string;
/** Primary content pane background (panels etc) */
primary: string;
/** Cards and elements that need to stand out on the primary background */
secondary: string;
};
border: {
weak: string;
medium: string;
strong: string;
};
gradients: {
brandVertical: string;
brandHorizontal: string;
};
action: {
/** Used for selected menu item / select option */
selected: string;
/**
* @alpha (Do not use from plugins)
* Used for selected items when background only change is not enough (Currently only used for FilterPill)
**/
selectedBorder: string;
/** Used for hovered menu item / select option */
hover: string;
/** Used for button/colored background hover opacity */
hoverOpacity: number;
/** Used focused menu item / select option */
focus: string;
/** Used for disabled buttons and inputs */
disabledBackground: string;
/** Disabled text */
disabledText: string;
/** Disablerd opacity */
disabledOpacity: number;
};
hoverFactor: number;
contrastThreshold: number;
tonalOffset: number;
}
export interface ThemeHoverStrengh {}
/** @beta */
export interface ThemeColors extends ThemeColorsBase<ThemeRichColor> {
/** Returns a text color for the background */
getContrastText(background: string, threshold?: number): string;
/* Brighten or darken a color by specified factor (0-1) */
emphasize(color: string, amount?: number): string;
}
/** @internal */
export type ThemeColorsInput = DeepPartial<ThemeColorsBase<ThemeRichColor>>;
class DarkColors implements ThemeColorsBase<Partial<ThemeRichColor>> {
mode: ThemeColorsMode = 'dark';
// Used to get more white opacity colors
whiteBase = '204, 204, 220';
border = {
weak: `rgba(${this.whiteBase}, 0.12)`,
medium: `rgba(${this.whiteBase}, 0.20)`,
strong: `rgba(${this.whiteBase}, 0.30)`,
};
text = {
primary: `rgb(${this.whiteBase})`,
secondary: `rgba(${this.whiteBase}, 0.65)`,
disabled: `rgba(${this.whiteBase}, 0.6)`,
link: palette.blueDarkText,
maxContrast: palette.white,
};
primary = {
main: palette.blueDarkMain,
text: palette.blueDarkText,
border: palette.blueDarkText,
};
secondary = {
main: `rgba(${this.whiteBase}, 0.10)`,
shade: `rgba(${this.whiteBase}, 0.14)`,
transparent: `rgba(${this.whiteBase}, 0.08)`,
text: this.text.primary,
contrastText: `rgb(${this.whiteBase})`,
border: `rgba(${this.whiteBase}, 0.08)`,
};
info = this.primary;
error = {
main: palette.redDarkMain,
text: palette.redDarkText,
};
success = {
main: palette.greenDarkMain,
text: palette.greenDarkText,
};
warning = {
main: palette.orangeDarkMain,
text: palette.orangeDarkText,
};
background = {
canvas: palette.gray05,
primary: palette.gray10,
secondary: palette.gray15,
};
action = {
hover: `rgba(${this.whiteBase}, 0.16)`,
selected: `rgba(${this.whiteBase}, 0.12)`,
selectedBorder: palette.orangeDarkMain,
focus: `rgba(${this.whiteBase}, 0.16)`,
hoverOpacity: 0.08,
disabledText: this.text.disabled,
disabledBackground: `rgba(${this.whiteBase}, 0.04)`,
disabledOpacity: 0.38,
};
gradients = {
brandHorizontal: 'linear-gradient(270deg, #F55F3E 0%, #FF8833 100%)',
brandVertical: 'linear-gradient(0.01deg, #F55F3E 0.01%, #FF8833 99.99%)',
};
contrastThreshold = 3;
hoverFactor = 0.03;
tonalOffset = 0.15;
}
class LightColors implements ThemeColorsBase<Partial<ThemeRichColor>> {
mode: ThemeColorsMode = 'light';
blackBase = '36, 41, 46';
primary = {
main: palette.blueLightMain,
border: palette.blueLightText,
text: palette.blueLightText,
};
text = {
primary: `rgba(${this.blackBase}, 1)`,
secondary: `rgba(${this.blackBase}, 0.75)`,
disabled: `rgba(${this.blackBase}, 0.64)`,
link: this.primary.text,
maxContrast: palette.black,
};
border = {
weak: `rgba(${this.blackBase}, 0.12)`,
medium: `rgba(${this.blackBase}, 0.30)`,
strong: `rgba(${this.blackBase}, 0.40)`,
};
secondary = {
main: `rgba(${this.blackBase}, 0.08)`,
shade: `rgba(${this.blackBase}, 0.15)`,
transparent: `rgba(${this.blackBase}, 0.08)`,
contrastText: `rgba(${this.blackBase}, 1)`,
text: this.text.primary,
border: this.border.weak,
};
info = {
main: palette.blueLightMain,
text: palette.blueLightText,
};
error = {
main: palette.redLightMain,
text: palette.redLightText,
border: palette.redLightText,
};
success = {
main: palette.greenLightMain,
text: palette.greenLightText,
};
warning = {
main: palette.orangeLightMain,
text: palette.orangeLightText,
};
background = {
canvas: palette.gray90,
primary: palette.white,
secondary: palette.gray100,
};
action = {
hover: `rgba(${this.blackBase}, 0.12)`,
selected: `rgba(${this.blackBase}, 0.08)`,
selectedBorder: palette.orangeLightMain,
hoverOpacity: 0.08,
focus: `rgba(${this.blackBase}, 0.12)`,
disabledBackground: `rgba(${this.blackBase}, 0.04)`,
disabledText: this.text.disabled,
disabledOpacity: 0.38,
};
gradients = {
brandHorizontal: 'linear-gradient(90deg, #FF8833 0%, #F53E4C 100%)',
brandVertical: 'linear-gradient(0.01deg, #F53E4C -31.2%, #FF8833 113.07%)',
};
contrastThreshold = 3;
hoverFactor = 0.03;
tonalOffset = 0.2;
}
export function createColors(colors: ThemeColorsInput): ThemeColors {
const dark = new DarkColors();
const light = new LightColors();
const base = (colors.mode ?? 'dark') === 'dark' ? dark : light;
const {
primary = base.primary,
secondary = base.secondary,
info = base.info,
warning = base.warning,
success = base.success,
error = base.error,
tonalOffset = base.tonalOffset,
hoverFactor = base.hoverFactor,
contrastThreshold = base.contrastThreshold,
...other
} = colors;
function getContrastText(background: string, threshold: number = contrastThreshold) {
const contrastText =
getContrastRatio(dark.text.maxContrast, background, base.background.primary) >= threshold
? dark.text.maxContrast
: light.text.maxContrast;
// todo, need color framework
return contrastText;
}
const getRichColor = ({ color, name }: GetRichColorProps): ThemeRichColor => {
color = { ...color, name };
if (!color.main) {
throw new Error(`Missing main color for ${name}`);
}
if (!color.text) {
color.text = color.main;
}
if (!color.border) {
color.border = color.text;
}
if (!color.shade) {
color.shade = base.mode === 'light' ? darken(color.main, tonalOffset) : lighten(color.main, tonalOffset);
}
if (!color.transparent) {
color.transparent = alpha(color.main, 0.15);
}
if (!color.contrastText) {
color.contrastText = getContrastText(color.main);
}
if (!color.borderTransparent) {
color.borderTransparent = alpha(color.border, 0.25);
}
return color as ThemeRichColor;
};
return merge(
{
...base,
primary: getRichColor({ color: primary, name: 'primary' }),
secondary: getRichColor({ color: secondary, name: 'secondary' }),
info: getRichColor({ color: info, name: 'info' }),
error: getRichColor({ color: error, name: 'error' }),
success: getRichColor({ color: success, name: 'success' }),
warning: getRichColor({ color: warning, name: 'warning' }),
getContrastText,
emphasize: (color: string, factor?: number) => {
return emphasize(color, factor ?? hoverFactor);
},
},
other
);
}
interface GetRichColorProps {
color: Partial<ThemeRichColor>;
name: string;
}