Skip to content

Commit 4dbd965

Browse files
Material Engcopybara-github
authored andcommitted
- add content color to android schemes
- add content color to score - add core palette public methods for all schemes - add palette token set (applyTheme) - add brightness suffix (applyTheme) PiperOrigin-RevId: 454055065
1 parent da1e5ae commit 4dbd965

File tree

4 files changed

+85
-11
lines changed

4 files changed

+85
-11
lines changed

typescript/scheme/scheme.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ export class Scheme {
163163
return Scheme.darkFromCorePalette(CorePalette.contentOf(argb));
164164
}
165165

166-
private static lightFromCorePalette(core: CorePalette): Scheme {
166+
/**
167+
* Light scheme from core palette
168+
*/
169+
static lightFromCorePalette(core: CorePalette): Scheme {
167170
return new Scheme({
168171
primary: core.a1.tone(40),
169172
onPrimary: core.a1.tone(100),
@@ -195,7 +198,10 @@ export class Scheme {
195198
});
196199
}
197200

198-
private static darkFromCorePalette(core: CorePalette): Scheme {
201+
/**
202+
* Dark scheme from core palette
203+
*/
204+
static darkFromCorePalette(core: CorePalette): Scheme {
199205
return new Scheme({
200206
primary: core.a1.tone(80),
201207
onPrimary: core.a1.tone(20),

typescript/scheme/scheme_android.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,40 @@ export class SchemeAndroid {
127127
*/
128128
static light(argb: number): SchemeAndroid {
129129
const core = CorePalette.of(argb);
130+
return SchemeAndroid.lightFromCorePalette(core);
131+
}
132+
133+
/**
134+
* @param argb ARGB representation of a color.
135+
* @return Dark Material color scheme, based on the color's hue.
136+
*/
137+
static dark(argb: number): SchemeAndroid {
138+
const core = CorePalette.of(argb);
139+
return SchemeAndroid.darkFromCorePalette(core);
140+
}
141+
142+
/**
143+
* @param argb ARGB representation of a color.
144+
* @return Light Android color scheme, based on the color's hue.
145+
*/
146+
static lightContent(argb: number): SchemeAndroid {
147+
const core = CorePalette.contentOf(argb);
148+
return SchemeAndroid.lightFromCorePalette(core);
149+
}
150+
151+
/**
152+
* @param argb ARGB representation of a color.
153+
* @return Dark Android color scheme, based on the color's hue.
154+
*/
155+
static darkContent(argb: number): SchemeAndroid {
156+
const core = CorePalette.contentOf(argb);
157+
return SchemeAndroid.darkFromCorePalette(core);
158+
}
159+
160+
/**
161+
* Light scheme from core palette
162+
*/
163+
static lightFromCorePalette(core: CorePalette): SchemeAndroid {
130164
return new SchemeAndroid({
131165
colorAccentPrimary: core.a1.tone(90),
132166
colorAccentPrimaryVariant: core.a1.tone(40),
@@ -157,11 +191,9 @@ export class SchemeAndroid {
157191
}
158192

159193
/**
160-
* @param argb ARGB representation of a color.
161-
* @return Dark Material color scheme, based on the color's hue.
194+
* Dark scheme from core palette
162195
*/
163-
static dark(argb: number): SchemeAndroid {
164-
const core = CorePalette.of(argb);
196+
static darkFromCorePalette(core: CorePalette): SchemeAndroid {
165197
return new SchemeAndroid({
166198
colorAccentPrimary: core.a1.tone(90),
167199
colorAccentPrimaryVariant: core.a1.tone(70),
@@ -218,4 +250,8 @@ export class SchemeAndroid {
218250
volumeBackground: number,
219251
scrim: number
220252
}) {}
253+
254+
toJSON() {
255+
return {...this.props};
256+
}
221257
}

typescript/score/score.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class Score {
5050
* were not suitable for a theme, a default fallback color will be
5151
* provided, Google Blue.
5252
*/
53-
static score(colorsToPopulation: Map<number, number>): number[] {
53+
static score(colorsToPopulation: Map<number, number>, contentColor = false):
54+
number[] {
5455
// Determine the total count of all colors.
5556
let populationSum = 0;
5657
for (const population of colorsToPopulation.values()) {
@@ -106,7 +107,9 @@ export class Score {
106107

107108
// Remove colors that are unsuitable, ex. very dark or unchromatic colors.
108109
// Also, remove colors that are very similar in hue.
109-
const filteredColors = Score.filter(colorsToExcitedProportion, colorsToCam);
110+
const filteredColors = contentColor ?
111+
Score.filterContent(colorsToCam) :
112+
Score.filter(colorsToExcitedProportion, colorsToCam);
110113
const dedupedColorsToScore = new Map<number, number>();
111114
for (const color of filteredColors) {
112115
let duplicateHue = false;
@@ -156,4 +159,8 @@ export class Score {
156159
}
157160
return filtered;
158161
}
162+
163+
private static filterContent(colorsToCam: Map<number, Cam16>): number[] {
164+
return Array.from(colorsToCam.keys());
165+
}
159166
}

typescript/utils/theme_utils.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,38 @@ export function customColor(
154154
export function applyTheme(theme: Theme, options?: {
155155
dark?: boolean,
156156
target?: HTMLElement,
157+
brightnessSuffix?: boolean,
158+
paletteTones?: number[],
157159
}) {
158160
const target = options?.target || document.body;
159161
const isDark = options?.dark ?? false;
160162
const scheme = isDark ? theme.schemes.dark : theme.schemes.light;
163+
setSchemeProperties(target, scheme);
164+
if (options?.brightnessSuffix) {
165+
setSchemeProperties(target, theme.schemes.dark, '-dark');
166+
setSchemeProperties(target, theme.schemes.light, '-light');
167+
}
168+
if (options?.paletteTones) {
169+
const tones = options?.paletteTones ?? [];
170+
for (const [key, palette] of Object.entries(theme.palettes)) {
171+
const paletteKey = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
172+
for (const tone of tones) {
173+
const token = `--md-ref-palette-${paletteKey}-${paletteKey}${tone}`;
174+
const color = hexFromArgb(palette.tone(tone));
175+
target.style.setProperty(token, color);
176+
}
177+
}
178+
}
179+
}
180+
181+
function setSchemeProperties(
182+
target: HTMLElement,
183+
scheme: Scheme,
184+
suffix: string = '',
185+
) {
161186
for (const [key, value] of Object.entries(scheme.toJSON())) {
162-
const token = key.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
187+
const token = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
163188
const color = hexFromArgb(value);
164-
target.style.setProperty(`--md-sys-color-${token}`, color);
189+
target.style.setProperty(`--md-sys-color-${token}${suffix}`, color);
165190
}
166-
}
191+
}

0 commit comments

Comments
 (0)