ucs: Use cube and square helpers
diff --git a/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/CieLab.kt b/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/CieLab.kt
index 074987d..79471bc 100644
--- a/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/CieLab.kt
+++ b/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/CieLab.kt
@@ -5,6 +5,7 @@
import dev.kdrag0n.colorkt.util.ConversionGraph
import dev.kdrag0n.colorkt.util.ConversionProvider
import dev.kdrag0n.colorkt.util.cbrt
+import dev.kdrag0n.colorkt.util.cube
import kotlin.math.pow
/**
@@ -51,7 +52,7 @@
}
private fun fInv(x: Double) = if (x > 6.0/29.0) {
- x.pow(3)
+ cube(x)
} else {
(108.0/841.0) * (x - 4.0/29.0)
}
diff --git a/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/Srlab2.kt b/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/Srlab2.kt
index e2ba965..ed6787c 100644
--- a/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/Srlab2.kt
+++ b/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/lab/Srlab2.kt
@@ -4,7 +4,7 @@
import dev.kdrag0n.colorkt.util.ConversionGraph
import dev.kdrag0n.colorkt.util.ConversionProvider
import dev.kdrag0n.colorkt.util.cbrt
-import kotlin.math.pow
+import dev.kdrag0n.colorkt.util.cube
/**
* A color in the SRLAB2 uniform color space, which represents colors in [dev.kdrag0n.colorkt.ucs.lab.Lab] form.
@@ -30,9 +30,9 @@
val y2 = 0.01 * L - 0.000533159 * a - 0.000269178 * b
val z2 = 0.01 * L - 0.005800000 * b
- val x = cube(x2)
- val y = cube(y2)
- val z = cube(z2)
+ val x = cielabFInv(x2)
+ val y = cielabFInv(y2)
+ val z = cielabFInv(z2)
return LinearSrgb(
r = 5.435679 * x - 4.599131 * y + 0.163593 * z,
@@ -47,16 +47,16 @@
ConversionGraph.add<Srlab2, LinearSrgb> { it.toLinearSrgb() }
}
- private fun root(x: Double) = if (x <= 216.0 / 24389.0) {
+ private fun cielabF(x: Double) = if (x <= 216.0 / 24389.0) {
x * 24389.0 / 2700.0
} else {
1.16 * cbrt(x) - 0.16
}
- private fun cube(x: Double) = if (x <= 0.08) {
+ private fun cielabFInv(x: Double) = if (x <= 0.08) {
x * 2700.0 / 24389.0
} else {
- ((x + 0.16) / 1.16).pow(3)
+ cube((x + 0.16) / 1.16)
}
/**
@@ -70,9 +70,9 @@
val y = 0.161987 * r + 0.756636 * g + 0.081376 * b
val z = 0.017228 * r + 0.108660 * g + 0.874112 * b
- val x2 = root(x)
- val y2 = root(y)
- val z2 = root(z)
+ val x2 = cielabF(x)
+ val y2 = cielabF(y)
+ val z2 = cielabF(z)
return Srlab2(
L = 37.0950 * x2 + 62.9054 * y2 - 0.0008 * z2,
diff --git a/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/polar/Lch.kt b/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/polar/Lch.kt
index 36f21fb..12b1c10 100644
--- a/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/polar/Lch.kt
+++ b/src/commonMain/kotlin/dev/kdrag0n/colorkt/ucs/polar/Lch.kt
@@ -4,6 +4,7 @@
import dev.kdrag0n.colorkt.util.toDegrees
import dev.kdrag0n.colorkt.util.toRadians
import dev.kdrag0n.colorkt.ucs.lab.Lab
+import dev.kdrag0n.colorkt.util.square
import kotlin.math.*
/**
@@ -38,7 +39,7 @@
return doubleArrayOf(
L,
- sqrt(a.pow(2) + b.pow(2)),
+ sqrt(square(a) + square(b)),
// Normalize the angle, as many will be negative
if (hDeg < 0) hDeg + 360 else hDeg,
)