[go: nahoru, domu]

blob: 21e6bce0f0cc6e275de9cea540cf9bd07af511a2 [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
19import androidx.ui.geometry.Offset
20import android.graphics.BitmapShader
21import android.graphics.LinearGradient
22import android.graphics.RadialGradient
23
Nader Jawadd4dceed2020-06-26 17:18:55 -070024actual typealias Shader = android.graphics.Shader
Nikolay Igottief8e2c32020-05-18 20:09:50 +030025
26internal actual fun ActualLinearGradientShader(
27 from: Offset,
28 to: Offset,
29 colors: List<Color>,
30 colorStops: List<Float>?,
31 tileMode: TileMode
32): Shader {
33 validateColorStops(colors, colorStops)
Nader Jawadd4dceed2020-06-26 17:18:55 -070034 return LinearGradient(
Nader Jawad6df06122020-06-03 15:27:08 -070035 from.x,
36 from.y,
37 to.x,
38 to.y,
Nikolay Igottief8e2c32020-05-18 20:09:50 +030039 colors.toIntArray(),
40 colorStops?.toFloatArray(),
41 tileMode.toNativeTileMode()
42 )
Nikolay Igottief8e2c32020-05-18 20:09:50 +030043}
44
45internal actual fun ActualRadialGradientShader(
46 center: Offset,
47 radius: Float,
48 colors: List<Color>,
49 colorStops: List<Float>?,
50 tileMode: TileMode
51): Shader {
52 validateColorStops(colors, colorStops)
Nader Jawadd4dceed2020-06-26 17:18:55 -070053 return RadialGradient(
Nader Jawad6df06122020-06-03 15:27:08 -070054 center.x,
55 center.y,
Nikolay Igottief8e2c32020-05-18 20:09:50 +030056 radius,
57 colors.toIntArray(),
58 colorStops?.toFloatArray(),
59 tileMode.toNativeTileMode()
60 )
Nikolay Igottief8e2c32020-05-18 20:09:50 +030061}
62
63internal actual fun ActualImageShader(
64 image: ImageAsset,
65 tileModeX: TileMode,
66 tileModeY: TileMode
67): Shader {
Nader Jawadd4dceed2020-06-26 17:18:55 -070068 return BitmapShader(
Nikolay Igottief8e2c32020-05-18 20:09:50 +030069 image.asAndroidBitmap(),
70 tileModeX.toNativeTileMode(),
71 tileModeY.toNativeTileMode()
72 )
Nikolay Igottief8e2c32020-05-18 20:09:50 +030073}
74
75private fun List<Color>.toIntArray(): IntArray =
76 IntArray(size) { i -> this[i].toArgb() }
77
78private fun validateColorStops(colors: List<Color>, colorStops: List<Float>?) {
79 if (colorStops == null) {
80 if (colors.size < 2) {
81 throw IllegalArgumentException(
82 "colors must have length of at least 2 if colorStops " +
83 "is omitted."
84 )
85 }
86 } else if (colors.size != colorStops.size) {
87 throw IllegalArgumentException(
88 "colors and colorStops arguments must have" +
89 " equal length."
90 )
91 }
92}