[go: nahoru, domu]

blob: a77f4cd6b67d7c9dc20dfc549391c0903c22a62f [file] [log] [blame]
Nikolay Igottief8e2c32020-05-18 20:09:50 +03001/*
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
17package androidx.ui.graphics
18
Nikolay Igottief8e2c32020-05-18 20:09:50 +030019import android.graphics.BitmapShader
20import android.graphics.LinearGradient
21import android.graphics.RadialGradient
Igor Deminb3f9d002020-07-09 19:29:41 +030022import androidx.compose.InternalComposeApi
23import androidx.ui.geometry.Offset
Nikolay Igottief8e2c32020-05-18 20:09:50 +030024
Nader Jawadd4dceed2020-06-26 17:18:55 -070025actual typealias Shader = android.graphics.Shader
Nikolay Igottief8e2c32020-05-18 20:09:50 +030026
Igor Deminb3f9d002020-07-09 19:29:41 +030027@Suppress("DEPRECATION_ERROR")
28@OptIn(InternalComposeApi::class)
Nikolay Igottief8e2c32020-05-18 20:09:50 +030029internal actual fun ActualLinearGradientShader(
30 from: Offset,
31 to: Offset,
32 colors: List<Color>,
33 colorStops: List<Float>?,
34 tileMode: TileMode
Igor Deminb3f9d002020-07-09 19:29:41 +030035): Shader = GraphicsFactory.Shader.linear(
36 from, to, colors, colorStops, tileMode
37)
38
39@Suppress("DEPRECATION_ERROR")
40@OptIn(InternalComposeApi::class)
41internal actual fun ActualRadialGradientShader(
42 center: Offset,
43 radius: Float,
44 colors: List<Color>,
45 colorStops: List<Float>?,
46 tileMode: TileMode
47): Shader = GraphicsFactory.Shader.radial(
48 center, radius, colors, colorStops, tileMode
49)
50
51@Suppress("DEPRECATION_ERROR")
52@OptIn(InternalComposeApi::class)
53internal actual fun ActualImageShader(
54 image: ImageAsset,
55 tileModeX: TileMode,
56 tileModeY: TileMode
57): Shader = GraphicsFactory.Shader.image(
58 image, tileModeX, tileModeY
59)
60
61internal fun AndroidLinearGradientShader(
62 from: Offset,
63 to: Offset,
64 colors: List<Color>,
65 colorStops: List<Float>?,
66 tileMode: TileMode
Nikolay Igottief8e2c32020-05-18 20:09:50 +030067): Shader {
68 validateColorStops(colors, colorStops)
Nader Jawadd4dceed2020-06-26 17:18:55 -070069 return LinearGradient(
Nader Jawad6df06122020-06-03 15:27:08 -070070 from.x,
71 from.y,
72 to.x,
73 to.y,
Nikolay Igottief8e2c32020-05-18 20:09:50 +030074 colors.toIntArray(),
75 colorStops?.toFloatArray(),
76 tileMode.toNativeTileMode()
77 )
Nikolay Igottief8e2c32020-05-18 20:09:50 +030078}
79
Igor Deminb3f9d002020-07-09 19:29:41 +030080internal fun AndroidRadialGradientShader(
Nikolay Igottief8e2c32020-05-18 20:09:50 +030081 center: Offset,
82 radius: Float,
83 colors: List<Color>,
84 colorStops: List<Float>?,
85 tileMode: TileMode
86): Shader {
87 validateColorStops(colors, colorStops)
Nader Jawadd4dceed2020-06-26 17:18:55 -070088 return RadialGradient(
Nader Jawad6df06122020-06-03 15:27:08 -070089 center.x,
90 center.y,
Nikolay Igottief8e2c32020-05-18 20:09:50 +030091 radius,
92 colors.toIntArray(),
93 colorStops?.toFloatArray(),
94 tileMode.toNativeTileMode()
95 )
Nikolay Igottief8e2c32020-05-18 20:09:50 +030096}
97
Igor Deminb3f9d002020-07-09 19:29:41 +030098internal fun AndroidImageShader(
Nikolay Igottief8e2c32020-05-18 20:09:50 +030099 image: ImageAsset,
100 tileModeX: TileMode,
101 tileModeY: TileMode
102): Shader {
Nader Jawadd4dceed2020-06-26 17:18:55 -0700103 return BitmapShader(
Nikolay Igottief8e2c32020-05-18 20:09:50 +0300104 image.asAndroidBitmap(),
105 tileModeX.toNativeTileMode(),
106 tileModeY.toNativeTileMode()
107 )
Nikolay Igottief8e2c32020-05-18 20:09:50 +0300108}
109
110private fun List<Color>.toIntArray(): IntArray =
111 IntArray(size) { i -> this[i].toArgb() }
112
113private fun validateColorStops(colors: List<Color>, colorStops: List<Float>?) {
114 if (colorStops == null) {
115 if (colors.size < 2) {
116 throw IllegalArgumentException(
117 "colors must have length of at least 2 if colorStops " +
118 "is omitted."
119 )
120 }
121 } else if (colors.size != colorStops.size) {
122 throw IllegalArgumentException(
123 "colors and colorStops arguments must have" +
124 " equal length."
125 )
126 }
127}