@@ -37,9 +37,9 @@ class LmsColorSpace extends ColorSpace {
37
37
switch (dest) {
38
38
case ColorSpace .oklab:
39
39
// Algorithm from https://drafts.csswg.org/css-color-4/#color-conversion-code
40
- var longScaled = math. pow (long, 1 / 3 );
41
- var mediumScaled = math. pow (medium, 1 / 3 );
42
- var shortScaled = math. pow (short, 1 / 3 );
40
+ var longScaled = _cubeRootPreservingSign (long);
41
+ var mediumScaled = _cubeRootPreservingSign (medium);
42
+ var shortScaled = _cubeRootPreservingSign (short);
43
43
var lightness = lmsToOklab[0 ] * longScaled +
44
44
lmsToOklab[1 ] * mediumScaled +
45
45
lmsToOklab[2 ] * shortScaled;
@@ -62,9 +62,9 @@ class LmsColorSpace extends ColorSpace {
62
62
// This is equivalent to converting to OKLab and then to OKLCH, but we
63
63
// do it inline to avoid extra list allocations since we expect
64
64
// conversions to and from OKLCH to be very common.
65
- var longScaled = math. pow (long, 1 / 3 );
66
- var mediumScaled = math. pow (medium, 1 / 3 );
67
- var shortScaled = math. pow (short, 1 / 3 );
65
+ var longScaled = _cubeRootPreservingSign (long);
66
+ var mediumScaled = _cubeRootPreservingSign (medium);
67
+ var shortScaled = _cubeRootPreservingSign (short);
68
68
return labToLch (
69
69
dest,
70
70
lmsToOklab[0 ] * longScaled +
@@ -83,6 +83,11 @@ class LmsColorSpace extends ColorSpace {
83
83
}
84
84
}
85
85
86
+ /// Returns the cube root of the absolute value of [number] with the same sign
87
+ /// as [number] .
88
+ double _cubeRootPreservingSign (double number) =>
89
+ math.pow (number.abs (), 1 / 3 ) * number.sign;
90
+
86
91
@protected
87
92
double toLinear (double channel) => channel;
88
93
0 commit comments