1 /*
2  * Copyright (C) 2024 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.ink.brush
18 
19 import androidx.annotation.RestrictTo
20 import androidx.ink.brush.color.Color as ComposeColor
21 import androidx.ink.brush.color.colorspace.ColorSpace as ComposeColorSpace
22 import androidx.ink.brush.color.colorspace.ColorSpaces as ComposeColorSpaces
23 import androidx.ink.brush.color.colorspace.Rgb as ComposeRgbColorSpace
24 
25 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
toColorInInkSupportedColorSpacenull26 public fun ComposeColor.toColorInInkSupportedColorSpace(): ComposeColor {
27     return if (this.colorSpace.isSupportedInInk()) {
28         this
29     } else {
30         this.convert(ComposeColorSpaces.DisplayP3)
31     }
32 }
33 
toInkColorSpaceIdnull34 internal fun ComposeColorSpace.toInkColorSpaceId() =
35     when (this) {
36         ComposeColorSpaces.Srgb -> 0
37         ComposeColorSpaces.DisplayP3 -> 1
38         else -> throw IllegalArgumentException("Unsupported Compose color space")
39     }
40 
composeColorSpaceFromInkColorSpaceIdnull41 internal fun composeColorSpaceFromInkColorSpaceId(id: Int): ComposeRgbColorSpace =
42     when (id) {
43         0 -> ComposeColorSpaces.Srgb
44         1 -> ComposeColorSpaces.DisplayP3
45         else -> throw IllegalArgumentException("Unsupported Compose color space")
46     }
47 
isSupportedInInknull48 internal fun ComposeColorSpace.isSupportedInInk() =
49     (this == ComposeColorSpaces.Srgb || this == ComposeColorSpaces.DisplayP3)
50