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 import androidx.annotation.IntRange
20 import androidx.compose.runtime.Immutable
21 import androidx.compose.runtime.Stable
22 import androidx.compose.ui.util.packInts
23 import androidx.compose.ui.util.unpackInt1
24 
25 /**
26  * A color model is required by a [ColorSpace] to describe the way colors can be represented as
27  * tuples of numbers. A common color model is the [RGB][Rgb] color model which defines a color as
28  * represented by a tuple of 3 numbers (red, green and blue).
29  */
30 @Immutable
31 @kotlin.jvm.JvmInline
32 value class ColorModel
33 internal constructor(
34     /**
35      * pack both the number of components and an ordinal value to distinguish between different
36      * ColorModel types that have the same number of components
37      */
38     internal val packedValue: Long
39 ) {
40     /**
41      * Returns the number of components for this color model.
42      *
43      * @return An integer between 1 and 4
44      */
45     @get:IntRange(from = 1, to = 4)
46     @Stable
47     val componentCount: Int
48         get() {
49             return unpackInt1(packedValue)
50         }
51 
52     companion object {
53         /**
54          * The RGB model is a color model with 3 components that refer to the three additive
55          * primiaries: red, green and blue.
56          */
57         val Rgb = ColorModel(packInts(3, 0))
58 
59         /**
60          * The XYZ model is a color model with 3 components that are used to model human color
61          * vision on a basic sensory level.
62          */
63         val Xyz = ColorModel(packInts(3, 1))
64 
65         /**
66          * The Lab model is a color model with 3 components used to describe a color space that is
67          * more perceptually uniform than XYZ.
68          */
69         val Lab = ColorModel(packInts(3, 2))
70 
71         /**
72          * The CMYK model is a color model with 4 components that refer to four inks used in color
73          * printing: cyan, magenta, yellow and black (or key). CMYK is a subtractive color model.
74          */
75         val Cmyk = ColorModel(packInts(4, 3))
76     }
77 
toStringnull78     override fun toString() =
79         when (this) {
80             Rgb -> "Rgb"
81             Xyz -> "Xyz"
82             Lab -> "Lab"
83             Cmyk -> "Cmyk"
84             else -> "Unknown"
85         }
86 }
87