-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is this project still maintained? #163
Comments
@floinay I had the same issues while trying to figure out, eventually I understood part of the source code and created this lib to assist me https://github.com/maiconcarraro/use-material-you (react) you can get some ideas looking the code, probably possible to create a convert to the old theme. |
Thanks a lot. Maybe it can help for other dev to create dynamic customizable theme with angular material and color utilities: import { argbFromHex, DynamicScheme as DS, Hct, Scheme, Theme, TonalPalette } from '@material/material-color-utilities';
import { ColorsSettings } from 'libraries/college-settings';
import { Variant } from '@material/material-color-utilities/dynamiccolor/variant';
import { DYNAMIC_SCHEME_FIELDS, DynamicSchemeProps } from './material-scheme';
type OnlyDynamicColors = keyof DynamicSchemeProps;
export type DynamicSchemeProps = {
primary: number;
onPrimary: number;
primaryContainer: number;
onPrimaryContainer: number;
secondary: number;
onSecondary: number;
secondaryContainer: number;
onSecondaryContainer: number;
tertiary: number;
onTertiary: number;
tertiaryContainer: number;
onTertiaryContainer: number;
error: number;
onError: number;
errorContainer: number;
onErrorContainer: number;
background: number;
onBackground: number;
surface: number;
onSurface: number;
surfaceVariant: number;
onSurfaceVariant: number;
outline: number;
outlineVariant: number;
shadow: number;
scrim: number;
inverseSurface: number;
inverseOnSurface: number;
inversePrimary: number;
};
export const DYNAMIC_SCHEME_FIELDS: Array<keyof DynamicSchemeProps> = [
'primary',
'onPrimary',
'primaryContainer',
'onPrimaryContainer',
'secondary',
'onSecondary',
'secondaryContainer',
'onSecondaryContainer',
'tertiary',
'onTertiary',
'tertiaryContainer',
'onTertiaryContainer',
'error',
'onError',
'errorContainer',
'onErrorContainer',
'background',
'onBackground',
'surface',
'onSurface',
'surfaceVariant',
'onSurfaceVariant',
'outline',
'outlineVariant',
'shadow',
'scrim',
'inverseSurface',
'inverseOnSurface',
'inversePrimary',
] satisfies OnlyDynamicColors[];
interface DynamicSchemeOptions {
sourceColorArgb: number;
variant: Variant;
contrastLevel: number;
isDark: boolean;
primaryPalette: TonalPalette;
secondaryPalette: TonalPalette;
tertiaryPalette: TonalPalette;
neutralPalette: TonalPalette;
neutralVariantPalette: TonalPalette;
}
class DynamicScheme extends DS {
private readonly props: DynamicSchemeProps;
constructor(args: DynamicSchemeOptions) {
super(args);
this.props = DYNAMIC_SCHEME_FIELDS.reduce((scheme, field) => {
scheme[field] = this[field];
return scheme;
}, {} as DynamicSchemeProps);
}
toJSON() {
return { ...this.props };
}
}
export function themeFromColorSettings(theme: ColorsSettings): Theme {
const source = argbFromHex(theme.colors_primary);
const hct = Hct.fromInt(argbFromHex(theme.colors_primary));
const hue = hct.hue;
const t = new DynamicScheme({
isDark: theme.is_dark_theme,
primaryPalette: TonalPalette.fromInt(argbFromHex(theme.colors_primary)),
secondaryPalette: TonalPalette.fromInt(argbFromHex(theme.colors_accent)),
tertiaryPalette: TonalPalette.fromHueAndChroma(hue + 60, 24),
neutralPalette: TonalPalette.fromHueAndChroma(hue, 4),
neutralVariantPalette: TonalPalette.fromHueAndChroma(hue, 8),
contrastLevel: 0,
sourceColorArgb: argbFromHex(theme.colors_primary),
variant: 5,
});
return {
source,
schemes: {
light: t as any as Scheme,
dark: t as any as Scheme,
},
palettes: {
primary: t.primaryPalette,
secondary: t.secondaryPalette,
tertiary: t.tertiaryPalette,
neutral: t.neutralPalette,
neutralVariant: t.neutralVariantPalette,
error: t.errorPalette,
},
customColors: [],
};
}
const theme = themeFromColorSettings(settings);
const targetElement = document.documentElement;
applyTheme(theme, {
target: targetElement,
brightnessSuffix: false,
}); |
For folks using Angular, here's package I developed for generating Material themes in Angular: https://github.com/Char2sGu/angularity/blob/develop/packages/theming-material/src/scheme.ts |
import * as m3utils from '@material/material-color-utilities';
export function themeFromSourceColor(sourceColorArgb, variant, contrastLevel) {
const sourceColorHct = m3utils.Hct.fromInt(sourceColorArgb);
const schemeVariant = (typeof m3utils[variant] === 'undefined') ? 'SchemeTonalSpot' : variant;
const schemeContrastLevel = (typeof contrastLevel === 'number') ? m3utils.clampDouble(-1.0, 1.0, contrastLevel) : 0.0;
const scheme = new m3utils[schemeVariant](sourceColorHct, false, schemeContrastLevel);
const darkScheme = new m3utils[schemeVariant](sourceColorHct, true, schemeContrastLevel);
const colorRoles = Object.keys(m3utils.MaterialDynamicColors).filter(key => m3utils.MaterialDynamicColors[key] instanceof m3utils.DynamicColor);
const getDynamicScheme = (scheme) => Object.fromEntries(colorRoles.map(role => [role, m3utils.MaterialDynamicColors[role].getHct(scheme)]));
return {
source: sourceColorHct,
schemes: {
light: getDynamicScheme(scheme),
dark: getDynamicScheme(darkScheme),
},
palettes: {
primary: scheme.primaryPalette,
secondary: scheme.secondaryPalette,
tertiary: scheme.tertiaryPalette,
neutral: scheme.neutralPalette,
neutralVariant: scheme.neutralVariantPalette,
error: scheme.errorPalette,
},
};
} |
Looking at this issue it looks like this repo is not maintained anymore. |
I have noticed several parts of the TypeScript implementation still rely on deprecated methods like CorePalette, with suggestions to use DynamicScheme instead. However, there is no proper documentation, examples, or even clear code for how to fully generate themes using DynamicScheme in a manner similar to the existing theme utilities like applyTheme.
themeFromSourceColor
does not allow for customization of parameters.The text was updated successfully, but these errors were encountered: