1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.graphics;
18 
19 import android.annotation.ColorInt;
20 import android.annotation.ColorLong;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.compat.annotation.UnsupportedAppUsage;
24 import android.os.Build;
25 
26 
27 public class LinearGradient extends Shader {
28     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
29     private float mX0;
30     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
31     private float mY0;
32     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
33     private float mX1;
34     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
35     private float mY1;
36     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
37     private float[] mPositions;
38     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
39     private TileMode mTileMode;
40 
41     // @ColorInts are replaced by @ColorLongs, but these remain due to @UnsupportedAppUsage.
42     @UnsupportedAppUsage
43     @ColorInt
44     private int[] mColors;
45     @UnsupportedAppUsage
46     @ColorInt
47     private int mColor0;
48     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
49     @ColorInt
50     private int mColor1;
51 
52     @ColorLong
53     private final long[] mColorLongs;
54 
55 
56     /**
57      * Create a shader that draws a linear gradient along a line.
58      *
59      * @param x0           The x-coordinate for the start of the gradient line
60      * @param y0           The y-coordinate for the start of the gradient line
61      * @param x1           The x-coordinate for the end of the gradient line
62      * @param y1           The y-coordinate for the end of the gradient line
63      * @param colors       The sRGB colors to be distributed along the gradient line
64      * @param positions    May be null. The relative positions [0..1] of
65      *                     each corresponding color in the colors array. If this is null,
66      *                     the colors are distributed evenly along the gradient line.
67      * @param tile         The Shader tiling mode
68      */
LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors, @Nullable float[] positions, @NonNull TileMode tile)69     public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors,
70             @Nullable float[] positions, @NonNull TileMode tile) {
71         this(x0, y0, x1, y1, convertColors(colors), positions, tile,
72                 ColorSpace.get(ColorSpace.Named.SRGB));
73     }
74 
75     /**
76      * Create a shader that draws a linear gradient along a line.
77      *
78      * @param x0           The x-coordinate for the start of the gradient line
79      * @param y0           The y-coordinate for the start of the gradient line
80      * @param x1           The x-coordinate for the end of the gradient line
81      * @param y1           The y-coordinate for the end of the gradient line
82      * @param colors       The colors to be distributed along the gradient line
83      * @param positions    May be null. The relative positions [0..1] of
84      *                     each corresponding color in the colors array. If this is null,
85      *                     the colors are distributed evenly along the gradient line.
86      * @param tile         The Shader tiling mode
87      *
88      * @throws IllegalArgumentException if there are less than two colors, the colors do
89      *      not share the same {@link ColorSpace} or do not use a valid one, or {@code positions}
90      *      is not {@code null} and has a different length from {@code colors}.
91      */
LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorLong long[] colors, @Nullable float[] positions, @NonNull TileMode tile)92     public LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorLong long[] colors,
93             @Nullable float[] positions, @NonNull TileMode tile) {
94         this(x0, y0, x1, y1, colors.clone(), positions, tile, detectColorSpace(colors));
95     }
96 
97     /**
98      * Base constructor. Assumes @param colors is a copy that this object can hold onto,
99      * and all colors share @param colorSpace.
100      */
LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorLong long[] colors, @Nullable float[] positions, @NonNull TileMode tile, @NonNull ColorSpace colorSpace)101     private LinearGradient(float x0, float y0, float x1, float y1,
102             @NonNull @ColorLong long[] colors, @Nullable float[] positions, @NonNull TileMode tile,
103             @NonNull ColorSpace colorSpace) {
104         super(colorSpace);
105 
106         if (positions != null && colors.length != positions.length) {
107             throw new IllegalArgumentException("color and position arrays must be of equal length");
108         }
109         mX0 = x0;
110         mY0 = y0;
111         mX1 = x1;
112         mY1 = y1;
113         mColorLongs = colors;
114         mPositions = positions != null ? positions.clone() : null;
115         mTileMode = tile;
116     }
117 
118     /**
119      * Create a shader that draws a linear gradient along a line.
120      *
121      * @param x0       The x-coordinate for the start of the gradient line
122      * @param y0       The y-coordinate for the start of the gradient line
123      * @param x1       The x-coordinate for the end of the gradient line
124      * @param y1       The y-coordinate for the end of the gradient line
125      * @param color0   The sRGB color at the start of the gradient line.
126      * @param color1   The sRGB color at the end of the gradient line.
127      * @param tile     The Shader tiling mode
128      */
LinearGradient(float x0, float y0, float x1, float y1, @ColorInt int color0, @ColorInt int color1, @NonNull TileMode tile)129     public LinearGradient(float x0, float y0, float x1, float y1,
130             @ColorInt int color0, @ColorInt int color1,
131             @NonNull TileMode tile) {
132         this(x0, y0, x1, y1, Color.pack(color0), Color.pack(color1), tile);
133     }
134 
135     /**
136      * Create a shader that draws a linear gradient along a line.
137      *
138      * @param x0       The x-coordinate for the start of the gradient line
139      * @param y0       The y-coordinate for the start of the gradient line
140      * @param x1       The x-coordinate for the end of the gradient line
141      * @param y1       The y-coordinate for the end of the gradient line
142      * @param color0   The color at the start of the gradient line.
143      * @param color1   The color at the end of the gradient line.
144      * @param tile     The Shader tiling mode
145      *
146      * @throws IllegalArgumentException if the colors do
147      *      not share the same {@link ColorSpace} or do not use a valid one.
148      */
LinearGradient(float x0, float y0, float x1, float y1, @ColorLong long color0, @ColorLong long color1, @NonNull TileMode tile)149     public LinearGradient(float x0, float y0, float x1, float y1,
150             @ColorLong long color0, @ColorLong long color1,
151             @NonNull TileMode tile) {
152         this(x0, y0, x1, y1, new long[] {color0, color1}, null, tile);
153     }
154 
155     /** @hide */
156     @Override
createNativeInstance(long nativeMatrix, boolean filterFromPaint)157     protected long createNativeInstance(long nativeMatrix, boolean filterFromPaint) {
158         return nativeCreate(nativeMatrix, mX0, mY0, mX1, mY1,
159                 mColorLongs, mPositions, mTileMode.nativeInt,
160                 colorSpace().getNativeInstance());
161     }
162 
nativeCreate(long matrix, float x0, float y0, float x1, float y1, long[] colors, float[] positions, int tileMode, long colorSpaceHandle)163     private native long nativeCreate(long matrix, float x0, float y0, float x1, float y1,
164             long[] colors, float[] positions, int tileMode, long colorSpaceHandle);
165 }
166