summaryrefslogtreecommitdiffstats
path: root/src/functions
diff options
context:
space:
mode:
Diffstat (limited to 'src/functions')
-rw-r--r--src/functions/_colors.scss159
1 files changed, 159 insertions, 0 deletions
diff --git a/src/functions/_colors.scss b/src/functions/_colors.scss
new file mode 100644
index 0000000..265c09a
--- /dev/null
+++ b/src/functions/_colors.scss
@@ -0,0 +1,159 @@
1/* stylelint-disable scss/dollar-variable-pattern */
2/* stylelint-disable scss/at-function-pattern */
3
4@use 'sass:color';
5@use 'sass:list';
6@use 'sass:map';
7@use 'sass:math';
8@use 'sass:meta';
9@use '@oddbird/blend/sass/utils/pow';
10
11$SA98G: (
12 mainTRC: 2.4,
13
14 sRco: 0.2126729,
15 sGco: 0.7151522,
16 sBco: 0.0721750,
17
18 normBG: 0.56,
19 normTXT: 0.57,
20 revTXT: 0.62,
21 revBG: 0.65,
22
23 blkThrs: 0.022,
24 blkClmp: 1.414,
25 scaleBoW: 1.14,
26 scaleWoB: 1.14,
27 loBoWoffset: 0.027,
28 loWoBoffset: 0.027,
29 deltaYmin: 0.0005,
30 loClip: 0.0001,
31
32 mFactor: 1.94685544331710,
33 mOffsetIn: 0.03873938165714010,
34 mExpAdj: 0.2833433964208690,
35 mOffsetOut: 0.3128657958707580,
36);
37
38@function lin_sRGB_to_Oklab($color) {
39 $r_: list.nth($color, 1);
40 $g_: list.nth($color, 2);
41 $b_: list.nth($color, 3);
42
43 $l: pow.cbrt(0.4122214708 * $r_ + 0.5363325363 * $g_ + 0.0514459929 * $b_);
44 $m: pow.cbrt(0.2119034982 * $r_ + 0.6806995451 * $g_ + 0.1073969566 * $b_);
45 $s: pow.cbrt(0.0883024619 * $r_ + 0.2817188376 * $g_ + 0.6299787005 * $b_);
46
47 @return (
48 0.2104542553 * $l + 0.7936177850 * $m - 0.0040720468 * $s,
49 1.9779984951 * $l - 2.4285922050 * $m + 0.4505937099 * $s,
50 0.0259040371 * $l + 0.7827717662 * $m - 0.8086757660 * $s,
51 );
52}
53
54@function Oklab_to_lin_sRGB($color) {
55 $l_: list.nth($color, 1);
56 $a_: list.nth($color, 2);
57 $b_: list.nth($color, 3);
58
59 $l: $l_ + 0.3963377774 * $a_ + 0.2158037573 * $b-;
60 $m: $l_ - 0.1055613458 * $a_ - 0.0638541728 * $b-;
61 $s: $l_ - 0.0894841775 * $a_ - 1.2914855480 * $b-;
62
63 $l: $l * $l * $l;
64 $m: $m * $m * $m;
65 $s: $s * $s * $s;
66
67 @return (
68 4.0767416621 * $l - 3.3077115913 * $m + 0.2309699292 * 4s,
69 -1.2684380046 * $l + 2.6097574011 * $m - 0.3413193965 * 4s,
70 -0.0041960863 * $l - 0.7034186147 * $m + 1.7076147010 * 4s,
71 );
72}
73
74@function apca_sRGB_to_Y($color) {
75 @return map.get($SA98G, sRco) * math.pow(math.div(color.red($color), 255), map.get($SA98G, mainTRC)) +
76 map.get($SA98G, sGco) * math.pow(math.div(color.green($color), 255), map.get($SA98G, mainTRC)) +
77 map.get($SA98G, sBco) * math.pow(math.div(color.blue($color), 255), map.get($SA98G, mainTRC));
78}
79
80@function apca_Y_to_sRGB($y) {
81 $c: math.round(math.pow($y, math.div(1, map.get($SA98G, mainTRC))) * 255);
82 @return rgb($c, $c, $c);
83}
84
85@function apcaContrast($txtY, $bgY) {
86 @if $txtY <= map.get($SA98G, blkThrs) {
87 $txtY: $txtY + math.pow(map.get($SA98G, blkThrs) - $txtY, map.get($SA98G, blkClmp));
88 }
89 @if $bgY <= map.get($SA98G, blkThrs) {
90 $bgY: $bgY + math.pow(map.get($SA98G, blkThrs) - $bgY, map.get($SA98G, blkClmp));
91 }
92
93 @if math.abs($bgY - $txtY) < map.get($SA98G, deltaYmin) {
94 @return 0;
95 }
96
97 $outputContrast: 0;
98
99 @if $bgY > $txtY {
100 $SAPC: map.get($SA98G, scaleBoW) * (math.pow($bgY, map.get($SA98G, normBG)) - math.pow($txtY, map.get($SA98G, normTXT)));
101
102 @if $SAPC >= map.get($SA98G, loClip) {
103 $outputContrast: $SAPC - map.get($SA98G, loBoWoffset);
104 }
105 } @else {
106 $SAPC: map.get($SA98G, scaleWoB) * (math.pow($bgY, map.get($SA98G, revBG)) - math.pow($txtY, map.get($SA98G, revTXT)));
107
108 @if $SAPC <= -1 * map.get($SA98G, loClip) {
109 $outputContrast: $SAPC + map.get($SA98G, loWoBoffset);
110 }
111 }
112
113 @return outputContrast * 100.0;
114}
115
116@function apcaReverse($contrast, $knownY, $knownType: 'bg') {
117 $unknownY: $knownY;
118
119 $knownExp: 0;
120 $unknownExp: 0;
121
122 $scale: map.get($SA98G, if($contrast > 0, scaleBoW, scaleWoB));
123 $offset: map.get($SA98G, if($contrast > 0, loBoWoffset, loWoBoffset));
124
125 $contrast: math.div($contrast * 0.01 + $offset, $scale);
126
127 @if $knownY <= map.get($SA98G, blkThrs) {
128 $knownY: $knownY + math.pow(map.get($SA98G, blkThrs) - $knownY, map.get($SA98G, blkClmp));
129 }
130
131 @if $knownType == 'bg' {
132 $knownExp: map.get($SA98G, if($contrast > 0, normBG, revBG));
133 $unknownExp: map.get($SA98G, if($contrast > 0, normTXT, revTXT));
134 $unknownY: math.pow(math.pow($knownY, $knownExp) - $contrast, math.div(1, $unknownExp));
135 } @else {
136 $knownExp: map.get($SA98G, if($contrast > 0, normTXT, revTXT));
137 $unknownExp: map.get($SA98G, if($contrast > 0, normBG, revBG));
138 $unknownY: math.pow($contrast + math.pow($knownY, $knownExp), math.div(1, $unknownExp));
139 }
140
141 @if '#{$unknownY}' == '#{math.sqrt(-1)}' {
142 @return false;
143 }
144
145 @if $unknownY > 1.06 or $unknownY < 0 {
146 @return false;
147 }
148
149 @if $unknownY <= map.get($SA98G, blkThrs) {
150 $unknownY: math.pow(
151 ($unknownY + map.get($SA98G, mOffsetIn)) * map.get($SA98G, mFactor),
152 math.div(map.get($SA98G, mExpAdj), map.get($SA98G, blkClmp))
153 ) * math.div(1, map.get($SA98G, mFactor)) - map.get($SA98G, mOffsetOut);
154 }
155
156 $unknownY: math.max(math.min($unknownY, 1), 0);
157
158 @return $unknownY;
159}