|
1 |
| -@use "sass:color"; |
2 |
| -@use "sass:list"; |
| 1 | +@use 'sass:color'; |
| 2 | +@use 'sass:list'; |
3 | 3 | ////
|
4 | 4 | /// @group utils.color
|
5 | 5 | ////
|
6 | 6 |
|
7 | 7 | @use 'math.scss' as *;
|
8 | 8 | @use 'units.scss' as *;
|
9 | 9 |
|
| 10 | +/// Return the red channel of a particular rgb color as either a value between 0 and 255 or as a float between 0 and 1. |
| 11 | +/// @param {*} $color The color to be evaluated |
| 12 | +/// @param {*} $as-float [false] If true, return the value as a float between 0 and 1 |
| 13 | + |
| 14 | +@function rgb-red($color, $as-float: false) { |
| 15 | + $v: color.channel($color, 'red', $space: rgb); |
| 16 | + @return if($as-float, divide($v, 255), $v); |
| 17 | +} |
| 18 | + |
| 19 | +/// Return the green channel of a particular rgb color as either a value between 0 and 255 or as a float between 0 and 1. |
| 20 | +/// @param {*} $color The color to be evaluated |
| 21 | +/// @param {*} $as-float [false] If true, return the value as a float between 0 and 1 |
| 22 | + |
| 23 | +@function rgb-green($color, $as-float: false) { |
| 24 | + $v: color.channel($color, 'green', $space: rgb); |
| 25 | + @return if($as-float, divide($v, 255), $v); |
| 26 | +} |
| 27 | + |
| 28 | +/// Return the blue channel of a particular rgb color as either a value between 0 and 255 or as a float between 0 and 1. |
| 29 | +/// @param {*} $color The color to be evaluated |
| 30 | +/// @param {*} $as-float [false] If true, return the value as a float between 0 and 1 |
| 31 | + |
| 32 | +@function rgb-blue($color, $as-float: false) { |
| 33 | + $v: color.channel($color, 'blue', $space: rgb); |
| 34 | + @return if($as-float, divide($v, 255), $v); |
| 35 | +} |
| 36 | + |
10 | 37 | ///
|
11 | 38 | /// Produce a shade (a lighter or darker version) of a color based on the value of $shade and an
|
12 | 39 | /// optional darkest to lightest shade range.
|
|
69 | 96 | /// @example yiq(purple) // => 52.864
|
70 | 97 |
|
71 | 98 | @function yiq($color) {
|
72 |
| - $r: color.red($color); |
73 |
| - $g: color.green($color); |
74 |
| - $b: color.blue($color); |
| 99 | + $r: rgb-red($color); |
| 100 | + $g: rgb-green($color); |
| 101 | + $b: rgb-blue($color); |
75 | 102 | $yiq: (($r * 299) + ($g * 587) + ($b * 114)) * 0.001;
|
76 | 103 |
|
77 | 104 | // 0 (black) - 255 (white)
|
|
82 | 109 | /// https://www.w3.org/TR/WCAG20/#relativeluminancedef
|
83 | 110 | ///
|
84 | 111 | @function luminance($color) {
|
85 |
| - $R: divide(color.red($color), 255); |
86 |
| - $G: divide(color.green($color), 255); |
87 |
| - $B: divide(color.blue($color), 255); |
| 112 | + $R: rgb-red($color, true); |
| 113 | + $G: rgb-green($color, true); |
| 114 | + $B: rgb-blue($color, true); |
88 | 115 |
|
89 | 116 | $c: divide(0.055, 1.055);
|
90 | 117 |
|
|
0 commit comments