@@ -24,6 +24,81 @@ $_internals: _mat-theming-internals-do-not-access;
2424 @return if ($err , 0 , map .get ($theme , $_internals , theme-version ) or 0 );
2525}
2626
27+ /// Gets the type of theme represented by a theme object (light or dark).
28+ /// @param {Map} $theme The theme
29+ /// @return {String} The type of theme (either `light` or `dark`).
30+ @function get-theme-type ($theme ) {
31+ $err : _validate-theme-object ($theme );
32+ @if $err {
33+ // TODO(mmalerba): implement for old style theme objects.
34+ @error #{' get-theme-type does not support legacy theme objects.' } ;
35+ }
36+ @return map .get ($theme , $_internals , theme-type ) or light ;
37+ }
38+
39+ /// Gets a color from a theme object. This function can take 2 or 3 arguments. If 2 arguments are
40+ /// passed, the second argument is treated as the name of a color role. If 3 arguments are passed,
41+ /// the second argument is treated as the name of a color palette (primary, secondary, etc.) and the
42+ /// third is treated as the palette hue (10, 50, etc.)
43+ /// @param {Map} $theme The theme
44+ /// @param {String} $color-role-or-palette-name The name of the color role to get, or the name of a
45+ /// color palette.
46+ /// @param {Number} $hue The palette hue to get (passing this argument means the second argument is
47+ /// interpreted as a palette name).
48+ /// @return {Color} The requested theme color.
49+ @function get-theme-color ($theme , $args ... ) {
50+ $args-count : list .length ($args );
51+ @if $args-count == 1 {
52+ @return _get-theme-role-color ($theme , $args ... );
53+ } @else if $args-count == 2 {
54+ @return _get-theme-palette-color ($theme , $args ... );
55+ }
56+ @error #{' Expected 2 or 3 arguments. Got:' } $args-count + 1;
57+ }
58+
59+ /// Gets a role color from a theme object.
60+ /// @param {Map} $theme The theme
61+ /// @param {String} $color-role-name The name of the color role to get.
62+ /// @return {Color} The requested role color.
63+ @function _get-theme-role-color ($theme , $color-role-name ) {
64+ $err : _validate-theme-object ($theme );
65+ @if $err {
66+ // TODO(mmalerba): implement for old style theme objects.
67+ @error #{' get-theme-color does not support legacy theme objects.' } ;
68+ }
69+ $color-roles : map .get ($theme , $_internals , color-tokens , (mdc, theme));
70+ $result : map .get ($color-roles , $color-role-name );
71+ @if not $result {
72+ @error #{' Valid color roles are: #{map .keys ($color-roles )} . Got:' } $color-role-name ;
73+ }
74+ @return $result ;
75+ }
76+
77+ /// Gets a palette color from a theme object.
78+ /// @param {Map} $theme The theme
79+ /// @param {String} $palette-name The name of the palette to get the color from.
80+ /// @param {Number} $hue The hue to read from the palette.
81+ /// @return {Color} The requested palette color.
82+ @function _get-theme-palette-color ($theme , $palette-name , $hue ) {
83+ $err : _validate-theme-object ($theme );
84+ @if $err {
85+ // TODO(mmalerba): implement for old style theme objects.
86+ @error #{' get-theme-color does not support legacy theme objects.' } ;
87+ }
88+ $palettes : map .get ($theme , $_internals , palettes );
89+ $palette : map .get ($palettes , $palette-name );
90+ @if not $palette {
91+ $supported-palettes : map .keys ($palettes );
92+ @error #{' Valid palettes are: #{$supported-palettes } . Got:' } $palette-name ;
93+ }
94+ $result : map .get ($palette , $hue );
95+ @if not $result {
96+ $supported-hues : map .keys ($palette );
97+ @error #{' Valid hues for' } $palette-name #{' are: #{$supported-hues } . Got:' } $hue ;
98+ }
99+ @return $result ;
100+ }
101+
27102/// Gets the set of tokens from the given theme, limited to those affected by the requested theming
28103/// systems.
29104/// @param {Map} $theme The theme to get tokens from.
@@ -38,9 +113,8 @@ $_internals: _mat-theming-internals-do-not-access;
38113 }
39114 $err : validation .validate-allowed-values ($systems , color , typography , density , base );
40115 @if $err {
41- @error
42- #{' Expected $systems to contain valid system names (color, typographt, density, or base).' }
43- #{' Got invalid system names:' } $err ;
116+ @error #{' Expected $systems to contain valid system names (color, typography, density, or' }
117+ #{' base). Got invalid system names:' } $err ;
44118 }
45119 $result : ();
46120 @each $system in $systems {
0 commit comments