Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / platform_tools / android / apps / AndroidKit / src / main / java / org / skia / androidkit / Gradient.java
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
8 package org.skia.androidkit;
9
10 import android.support.annotation.Nullable;
11 import java.lang.IllegalArgumentException;
12
13 public class Gradient extends Shader {
14     protected interface GradientFactory {
15         long make(float[] colors, float[] pos, int tileMode, long nativeLocalMatrix);
16     }
17
18     protected Gradient(int[] colors, float[] pos, TileMode tm, Matrix lm,
19                        GradientFactory gf) throws IllegalArgumentException {
20         super(makeGradient(colors, pos, tm, lm, gf));
21     }
22
23     protected Gradient(float[] colors, float[] pos, TileMode tm, Matrix lm,
24                        GradientFactory gf) throws IllegalArgumentException {
25         super(makeGradient(colors, pos, tm, lm, gf));
26     }
27
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.");
32         }
33
34         float[] fcolors = new float[colors.length * 4];
35
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;
41         }
42
43         return gf.make(fcolors, pos, tm.ordinal(), lm != null ? lm.getNativeInstance() : 0);
44     }
45
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.");
50         }
51
52         if (colors.length / 4 != pos.length) {
53             throw new IllegalArgumentException("Colors must be 4x positions length.");
54         }
55
56         return gf.make(colors, pos, tm.ordinal(), lm != null ? lm.getNativeInstance() : 0);
57     }
58 }