This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.scss
268 lines (233 loc) · 7.6 KB
/
index.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
@use 'sass:color';
@use 'sass:list';
@use 'sass:math';
@use 'sass:map';
@use 'sass:meta';
@use 'sass:string';
$ONE_THIRD: math.div(1, 3);
/// Mix colors through the Oklab space
///
/// @param {color} $color1
/// @param {color} $color2
/// @param {float} $weight [0.5] Mix strength (`0` = $color1, `0.5` = 50–50, `1` = $color2)
/// @returns {color} Mix result
@function mix($color1, $color2, $weight: 0.5) {
@if $weight == 0 {
@return $color1;
}
@if $weight == 1 {
@return $color2;
}
$w1: 1 - $weight;
$w2: $weight;
$oklab1: rgbToOklab($color1);
$oklab2: rgbToOklab($color2);
$labmix: (
'l': map.get($oklab1, 'l') * $w1 + map.get($oklab2, 'l') * $w2,
'a': map.get($oklab1, 'a') * $w1 + map.get($oklab2, 'a') * $w2,
'b': map.get($oklab1, 'b') * $w1 + map.get($oklab2, 'b') * $w2,
);
$rgb: oklabToRGB($labmix);
@return rgba($rgb, color.alpha($color1) * $w1 + color.alpha($color2) * $w2); // don’t forget the alpha channel!
}
/// Convert sRGB to Oklab
///
/// @param {color} $color
/// @returns {map} Map of l, a, b, alpha
@function rgbToOklab($color) {
// sRGB -> Linear RGB D65
$red: __degamma(math.div(color.red($color), 255));
$green: __degamma(math.div(color.green($color), 255));
$blue: __degamma(math.div(color.blue($color), 255));
$alpha: color.alpha($color);
// Linear RGB D65 -> LMS
$lms: (
'l': 0.4122214708 * $red + 0.5363325363 * $green + 0.0514459929 * $blue,
'm': 0.2119034982 * $red + 0.6806995451 * $green + 0.1073969566 * $blue,
's': 0.0883024619 * $red + 0.2817188376 * $green + 0.6299787005 * $blue,
);
// LMS -> Oklab
$l: 0.2104542553 * __cbrt(map.get($lms, 'l')) + 0.793617785 * __cbrt(map.get($lms, 'm')) - 0.0040720468 * __cbrt(map.get($lms, 's'));
$a: 1.9779984951 * __cbrt(map.get($lms, 'l')) - 2.428592205 * __cbrt(map.get($lms, 'm')) + 0.4505937099 * __cbrt(map.get($lms, 's'));
$b: 0.0259040371 * __cbrt(map.get($lms, 'l')) + 0.7827717662 * __cbrt(map.get($lms, 'm')) - 0.808675766 * __cbrt(map.get($lms, 's'));
@return ('l': $l, 'a': $a, 'b': $b, alpha: $alpha);
}
/// Convert Oklab map to sRGB
///
/// @param {map} Map of l, a, b
/// @returns {color} sRGB color
@function oklabToRGB($oklab) {
@if meta.type-of($oklab) != 'map' {
@error "Must provide map of \"l\", \"a\", \"b\" values";
}
// Oklab -> LMS
$lms: (
'l': math.pow(map.get($oklab, 'l') + 0.39633779217376774 * map.get($oklab, 'a') + 0.2158037580607588 * map.get($oklab, 'b'), 3),
'm': math.pow(map.get($oklab, 'l') - 0.10556134232365633 * map.get($oklab, 'a') - 0.0638541747717059 * map.get($oklab, 'b'), 3),
's': math.pow(map.get($oklab, 'l') - 0.08948418209496574 * map.get($oklab, 'a') - 1.2914855378640917 * map.get($oklab, 'b'), 3),
);
// LMS -> Linear RGB D65
$r: math.max(4.0767416621 * map.get($lms, 'l') - 3.3077115913 * map.get($lms, 'm') + 0.2309699292 * map.get($lms, 's'), 0);
$g: math.max(-1.2684380046 * map.get($lms, 'l') + 2.6097574011 * map.get($lms, 'm') - 0.3413193965 * map.get($lms, 's'), 0);
$b: math.max(-0.0041960863 * map.get($lms, 'l') - 0.7034186147 * map.get($lms, 'm') + 1.707614701 * map.get($lms, 's'), 0);
$alpha: 1;
@if map.has-key($oklab, 'alpha') {
$alpha: map.get($oklab, 'alpha');
}
// Linear RGB D65 -> sRGB
@return rgba(__gamma($r) * 255, __gamma($g) * 255, __gamma($b) * 255, $alpha);
}
$ε: 0.0002;
/// Convert RGB to Oklch map
/// @param {color} $color
/// @returns {map} Map of l, c, h, alpha
@function rgbToOklch($color) {
$oklab: rgbToOklab($color);
$l: map.get($oklab, 'l');
$a: map.get($oklab, 'a');
$b: map.get($oklab, 'b');
$alpha: 1;
@if (map.has-key($oklab, 'alpha')) {
$alpha: map.get($oklab, 'alpha');
}
$h: 0;
@if (math.abs($a) >= $ε or math.abs($b) >= $ε) {
$h: __unitless(math.atan2($b, $a));
}
@while ($h < 0) {
$h: $h + 360;
}
@while ($h >= 360) {
$h: $h - 360;
}
@return (l: $l, c: math.sqrt(math.pow($a, 2) + math.pow($b, 2)), h: $h, alpha: $alpha);
}
/// Convert Oklch map to sRGB
///
/// @param {map} Map of l, c, h
/// @returns {color} sRGB color
@function oklchToRGB($oklch) {
@if meta.type-of($oklch) != 'map' {
@error "Must provide map of \"l\", \"c\", \"h\" values";
}
$l: map.get($oklch, 'l');
$c: map.get($oklch, 'c');
$h: map.get($oklch, 'h');
$alpha: 1;
@if (map.has-key($oklch, 'alpha')) {
$alpha: map.get($oklch, 'alpha');
}
// if lightness is 0, return pure black
@if ($l < $ε) {
@return rgba(0, 0, 0, $alpha);
}
@while ($h < 0) {
$h: $h + 360;
}
@while ($h >= 360) {
$h: $h - 360;
}
$a: math.cos(__degToRad($h)) * $c;
$b: math.sin(__degToRad($h)) * $c;
@return oklabToRGB(
(
l: $l,
a: $a,
b: $b,
alpha: $alpha,
)
);
}
/// Relatively lighten color via Oklab. Produces better results than Sass’ lighten().
///
/// @param {color} $color
/// @param {float} $weight [0.5] Lightness strength (`0` = $color; `1` = white; `-1` = black)
/// @returns {color} Lightened color
@function lighten($color, $weight: 0.5) {
@if $weight >= 0 {
@return mix($color, #fff, $weight);
} @else {
@return darken($color, -$weight);
}
}
/// Relatively lighten color via Oklab. Produces better results than Sass’ lighten().
///
/// @param {color} $color
/// @param {float} $weight [0.5] Darkness strength (`0` = $color; `1` = black; `-1` = white)
/// @returns {color} Darkened color
@function darken($color, $weight: 0.5) {
@if $weight >= 0 {
@return mix($color, #000, $weight);
} @else {
@return lighten($color, -$weight);
}
}
/// Turn any color into P3 (supporting browsers only)
/// (https://webkit.org/blog/10042/wide-gamut-color-in-css-with-display-p3/)
///
/// @param {color} $color
/// @returns {string} P3 color string
@function p3($color) {
$r: math.div(color.red($color), 255);
$g: math.div(color.green($color), 255);
$b: math.div(color.blue($color), 255);
$a: color.alpha($color);
@if $a == 1 {
@return #{color(display-p3 $r $g $b)};
} @else {
@return #{color(display-p3 $r $g $b/$a)};
}
}
/// Get perceived lightness of a color. Produces far better results than HSL.
///
/// @param {color} $color
/// @returns {float} Perceived lightness from `0` (dark) to `1`
@function lightness($color) {
$oklab: rgbToOklab($color);
@return map.get($oklab, 'l');
}
/// Quickly generate fallbacks for a CSS property. Useful for experimental syntax and partial support.
/// ex: `@include better.fallback(color, better.p3(#c4432b), #c4432b);`
///
/// @param {string} $property - The CSS property
/// @param {string} $values... - Specify desired values in decreasing order of preference
@mixin fallback($property, $values...) {
@if list.length($values) < 2 {
@error "Must specify at least 2 values for fallback()";
}
@for $i from list.length($values) through 1 {
#{$property}: list.nth($values, $i);
}
}
// utils
/// convert sRGB to Linear RGB D65
@function __degamma($value) {
@if $value <= 0.04045 {
@return math.div($value, 12.92);
}
@return math.pow(math.div($value + 0.055, 1.055), 2.4);
}
/// convert Linear RGB D65 to sRGB
@function __gamma($value) {
@if $value <= 0.0031308 {
@return math.clamp($value * 12.92, 0, 1);
}
@return math.clamp(1.055 * math.pow($value, math.div(1, 2.4)) - 0.055, 0, 1);
}
/// Calculate cube root of a number (allows negative numbers, which Sass doesn’t allow)
@function __cbrt($num) {
$cbrt: math.pow(math.abs($num), $ONE_THIRD);
@if $num < 0 {
@return -1 * $cbrt;
} @else {
@return $cbrt;
}
}
/// Degrees -> radians
@function __degToRad($degrees) {
@return $degrees * math.div(math.$pi, 180);
}
/// Remove units
@function __unitless($number) {
@return math.div($number, ($number * 0 + 1));
}