@@ -31,56 +31,22 @@ fun Color.deltaE(other: Color): Double {
31
31
* Convert the color to the CIE XYZ color space within nominal range of [0.0, 1.0]
32
32
* using sRGB color space and D65 white reference white
33
33
*/
34
- private fun Color.toXYZ (): DoubleArray {
35
- // Values must be within nominal range of [0.0, 1.0]
36
- //
37
- var r = AndroidColor .red(this .value) / 255.0
38
- var g = AndroidColor .green(this .value) / 255.0
39
- var b = AndroidColor .blue(this .value) / 255.0
40
-
41
- // Inverse sRGB companding
42
- //
43
- r = if (r <= 0.04045 ) r / 12.92 else ((r + 0.055 ) / 1.055 ).pow(2.4 )
44
- g = if (g <= 0.04045 ) g / 12.92 else ((g + 0.055 ) / 1.055 ).pow(2.4 )
45
- b = if (b <= 0.04045 ) b / 12.92 else ((b + 0.055 ) / 1.055 ).pow(2.4 )
46
-
47
- // Linear RGB to XYZ using sRGB color space and D65 white reference white
48
- //
49
- return doubleArrayOf(
50
- 0.4124564 * r + 0.3575761 * g + 0.1804375 * b,
51
- 0.2126729 * r + 0.7151522 * g + 0.0721750 * b,
52
- 0.0193339 * r + 0.1191920 * g + 0.9503041 * b
53
- )
54
- }
55
-
56
34
/* *
57
35
* Convert the color to the CIE LAB color space using sRGB color space and D65 white reference white
58
36
*/
59
- private fun Color.toLAB (): DoubleArray {
60
- val (x, y, z) = this .toXYZ()
61
-
62
- // CIE standard illuminant D65
63
- //
64
- val Xr = 0.9504
65
- val Yr = 1.000
66
- val Zr = 1.0888
67
-
68
- val xr = x / Xr
69
- val yr = y / Yr
70
- val zr = z / Zr
71
-
72
- val e = 0.008856
73
- val k = 903.3
74
-
75
- val fx = if (xr > e) cbrt(xr) else ((k * xr) + 16 ) / 116.0
76
- val fy = if (yr > e) cbrt(yr) else ((k * yr) + 16 ) / 116.0
77
- val fz = if (zr > e) cbrt(zr) else ((k * zr) + 16 ) / 116.0
37
+ private fun Color.toLAB (): FloatArray {
38
+ val labConnector = ColorSpace .connect(
39
+ ColorSpace .get(ColorSpace .Named .SRGB ),
40
+ ColorSpace .get(ColorSpace .Named .CIE_LAB )
41
+ )
78
42
79
- return doubleArrayOf (
80
- 116 * fy - 16 ,
81
- 500 * (fx - fy) ,
82
- 200 * (fy - fz)
43
+ val rgb = floatArrayOf (
44
+ AndroidColor .red(value) / 255.0f ,
45
+ AndroidColor .green(value) / 255.0f ,
46
+ AndroidColor .blue(value) / 255.0f
83
47
)
48
+
49
+ return labConnector.transform(rgb[0 ], rgb[1 ], rgb[2 ])
84
50
}
85
51
86
52
/* *
0 commit comments