这里是an example I created on stackblitz,关于如何创建具有角度的主题并根据调色板颜色定义较浅和较深的颜色
这是重要的代码:
主题.scss:
@import '~@angular/material/theming';
@include mat-core();
$custom-primary: mat-palette($mat-teal);
$custom-accent: mat-palette($mat-pink, A200, A100, A400);
$custom-warn: mat-palette($mat-red);
$custom-theme: mat-light-theme($custom-primary, $custom-accent, $custom-warn);
@include angular-material-theme($custom-theme);
在这个文件中,我创建了一个自定义主题并定义了一个浅色主题。
变量.scss:
@import './theme.scss';
$primary: mat-color($custom-primary);
$primary-lighter: mat-color($custom-primary, lighter);
$primary-darker: mat-color($custom-primary, darker);
在这里,我根据上面主题中定义的原色创建了 3 个包含原色、浅色和深色的变量。
请注意,较浅和较深的颜色必须基于调色板中的颜色。您不能只选择一种随机颜色(即使它是有角度的颜色。对于此处的红色,您必须使用$custom-warn)。如果您仍然需要从调色板中调亮或调暗颜色,仍然有 scss lighten 和 darken 混合允许您这样做:
lighten($myColor, 30%),darken 相同。
要使这个主题和变量起作用,您需要在styles.scss 中导入theme.scss,并且无论您想在何处使用自定义变量,都必须导入variables.scss,仅此而已。