| 45 | /// `Graphics` or `Style`. `copy()` produces a defensive deep clone for |
| 46 | /// places that must outlive caller mutation (e.g. async paint queues). |
| 47 | public abstract class Gradient implements Paint { |
| 48 | /// Sentinel returned by `getKind()` for `LinearGradient` instances. |
| 49 | public static final byte KIND_LINEAR = 0; |
| 50 | /// Sentinel returned by `getKind()` for `RadialGradient` instances. |
| 51 | public static final byte KIND_RADIAL = 1; |
| 52 | /// Sentinel returned by `getKind()` for `ConicGradient` instances. |
| 53 | public static final byte KIND_CONIC = 2; |
| 54 | |
| 55 | /// Cycle modes mirroring `MultipleGradientPaint.CycleMethod`. Repeated as |
| 56 | /// byte constants here so that this class (and the .res serializer) does |
| 57 | /// not pull the enum across the resource format boundary. |
| 58 | public static final byte CYCLE_NONE = 0; |
| 59 | public static final byte CYCLE_REPEAT = 1; |
| 60 | public static final byte CYCLE_REFLECT = 2; |
| 61 | |
| 62 | private int[] colors; |
| 63 | private float[] positions; |
| 64 | private byte cycleMethod = CYCLE_NONE; |
| 65 | |
| 66 | // Weak-ref token (Display.createSoftWeakRef) holding a previously rasterized |
| 67 | // ARGB image for fillGradient's default software path. Sized to the last |
| 68 | // rectangle the gradient was drawn into; if a different size comes in we |
| 69 | // re-rasterize. The cache is intentionally weak so it does not pin large |
| 70 | // bitmaps in memory when the gradient is not actively painting. |
| 71 | private Object cachedRasterRef; |
| 72 | private int cachedRasterWidth; |
| 73 | private int cachedRasterHeight; |
| 74 | |
| 75 | Gradient(int[] colors, float[] positions) { |
| 76 | if (colors == null || positions == null || colors.length != positions.length || colors.length < 2) { |
| 77 | throw new IllegalArgumentException("colors and positions must be same length, at least 2"); |
| 78 | } |
| 79 | this.colors = colors; |
| 80 | this.positions = positions; |
| 81 | } |
| 82 | |
| 83 | /// Parses a CSS gradient function string and returns the corresponding |
| 84 | /// `Gradient` subclass. Supports `linear-gradient`, `radial-gradient`, |
| 85 | /// `conic-gradient`, and the `repeating-*` variants. Mirrors the syntax |
| 86 | /// accepted by the build-time CSS compiler so a string copied verbatim |
| 87 | /// from a `.css` file produces the same gradient at runtime. |
| 88 | /// |
| 89 | /// Returns null if `css` is null/empty or does not look like a gradient |
| 90 | /// function call. Throws `IllegalArgumentException` on hard parse errors |
| 91 | /// (unknown direction, missing stops, malformed colors). |
| 92 | public static Gradient parseCss(String css) { |
| 93 | return CSSGradientParser.parse(css); |
| 94 | } |
| 95 | |
| 96 | /// Returns one of `KIND_LINEAR`, `KIND_RADIAL`, `KIND_CONIC`. |
| 97 | public abstract byte getKind(); |
| 98 | |
| 99 | /// ARGB stop colors (length >= 2). |
| 100 | public final int[] getColors() { |
| 101 | return colors; |
| 102 | } |
| 103 | |
| 104 | /// Stop positions in [0,1] aligned with `getColors()`. |
nothing calls this directly
no outgoing calls
no test coverage detected