Skip to content

Commit e3d5db4

Browse files
committed
Remove use of deprecated color functions, add rgb-[channel] functions
1 parent da7c2f8 commit e3d5db4

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/utils/color.scss

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
1-
@use "sass:color";
2-
@use "sass:list";
1+
@use 'sass:color';
2+
@use 'sass:list';
33
////
44
/// @group utils.color
55
////
66

77
@use 'math.scss' as *;
88
@use 'units.scss' as *;
99

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+
1037
///
1138
/// Produce a shade (a lighter or darker version) of a color based on the value of $shade and an
1239
/// optional darkest to lightest shade range.
@@ -69,9 +96,9 @@
6996
/// @example yiq(purple) // => 52.864
7097

7198
@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);
75102
$yiq: (($r * 299) + ($g * 587) + ($b * 114)) * 0.001;
76103

77104
// 0 (black) - 255 (white)
@@ -82,9 +109,9 @@
82109
/// https://www.w3.org/TR/WCAG20/#relativeluminancedef
83110
///
84111
@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);
88115

89116
$c: divide(0.055, 1.055);
90117

0 commit comments

Comments
 (0)