|
79 | 79 |
|
80 | 80 | @return var(#{$var}, $fallback);
|
81 | 81 | }
|
| 82 | + |
| 83 | +/// |
| 84 | +/// @param $color Target color in any color format. |
| 85 | +/// @return Returns hash in string format that uniquely represents |
| 86 | +/// any given color format. Useful for generating unique keyframe names. |
| 87 | +/// @example |
| 88 | +/// `color-hash(#6200ee)` => "6200ee" |
| 89 | +/// `color-hash(rgb(255, 112, 112))` => "ff7070" |
| 90 | +/// `color-hash(var(--my-fancy-color))` => "--my-fancy-color" |
| 91 | +/// |
| 92 | +@function mdc-theme-color-hash($color) { |
| 93 | + @if mdc-theme-is-var-with-fallback_($color) { |
| 94 | + $color: map-get($color, "fallback"); |
| 95 | + } |
| 96 | + |
| 97 | + @if mdc-theme-is-css-var_($color) { |
| 98 | + @return mdc-theme-get-css-varname_($color); |
| 99 | + } |
| 100 | + |
| 101 | + @if type-of($color) == "string" { |
| 102 | + @return $color; |
| 103 | + } |
| 104 | + |
| 105 | + @return str-slice(ie-hex-str($color), 2); // Index starts at 1 |
| 106 | +} |
| 107 | + |
| 108 | +/// |
| 109 | +/// @return Returns true if given color is set to CSS variable. |
| 110 | +/// @access Private |
| 111 | +/// |
| 112 | +@function mdc-theme-is-css-var_($color) { |
| 113 | + @return str_slice(inspect($color), 1, 4) == "var("; |
| 114 | +} |
| 115 | + |
| 116 | +/// |
| 117 | +/// @return Returns CSS variable name from color value in CSS variable format. |
| 118 | +/// Returns original color value if not in CSS variable format. |
| 119 | +/// @example |
| 120 | +/// mdc-theme-get-css-varname_(var(--my-fancy-color, #4CAF50)) => --my-fancy-color |
| 121 | +/// mdc-theme-get-css-varname_(var(--my-fancy-color)) => --my-fancy-color |
| 122 | +/// @access Private |
| 123 | +/// |
| 124 | +@function mdc-theme-get-css-varname_($color) { |
| 125 | + @if not mdc-theme-is-css-var_($color) { |
| 126 | + @return $color; |
| 127 | + } |
| 128 | + |
| 129 | + $color: inspect($color); |
| 130 | + |
| 131 | + $var-start-index: str-index($color, "--"); |
| 132 | + $color: str_slice($color, $var-start-index); |
| 133 | + $var-end-index: str-index($color, ",") or str-index($color, ")"); |
| 134 | + $color: str_slice($color, 0, $var-end-index - 1); |
| 135 | + |
| 136 | + @return $color; |
| 137 | +} |
0 commit comments