tests: gamut: Add sanity checks for all clip methods
diff --git a/src/commonTest/kotlin/dev/kdrag0n/colorkt/tests/GamutTests.kt b/src/commonTest/kotlin/dev/kdrag0n/colorkt/tests/GamutTests.kt
index c4328be..46ded09 100644
--- a/src/commonTest/kotlin/dev/kdrag0n/colorkt/tests/GamutTests.kt
+++ b/src/commonTest/kotlin/dev/kdrag0n/colorkt/tests/GamutTests.kt
@@ -13,8 +13,14 @@
import dev.kdrag0n.colorkt.ucs.lab.CieLab
import dev.kdrag0n.colorkt.ucs.lch.Oklch
import dev.kdrag0n.colorkt.conversion.ConversionGraph.convert
+import dev.kdrag0n.colorkt.gamut.LchGamut
+import dev.kdrag0n.colorkt.gamut.OklabGamut
+import dev.kdrag0n.colorkt.rgb.LinearSrgb
import kotlin.test.Test
import kotlin.test.assertFalse
+import kotlin.test.assertTrue
+
+private const val EPSILON = 0.001
class GamutTests {
private val cond = Zcam.ViewingConditions(
@@ -32,13 +38,19 @@
val srcLinear = src.toLinear()
val lch = src.convert<Oklch>()
- // Boost the chroma
+ // Boost the chroma and clip lightness
val clipped = lch.copy(chroma = lch.chroma * 5).toOklab().clipToLinearSrgb()
// Now check
assertApprox(clipped.r, srcLinear.r)
assertApprox(clipped.g, srcLinear.g)
assertApprox(clipped.b, srcLinear.b)
+
+ // Now test all the methods and make sure they're reasonable: not NaN, 0, or out-of-gamut
+ OklabGamut.ClipMethod.values().forEach { method ->
+ val clippedM = lch.copy(chroma = lch.chroma * 5).toOklab().clipToLinearSrgb(method)
+ assertInGamut(clippedM)
+ }
}
}
@@ -57,6 +69,12 @@
assertApprox(clipped.r, srcLinear.r)
assertApprox(clipped.g, srcLinear.g)
assertApprox(clipped.b, srcLinear.b)
+
+ // Now test all the methods and make sure they're reasonable: not NaN, 0, or out-of-gamut
+ LchGamut.ClipMethod.values().forEach { method ->
+ val clippedM = zcam.copy(chroma = zcam.chroma * 5).clipToLinearSrgb(method)
+ assertInGamut(clippedM)
+ }
}
}
@@ -74,4 +92,14 @@
assertFalse { clipped.g.isNaN() }
assertFalse { clipped.b.isNaN() }
}
+
+ private fun assertInGamut(rgb: LinearSrgb) {
+ assertInGamut(rgb.r)
+ assertInGamut(rgb.g)
+ assertInGamut(rgb.b)
+ }
+
+ private fun assertInGamut(component: Double) {
+ assertTrue(component in (0.0 - EPSILON)..(1.0 + EPSILON), "$component is out of gamut")
+ }
}