summaryrefslogtreecommitdiffstats
path: root/src/functions/colors/_oklch.scss
blob: e3df041c78469ee8113a9ade5099f4b4e07b17d4 (plain) (blame)
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
/* stylelint-disable scss/dollar-variable-pattern */
/* stylelint-disable scss/at-function-pattern */

@use 'sass:list';
@use 'sass:math';
@use 'sass:meta';
@use 'sass:string';
@use '@oddbird/blend/sass/convert' as blend-convert;
@use '@oddbird/blend/sass/utils/pow';
@use 'iro-sass/src/index' as iro;

@function lin_sRGB_to_Oklab($color) {
    $r_: list.nth($color, 1);
    $g_: list.nth($color, 2);
    $b_: list.nth($color, 3);

    $l: pow.cbrt(0.4122214708 * $r_ + 0.5363325363 * $g_ + 0.0514459929 * $b_);
    $m: pow.cbrt(0.2119034982 * $r_ + 0.6806995451 * $g_ + 0.1073969566 * $b_);
	$s: pow.cbrt(0.0883024619 * $r_ + 0.2817188376 * $g_ + 0.6299787005 * $b_);
    
    @return (
        0.2104542553 * $l + 0.7936177850 * $m - 0.0040720468 * $s,
        1.9779984951 * $l - 2.4285922050 * $m + 0.4505937099 * $s,
        0.0259040371 * $l + 0.7827717662 * $m - 0.8086757660 * $s,
    );
}

@function Oklab_to_lin_sRGB($color) {
    $l_: list.nth($color, 1);
    $a_: list.nth($color, 2);
    $b_: list.nth($color, 3);

    $l: $l_ + 0.3963377774 * $a_ + 0.2158037573 * $b_;
    $m: $l_ - 0.1055613458 * $a_ - 0.0638541728 * $b_;
    $s: $l_ - 0.0894841775 * $a_ - 1.2914855480 * $b_;

    $l: $l * $l * $l;
    $m: $m * $m * $m;
    $s: $s * $s * $s;
    
    @return (
		 4.0767416621 * $l - 3.3077115913 * $m + 0.2309699292 * $s,
		-1.2684380046 * $l + 2.6097574011 * $m - 0.3413193965 * $s,
		-0.0041960863 * $l - 0.7034186147 * $m + 1.7076147010 * $s,
    );
}

@function oklch($arg) {
    $l: math.div(list.nth($arg, 1), 100%);
    $c: list.nth($arg, 2);
    $h: list.nth($arg, 3);

    @return blend-convert.rgbToSass(
        blend-convert.gam_sRGB(
            Oklab_to_lin_sRGB(
                blend-convert.LCH_to_Lab($l $c $h)
            )
        )
    );
}

@function parse-oklch($color) {
    @if meta.type-of($color) == 'color' {
        @return blend-convert.Lab_to_LCH(
            lin_sRGB_to_Oklab(
                blend-convert.lin_sRGB(
                    blend-convert.sassToRgb($color)
                )
            )
        );
    }

    @if meta.type-of($color) != 'string' {
        @return null;
    }

    @if string.slice($color, 1, 6) == 'oklch(' {
        $args: string.split(string.slice($color, 7, -2), ' ');

        $l: math.div(iro.fn-to-number(list.nth($args, 1)), 100%);
        $c: iro.fn-to-number(list.nth($args, 2));
        $h: iro.fn-to-number(list.nth($args, 3));

        @return $l $c $h;
    }

    @return null;
}