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 | |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 19 | import android.graphics.BitmapShader |
| 20 | import android.graphics.LinearGradient |
| 21 | import android.graphics.RadialGradient |
Igor Demin | b3f9d00 | 2020-07-09 19:29:41 +0300 | [diff] [blame] | 22 | import androidx.compose.InternalComposeApi |
| 23 | import androidx.ui.geometry.Offset |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 24 | |
Nader Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame] | 25 | actual typealias Shader = android.graphics.Shader |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 26 | |
Igor Demin | b3f9d00 | 2020-07-09 19:29:41 +0300 | [diff] [blame] | 27 | @Suppress("DEPRECATION_ERROR") |
| 28 | @OptIn(InternalComposeApi::class) |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 29 | internal actual fun ActualLinearGradientShader( |
| 30 | from: Offset, |
| 31 | to: Offset, |
| 32 | colors: List<Color>, |
| 33 | colorStops: List<Float>?, |
| 34 | tileMode: TileMode |
Igor Demin | b3f9d00 | 2020-07-09 19:29:41 +0300 | [diff] [blame] | 35 | ): Shader = GraphicsFactory.Shader.linear( |
| 36 | from, to, colors, colorStops, tileMode |
| 37 | ) |
| 38 | |
| 39 | @Suppress("DEPRECATION_ERROR") |
| 40 | @OptIn(InternalComposeApi::class) |
| 41 | internal 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) |
| 53 | internal actual fun ActualImageShader( |
| 54 | image: ImageAsset, |
| 55 | tileModeX: TileMode, |
| 56 | tileModeY: TileMode |
| 57 | ): Shader = GraphicsFactory.Shader.image( |
| 58 | image, tileModeX, tileModeY |
| 59 | ) |
| 60 | |
| 61 | internal fun AndroidLinearGradientShader( |
| 62 | from: Offset, |
| 63 | to: Offset, |
| 64 | colors: List<Color>, |
| 65 | colorStops: List<Float>?, |
| 66 | tileMode: TileMode |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 67 | ): Shader { |
| 68 | validateColorStops(colors, colorStops) |
Nader Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame] | 69 | return LinearGradient( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 70 | from.x, |
| 71 | from.y, |
| 72 | to.x, |
| 73 | to.y, |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 74 | colors.toIntArray(), |
| 75 | colorStops?.toFloatArray(), |
| 76 | tileMode.toNativeTileMode() |
| 77 | ) |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 78 | } |
| 79 | |
Igor Demin | b3f9d00 | 2020-07-09 19:29:41 +0300 | [diff] [blame] | 80 | internal fun AndroidRadialGradientShader( |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 81 | center: Offset, |
| 82 | radius: Float, |
| 83 | colors: List<Color>, |
| 84 | colorStops: List<Float>?, |
| 85 | tileMode: TileMode |
| 86 | ): Shader { |
| 87 | validateColorStops(colors, colorStops) |
Nader Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame] | 88 | return RadialGradient( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 89 | center.x, |
| 90 | center.y, |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 91 | radius, |
| 92 | colors.toIntArray(), |
| 93 | colorStops?.toFloatArray(), |
| 94 | tileMode.toNativeTileMode() |
| 95 | ) |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 96 | } |
| 97 | |
Igor Demin | b3f9d00 | 2020-07-09 19:29:41 +0300 | [diff] [blame] | 98 | internal fun AndroidImageShader( |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 99 | image: ImageAsset, |
| 100 | tileModeX: TileMode, |
| 101 | tileModeY: TileMode |
| 102 | ): Shader { |
Nader Jawad | d4dceed | 2020-06-26 17:18:55 -0700 | [diff] [blame] | 103 | return BitmapShader( |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 104 | image.asAndroidBitmap(), |
| 105 | tileModeX.toNativeTileMode(), |
| 106 | tileModeY.toNativeTileMode() |
| 107 | ) |
Nikolay Igotti | ef8e2c3 | 2020-05-18 20:09:50 +0300 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private fun List<Color>.toIntArray(): IntArray = |
| 111 | IntArray(size) { i -> this[i].toArgb() } |
| 112 | |
| 113 | private 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 | } |