-
Notifications
You must be signed in to change notification settings - Fork 136
/
core_palette.ts
122 lines (112 loc) · 3.58 KB
/
core_palette.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
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Hct} from '../hct/hct.js';
import {TonalPalette} from './tonal_palette.js';
/**
* Set of colors to generate a [CorePalette] from
*/
export interface CorePaletteColors {
primary: number;
secondary?: number;
tertiary?: number;
neutral?: number;
neutralVariant?: number;
error?: number;
}
/**
* An intermediate concept between the key color for a UI theme, and a full
* color scheme. 5 sets of tones are generated, all except one use the same hue
* as the key color, and all vary in chroma.
*/
export class CorePalette {
a1: TonalPalette;
a2: TonalPalette;
a3: TonalPalette;
n1: TonalPalette;
n2: TonalPalette;
error: TonalPalette;
/**
* @param argb ARGB representation of a color
*/
static of(argb: number): CorePalette {
return new CorePalette(argb, false);
}
/**
* @param argb ARGB representation of a color
*/
static contentOf(argb: number): CorePalette {
return new CorePalette(argb, true);
}
/**
* Create a [CorePalette] from a set of colors
*/
static fromColors(colors: CorePaletteColors): CorePalette {
return CorePalette.createPaletteFromColors(false, colors);
}
/**
* Create a content [CorePalette] from a set of colors
*/
static contentFromColors(colors: CorePaletteColors): CorePalette {
return CorePalette.createPaletteFromColors(true, colors);
}
private static createPaletteFromColors(
content: boolean,
colors: CorePaletteColors,
) {
const palette = new CorePalette(colors.primary, content);
if (colors.secondary) {
const p = new CorePalette(colors.secondary, content);
palette.a2 = p.a1;
}
if (colors.tertiary) {
const p = new CorePalette(colors.tertiary, content);
palette.a3 = p.a1;
}
if (colors.error) {
const p = new CorePalette(colors.error, content);
palette.error = p.a1;
}
if (colors.neutral) {
const p = new CorePalette(colors.neutral, content);
palette.n1 = p.n1;
}
if (colors.neutralVariant) {
const p = new CorePalette(colors.neutralVariant, content);
palette.n2 = p.n2;
}
return palette;
}
private constructor(argb: number, isContent: boolean) {
const hct = Hct.fromInt(argb);
const hue = hct.hue;
const chroma = hct.chroma;
if (isContent) {
this.a1 = TonalPalette.fromHueAndChroma(hue, chroma);
this.a2 = TonalPalette.fromHueAndChroma(hue, chroma / 3);
this.a3 = TonalPalette.fromHueAndChroma(hue + 60, chroma / 2);
this.n1 = TonalPalette.fromHueAndChroma(hue, Math.min(chroma / 12, 4));
this.n2 = TonalPalette.fromHueAndChroma(hue, Math.min(chroma / 6, 8));
} else {
this.a1 = TonalPalette.fromHueAndChroma(hue, Math.max(48, chroma));
this.a2 = TonalPalette.fromHueAndChroma(hue, 16);
this.a3 = TonalPalette.fromHueAndChroma(hue + 60, 24);
this.n1 = TonalPalette.fromHueAndChroma(hue, 4);
this.n2 = TonalPalette.fromHueAndChroma(hue, 8);
}
this.error = TonalPalette.fromHueAndChroma(25, 84);
}
}