Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 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.ui.graphics |
| 18 | |
| 19 | import androidx.ui.geometry.Offset |
| 20 | import android.graphics.BitmapShader |
| 21 | import android.graphics.LinearGradient |
| 22 | import android.graphics.RadialGradient |
| 23 | |
Nader Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame^] | 24 | actual typealias Shader = android.graphics.Shader |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 25 | |
| 26 | internal 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 Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame^] | 34 | return LinearGradient( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 35 | from.x, |
| 36 | from.y, |
| 37 | to.x, |
| 38 | to.y, |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 39 | colors.toIntArray(), |
| 40 | colorStops?.toFloatArray(), |
| 41 | tileMode.toNativeTileMode() |
| 42 | ) |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | internal 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 Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame^] | 53 | return RadialGradient( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 54 | center.x, |
| 55 | center.y, |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 56 | radius, |
| 57 | colors.toIntArray(), |
| 58 | colorStops?.toFloatArray(), |
| 59 | tileMode.toNativeTileMode() |
| 60 | ) |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | internal actual fun ActualImageShader( |
| 64 | image: ImageAsset, |
| 65 | tileModeX: TileMode, |
| 66 | tileModeY: TileMode |
| 67 | ): Shader { |
Nader Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame^] | 68 | return BitmapShader( |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 69 | image.asAndroidBitmap(), |
| 70 | tileModeX.toNativeTileMode(), |
| 71 | tileModeY.toNativeTileMode() |
| 72 | ) |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private fun List<Color>.toIntArray(): IntArray = |
| 76 | IntArray(size) { i -> this[i].toArgb() } |
| 77 | |
| 78 | private 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 | } |