Flutter Engine
The Flutter Engine
Gradient.java
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8package org.skia.jetski;
9
10import java.lang.IllegalArgumentException;
11
12public class Gradient extends Shader {
13 protected interface GradientFactory {
14 long make(float[] colors, float[] pos, int tileMode, long nativeLocalMatrix);
15 }
16
17 protected Gradient(int[] colors, float[] pos, TileMode tm, Matrix lm,
18 GradientFactory gf) throws IllegalArgumentException {
19 super(makeGradient(colors, pos, tm, lm, gf));
20 }
21
22 protected Gradient(float[] colors, float[] pos, TileMode tm, Matrix lm,
23 GradientFactory gf) throws IllegalArgumentException {
24 super(makeGradient(colors, pos, tm, lm, gf));
25 }
26
27 private static long makeGradient(int[] colors, float[] pos, TileMode tm, Matrix lm,
28 GradientFactory gf) throws IllegalArgumentException {
29 if (colors.length != pos.length) {
30 throw new IllegalArgumentException("Expecting equal-length colors and positions.");
31 }
32
33 float[] fcolors = new float[colors.length * 4];
34
35 for (int i = 0; i < colors.length; ++i) {
36 fcolors[4*i + 0] = ((colors[i] >> 16) & 0xff) / 255.0f;
37 fcolors[4*i + 1] = ((colors[i] >> 8) & 0xff) / 255.0f;
38 fcolors[4*i + 2] = ((colors[i] >> 0) & 0xff) / 255.0f;
39 fcolors[4*i + 3] = ((colors[i] >> 24) & 0xff) / 255.0f;
40 }
41
42 return gf.make(fcolors, pos, tm.ordinal(), lm != null ? lm.getNativeInstance() : 0);
43 }
44
45 private static long makeGradient(float[] colors, float[] pos, TileMode tm, Matrix lm,
46 GradientFactory gf) throws IllegalArgumentException {
47 if (colors.length % 4 != 0) {
48 throw new IllegalArgumentException("Colors length must be a multiple of 4.");
49 }
50
51 if (colors.length / 4 != pos.length) {
52 throw new IllegalArgumentException("Colors must be 4x positions length.");
53 }
54
55 return gf.make(colors, pos, tm.ordinal(), lm != null ? lm.getNativeInstance() : 0);
56 }
57}
SkPoint pos
Gradient(float[] colors, float[] pos, TileMode tm, Matrix lm, GradientFactory gf)
Definition: Gradient.java:22
Gradient(int[] colors, float[] pos, TileMode tm, Matrix lm, GradientFactory gf)
Definition: Gradient.java:17
sk_sp< SkShader >(* GradientFactory)(SkTileMode tm)
long make(float[] colors, float[] pos, int tileMode, long nativeLocalMatrix)
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkTileMode TileMode(jint tm)
Definition: Utils.cpp:26
SK_API sk_sp< PrecompileColorFilter > Matrix()
float length() const
Definition: SkPoint_impl.h:282