This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy path_theme-color.scss
365 lines (314 loc) · 12.4 KB
/
_theme-color.scss
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Copyright 2017 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
@use 'sass:color';
@use 'sass:list';
@use 'sass:map';
@use 'sass:math';
@use 'sass:meta';
@use 'sass:string';
@use './custom-properties';
@use './keys';
@function _linear-channel-value($channel-value) {
$normalized-channel-value: math.div($channel-value, 255);
@if $normalized-channel-value < 0.03928 {
@return math.div($normalized-channel-value, 12.92);
}
@return math.pow(math.div($normalized-channel-value + 0.055, 1.055), 2.4);
}
// Calculate the luminance for a color.
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
@function luminance($color) {
$red: _linear-channel-value(color.red($color));
$green: _linear-channel-value(color.green($color));
$blue: _linear-channel-value(color.blue($color));
@return 0.2126 * $red + 0.7152 * $green + 0.0722 * $blue;
}
// Calculate the contrast ratio between two colors.
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
@function contrast($back, $front) {
$backLum: luminance($back) + 0.05;
$foreLum: luminance($front) + 0.05;
@return math.div(math.max($backLum, $foreLum), math.min($backLum, $foreLum));
}
// Determine whether the color is 'light' or 'dark'.
@function tone($color) {
@if $color == 'dark' or $color == 'light' {
@return $color;
}
@if meta.type-of($color) != 'color' {
@warn '#{$color} is not a color. Falling back to "dark" tone.';
@return 'dark';
}
$minimumContrast: 3.1;
$lightContrast: contrast($color, white);
$darkContrast: contrast($color, rgba(black, 0.87));
@if ($lightContrast < $minimumContrast) and ($darkContrast > $lightContrast) {
@return 'light';
} @else {
@return 'dark';
}
}
// Determine whether to use dark or light text on top of given color to meet accessibility standards for contrast.
// Returns 'dark' if the given color is light and 'light' if the given color is dark.
@function contrast-tone($color) {
@return if(tone($color) == 'dark', 'light', 'dark');
}
///
/// @param $color Target color in any color format.
/// @return Returns hash in string format that uniquely represents
/// any given color format. Useful for generating unique keyframe names.
/// @example
/// `color-hash(#6200ee)` => "6200ee"
/// `color-hash(rgb(255, 112, 112))` => "ff7070"
/// `color-hash((varname: --my-fancy-color, fallback: teal))` => 'teal'
/// `color-hash((varname: --my-fancy-color, fallback: null))` => '--my-fancy-color'
///
@function color-hash($color) {
@if custom-properties.is-custom-prop($color) {
$color-value: custom-properties.get-fallback($color);
@if (custom-properties.is-custom-prop-string($color-value)) {
$varEndIndex: if(
string.index($color-value, ', '),
string.index($color-value, ', ') - 1,
-2
);
@return string.slice($color-value, 5, $varEndIndex);
}
@if (meta.type-of($color-value) == 'color') {
@return _get-hex-string($color-value);
}
@return custom-properties.get-varname($color);
}
@if meta.type-of($color) == 'string' {
@return $color;
}
@return _get-hex-string($color);
}
@function _get-hex-string($color) {
@return string.slice(color.ie-hex-str($color), 2); // Index starts at 1
}
//
// Main theme colors for your brand.
//
// If you're a user customizing your color scheme in SASS, these are probably the only variables you need to change.
//
$primary: #6200ee !default; // baseline purple, 500 tone
$on-primary: if(contrast-tone($primary) == 'dark', #000, #fff) !default;
// The $mdc-theme-accent variable is DEPRECATED - it exists purely for backward compatibility.
// The $mdc-theme-secondary* variables should be used for all new projects.
/// @deprecated - use $secondary
$accent: #018786 !default; // baseline teal, 600 tone
$secondary: $accent !default;
$on-secondary: if(contrast-tone($secondary) == 'dark', #000, #fff) !default;
$background: #fff !default; // White
$surface: #fff !default;
$on-surface: if(contrast-tone($surface) == 'dark', #000, #fff) !default;
$error: #b00020 !default;
$on-error: if(contrast-tone($error) == 'dark', #000, #fff) !default;
//
// Text colors according to light vs dark and text type.
//
$text-colors: (
dark: (
primary: rgba(black, 0.87),
secondary: rgba(black, 0.54),
hint: rgba(black, 0.38),
disabled: rgba(black, 0.38),
icon: rgba(black, 0.38),
),
light: (
primary: white,
secondary: rgba(white, 0.7),
hint: rgba(white, 0.5),
disabled: rgba(white, 0.5),
icon: rgba(white, 0.5),
),
) !default;
$text-emphasis: (
high: 0.87,
medium: 0.6,
disabled: 0.38,
) !default;
@function ink-color-for-fill_($text-style, $fill-color) {
$contrast-tone: contrast-tone($fill-color);
@return map.get(map.get($text-colors, $contrast-tone), $text-style);
}
//
// Primary text colors for each of the theme colors.
//
/// @deprecated Use individual variables (`$primary`, `$secondary`). Do not
/// override this Map of variables.
$property-values: (
primary: $primary,
secondary: $secondary,
background: $background,
surface: $surface,
error: $error,
on-primary: $on-primary,
on-secondary: $on-secondary,
on-surface: $on-surface,
on-error: $on-error,
text-primary-on-background: ink-color-for-fill_(primary, $background),
text-secondary-on-background: ink-color-for-fill_(secondary, $background),
text-hint-on-background: ink-color-for-fill_(hint, $background),
text-disabled-on-background: ink-color-for-fill_(disabled, $background),
text-icon-on-background: ink-color-for-fill_(icon, $background),
text-primary-on-light: ink-color-for-fill_(primary, light),
text-secondary-on-light: ink-color-for-fill_(secondary, light),
text-hint-on-light: ink-color-for-fill_(hint, light),
text-disabled-on-light: ink-color-for-fill_(disabled, light),
text-icon-on-light: ink-color-for-fill_(icon, light),
text-primary-on-dark: ink-color-for-fill_(primary, dark),
text-secondary-on-dark: ink-color-for-fill_(secondary, dark),
text-hint-on-dark: ink-color-for-fill_(hint, dark),
text-disabled-on-dark: ink-color-for-fill_(disabled, dark),
text-icon-on-dark: ink-color-for-fill_(icon, dark),
) !default;
@include keys.set-values(
$property-values,
$options: (custom-property-prefix: theme)
);
// A copy of the property values Map that is used to detect compile-time changes
// for Angular support.
$_property-values-copy: $property-values;
/// Checks if the global $mdc-theme-property-values was dynamically changed at
/// compile time. Typically, $property-values is configured once, but a Sass
/// hack allows the variable to be changed multiple times and effectively
/// support dynamic values.
///
/// Angular uses this in their dynamic theming. This function checks if this
/// scenario has occurred and returns the current global value that should be
/// used instead of the key store value.
///
/// @deprecated The function should not be used externally. It will be removed
/// when $mdc-theme-property-values is fully deprecated and removed.
@function deprecated-get-global-theme-key-value-if-changed($key) {
// Determine if we need to use a compile-time updated value to support
// Angular.
$current-global-value: map.get($property-values, $key);
$configured-global-value: map.get($_property-values-copy, $key);
@if $current-global-value != $configured-global-value {
// $mdc-theme-property-values was changed at compile time. Return the new
// compile-time value.
@return (value: $current-global-value, changed: true);
}
@return (changed: false);
}
// @deprecated use theme.property(). If you need to ensure the value is not a
// custom property, use custom-properties.is-custom-prop() to check if the value
// is a custom prop, then custom-properties.get-fallback() to get its value.
// If `$style` is a color (a literal color value, `currentColor`, or a CSS custom property), it is returned verbatim.
// Otherwise, `$style` is treated as a theme property name, and the corresponding value from
// `$mdc-theme-property-values` is returned. If this also fails, an error is thrown.
//
// This is mainly useful in situations where `mdc-theme-prop` cannot be used directly (e.g., `box-shadow`).
//
// Examples:
//
// 1. mdc-theme-prop-value(primary) => "#6200ee"
// 2. mdc-theme-prop-value(blue) => 'blue'
//
// NOTE: This function must be defined in _variables.scss instead of _functions.scss to avoid circular imports.
@function prop-value($style) {
@if custom-properties.is-custom-prop($style) {
@return custom-properties.get-fallback($style);
}
@if is-valid-theme-prop-value_($style) {
@return $style;
}
@if is-theme-key($style) {
// Determine if we need to use a compile-time updated value to support
// Angular.
$result: deprecated-get-global-theme-key-value-if-changed($style);
@if map.get($result, changed) {
@return map.get($result, value);
}
}
@return keys.resolve($style);
}
// NOTE: This function must be defined in _variables.scss instead of _functions.scss to avoid circular imports.
@function accessible-ink-color($fill-color, $text-style: primary) {
$fill-color-value: prop-value($fill-color);
$color-map-for-tone: map.get($text-colors, contrast-tone($fill-color-value));
@if not map.has-key($color-map-for-tone, $text-style) {
@error "Invalid $text-style: '#{$text-style}'. Choose one of: #{map.keys($color-map-for-tone)}";
}
@return map.get($color-map-for-tone, $text-style);
}
// NOTE: This function is depended upon by mdc-theme-prop-value (above) and thus must be defined in this file.
@function is-valid-theme-prop-value_($style) {
@if $style == null {
@return false;
}
@return meta.type-of($style) == 'color' or $style == 'currentColor' or
string.slice($style, 1, 4) == 'var(' or $style == 'inherit' or $style ==
'transparent' or
// NOTE: `GrayText` is deprecated, but is the only feasible way to convey the
// correct high-contrast mode colors in alignment with Windows system colors.
$style == 'GrayText';
}
@function text-emphasis($emphasis) {
@return map.get($text-emphasis, $emphasis);
}
@function is-theme-key($style) {
@return map.has-key($property-values, $style);
}
@function get-theme-keys() {
@return map.keys($property-values);
}
///
/// @param {Color|String} Color property key name (i.e., `primary`, `secondary`,
/// etc).
/// @return Returns custom property map containing CSS custom property and
/// fallback value (i.e., (varname: ..., fallback: ...). Returns color if
/// valid color value is provided. Throws error otherwise.
/// @examples
/// 1. get-custom-property(primary)
/// => (varname: --mdc-theme-primary, fallback: #6200ee)
///
/// 2. get-custom-property(#fff)
/// => #fff
///
@function get-custom-property($color) {
$is-tokens-custom-prop: meta.type-of($color) == 'string' and
string.index($color, '--') != null;
@if custom-properties.is-custom-prop($color) or
$is-tokens-custom-prop or
is-valid-theme-prop-value_($color)
{
@return $color;
} @else if is-theme-key($color) {
$custom-prop: keys.create-custom-property($color);
// Determine if we need to use a compile-time updated value to support
// Angular.
$result: deprecated-get-global-theme-key-value-if-changed($color);
@if map.get($result, changed) {
$custom-prop: custom-properties.set-fallback(
$custom-prop,
map.get($result, value)
);
}
@return $custom-prop;
} @else {
@error "Invalid theme property: '#{$color}'. Choose one of: #{get-theme-keys()}";
}
}