1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.ui.graphics.colorspace
18
19 /**
20 * Defines the parameters for the ICC parametric curve type 4, as defined in ICC.1:2004-10, section
21 * 10.15.
22 *
23 * [The EOTF is of the form linked
24 * here](https://d.android.com/reference/android/graphics/ColorSpace.Rgb.TransferParameters)
25 *
26 * The corresponding OETF is simply the inverse function.
27 *
28 * The parameters defined by this class form a valid transfer function only if all the following
29 * conditions are met:
30 * * No parameter is a [Not-a-Number][Double.isNaN]
31 * * `d` is in the range `[0..1]`
32 * * The function is not constant
33 * * The function is positive and increasing
34 */
35 @Suppress("DataClassDefinition")
36 data class TransferParameters(
37 /** Value g in the equation of the EOTF described above. */
38 val gamma: Double,
39 /** Value a in the equation of the EOTF described above. */
40 val a: Double,
41 /** Value b in the equation of the EOTF described above. */
42 val b: Double,
43 /** Value c in the equation of the EOTF described above. */
44 val c: Double,
45 /** Value d in the equation of the EOTF described above. */
46 val d: Double,
47 /** Value e in the equation of the EOTF described above. */
48 val e: Double = 0.0,
49 /** Value f in the equation of the EOTF described above. */
50 val f: Double = 0.0
51 ) {
52 init {
53 if (
54 a.isNaN() ||
55 b.isNaN() ||
56 c.isNaN() ||
57 d.isNaN() ||
58 e.isNaN() ||
59 f.isNaN() ||
60 gamma.isNaN()
61 ) {
62 throw IllegalArgumentException("Parameters cannot be NaN")
63 }
64
65 if (!isSpecialG(gamma)) {
66 // Next representable float after 1.0
67 // We use doubles here but the representation inside our native code is often floats
68 if (!(d >= 0.0 && d <= 1.0)) {
69 throw IllegalArgumentException(
70 "Parameter d must be in the range [0..1], was " + "$d"
71 )
72 }
73
74 if (d == 0.0 && (a == 0.0 || gamma == 0.0)) {
75 throw IllegalArgumentException(
76 "Parameter a or g is zero, the transfer function is constant"
77 )
78 }
79
80 if (d >= 1.0 && c == 0.0) {
81 throw IllegalArgumentException(
82 "Parameter c is zero, the transfer function is constant"
83 )
84 }
85
86 if ((a == 0.0 || gamma == 0.0) && c == 0.0) {
87 throw IllegalArgumentException(
88 "Parameter a or g is zero," +
89 " and c is zero, the transfer function is constant"
90 )
91 }
92
93 if (c < 0.0) {
94 throw IllegalArgumentException("The transfer function must be increasing")
95 }
96
97 if (a < 0.0 || gamma < 0.0) {
98 throw IllegalArgumentException(
99 ("The transfer function must be " + "positive or increasing")
100 )
101 }
102 }
103 }
104
105 internal val isHLGish: Boolean
106 get() = gamma == TypeHLGish
107
108 internal val isPQish: Boolean
109 get() = gamma == TypePQish
110 }
111
112 internal const val TypePQish = -2.0
113 internal const val TypeHLGish = -3.0
114
isSpecialGnull115 private fun isSpecialG(gamma: Double) = gamma == TypePQish || gamma == TypeHLGish
116