|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager.style |
| 9 | + |
| 10 | +import android.content.Context |
| 11 | +import android.graphics.Matrix |
| 12 | +import android.graphics.Shader |
| 13 | +import android.graphics.SweepGradient |
| 14 | +import com.facebook.react.bridge.ColorPropConverter |
| 15 | +import com.facebook.react.bridge.ReadableMap |
| 16 | +import com.facebook.react.bridge.ReadableType |
| 17 | +import com.facebook.react.uimanager.LengthPercentage |
| 18 | +import com.facebook.react.uimanager.LengthPercentageType |
| 19 | +import com.facebook.react.uimanager.PixelUtil.dpToPx |
| 20 | + |
| 21 | +internal class ConicGradient( |
| 22 | + val from: Float, |
| 23 | + val position: RadialGradient.Position, |
| 24 | + val colorStops: List<ColorStop>, |
| 25 | +) : Gradient { |
| 26 | + companion object { |
| 27 | + fun parse(gradientMap: ReadableMap, context: Context): Gradient? { |
| 28 | + if (!gradientMap.hasKey("from") || !gradientMap.hasKey("position")) { |
| 29 | + return null |
| 30 | + } |
| 31 | + val from = gradientMap.getDouble("from").toFloat() |
| 32 | + val positionMap = gradientMap.getMap("position") ?: return null |
| 33 | + var top: LengthPercentage? = null |
| 34 | + var left: LengthPercentage? = null |
| 35 | + var right: LengthPercentage? = null |
| 36 | + var bottom: LengthPercentage? = null |
| 37 | + |
| 38 | + if (positionMap.hasKey("top")) { |
| 39 | + top = LengthPercentage.setFromDynamic(positionMap.getDynamic("top")) |
| 40 | + } else if (positionMap.hasKey("bottom")) { |
| 41 | + bottom = LengthPercentage.setFromDynamic(positionMap.getDynamic("bottom")) |
| 42 | + } |
| 43 | + if (positionMap.hasKey("left")) { |
| 44 | + left = LengthPercentage.setFromDynamic(positionMap.getDynamic("left")) |
| 45 | + } else if (positionMap.hasKey("right")) { |
| 46 | + right = LengthPercentage.setFromDynamic(positionMap.getDynamic("right")) |
| 47 | + } |
| 48 | + |
| 49 | + val colorStopsArray = gradientMap.getArray("colorStops") ?: return null |
| 50 | + val colorStops = ArrayList<ColorStop>(colorStopsArray.size()) |
| 51 | + for (i in 0 until colorStopsArray.size()) { |
| 52 | + val colorStop = colorStopsArray.getMap(i) ?: continue |
| 53 | + val color: Int? = |
| 54 | + when { |
| 55 | + !colorStop.hasKey("color") || colorStop.isNull("color") -> null |
| 56 | + colorStop.getType("color") == ReadableType.Map -> |
| 57 | + ColorPropConverter.getColor(colorStop.getMap("color"), context) |
| 58 | + else -> colorStop.getInt("color") |
| 59 | + } |
| 60 | + val stopPosition = LengthPercentage.setFromDynamic(colorStop.getDynamic("position")) |
| 61 | + colorStops.add(ColorStop(color, stopPosition)) |
| 62 | + } |
| 63 | + |
| 64 | + if (colorStops.size < 2) { |
| 65 | + return null |
| 66 | + } |
| 67 | + return ConicGradient(from, RadialGradient.Position(top, left, right, bottom), colorStops) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + override fun getShader(width: Float, height: Float): Shader { |
| 72 | + var centerX = width / 2f |
| 73 | + var centerY = height / 2f |
| 74 | + position.top?.let { |
| 75 | + centerY = if (it.type == LengthPercentageType.PERCENT) it.resolve(height) else it.resolve(height).dpToPx() |
| 76 | + } |
| 77 | + position.bottom?.let { |
| 78 | + centerY = if (it.type == LengthPercentageType.PERCENT) height - it.resolve(height) else height - it.resolve(height).dpToPx() |
| 79 | + } |
| 80 | + position.left?.let { |
| 81 | + centerX = if (it.type == LengthPercentageType.PERCENT) it.resolve(width) else it.resolve(width).dpToPx() |
| 82 | + } |
| 83 | + position.right?.let { |
| 84 | + centerX = if (it.type == LengthPercentageType.PERCENT) width - it.resolve(width) else width - it.resolve(width).dpToPx() |
| 85 | + } |
| 86 | + |
| 87 | + val finalStops = ColorStopUtils.getFixedColorStops(colorStops, 1f) |
| 88 | + val colors = IntArray(finalStops.size) |
| 89 | + val positions = FloatArray(finalStops.size) |
| 90 | + finalStops.forEachIndexed { index, colorStop -> |
| 91 | + colors[index] = colorStop.color ?: 0 |
| 92 | + positions[index] = colorStop.position ?: 0f |
| 93 | + } |
| 94 | + |
| 95 | + val shader = SweepGradient(centerX, centerY, colors, positions) |
| 96 | + shader.setLocalMatrix(Matrix().apply { setRotate(from - 90f, centerX, centerY) }) |
| 97 | + return shader |
| 98 | + } |
| 99 | +} |
0 commit comments