2 * Copyright 2021 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 package org.skia.androidkit;
10 import android.support.annotation.Nullable;
11 import java.lang.IllegalArgumentException;
13 public class Gradient extends Shader {
14 protected interface GradientFactory {
15 long make(float[] colors, float[] pos, int tileMode, long nativeLocalMatrix);
18 protected Gradient(int[] colors, float[] pos, TileMode tm, Matrix lm,
19 GradientFactory gf) throws IllegalArgumentException {
20 super(makeGradient(colors, pos, tm, lm, gf));
23 protected Gradient(float[] colors, float[] pos, TileMode tm, Matrix lm,
24 GradientFactory gf) throws IllegalArgumentException {
25 super(makeGradient(colors, pos, tm, lm, gf));
28 private static long makeGradient(int[] colors, float[] pos, TileMode tm, Matrix lm,
29 GradientFactory gf) throws IllegalArgumentException {
30 if (colors.length != pos.length) {
31 throw new IllegalArgumentException("Expecting equal-length colors and positions.");
34 float[] fcolors = new float[colors.length * 4];
36 for (int i = 0; i < colors.length; ++i) {
37 fcolors[4*i + 0] = ((colors[i] >> 16) & 0xff) / 255.0f;
38 fcolors[4*i + 1] = ((colors[i] >> 8) & 0xff) / 255.0f;
39 fcolors[4*i + 2] = ((colors[i] >> 0) & 0xff) / 255.0f;
40 fcolors[4*i + 3] = ((colors[i] >> 24) & 0xff) / 255.0f;
43 return gf.make(fcolors, pos, tm.ordinal(), lm != null ? lm.getNativeInstance() : 0);
46 private static long makeGradient(float[] colors, float[] pos, TileMode tm, Matrix lm,
47 GradientFactory gf) throws IllegalArgumentException {
48 if (colors.length % 4 != 0) {
49 throw new IllegalArgumentException("Colors length must be a multiple of 4.");
52 if (colors.length / 4 != pos.length) {
53 throw new IllegalArgumentException("Colors must be 4x positions length.");
56 return gf.make(colors, pos, tm.ordinal(), lm != null ? lm.getNativeInstance() : 0);