• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 @android.ravenwood.annotation.RavenwoodKeepWholeClass
27 public class SweepGradient extends Shader {
28     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
29     private float mCx;
30     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
31     private float mCy;
32     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
33     private float[] mPositions;
34 
35     // @ColorInts are replaced by @ColorLongs, but these remain due to @UnsupportedAppUsage.
36     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
37     @ColorInt
38     private int[] mColors;
39     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
40     @ColorInt
41     private int mColor0;
42     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
43     @ColorInt
44     private int mColor1;
45 
46     @ColorLong
47     private final long[] mColorLongs;
48 
49     /**
50      * A Shader that draws a sweep gradient around a center point.
51      *
52      * @param cx       The x-coordinate of the center
53      * @param cy       The y-coordinate of the center
54      * @param colors   The sRGB colors to be distributed between around the center.
55      *                 There must be at least 2 colors in the array.
56      * @param positions May be NULL. The relative position of
57      *                 each corresponding color in the colors array, beginning
58      *                 with 0 and ending with 1.0. If the values are not
59      *                 monotonic, the drawing may produce unexpected results.
60      *                 If positions is NULL, then the colors are automatically
61      *                 spaced evenly.
62      */
SweepGradient(float cx, float cy, @NonNull @ColorInt int[] colors, @Nullable float[] positions)63     public SweepGradient(float cx, float cy, @NonNull @ColorInt int[] colors,
64             @Nullable float[] positions) {
65         this(cx, cy, convertColors(colors), positions, ColorSpace.get(ColorSpace.Named.SRGB));
66     }
67 
68     /**
69      * A Shader that draws a sweep gradient around a center point.
70      *
71      * @param cx       The x-coordinate of the center
72      * @param cy       The y-coordinate of the center
73      * @param colors   The colors to be distributed between around the center.
74      *                 There must be at least 2 colors in the array.
75      * @param positions May be NULL. The relative position of
76      *                 each corresponding color in the colors array, beginning
77      *                 with 0 and ending with 1.0. If the values are not
78      *                 monotonic, the drawing may produce unexpected results.
79      *                 If positions is NULL, then the colors are automatically
80      *                 spaced evenly.
81      * @throws IllegalArgumentException if there are less than two colors, the colors do
82      *      not share the same {@link ColorSpace} or do not use a valid one, or {@code positions}
83      *      is not {@code null} and has a different length from {@code colors}.
84      */
SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors, @Nullable float[] positions)85     public SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors,
86             @Nullable float[] positions) {
87         this(cx, cy, colors.clone(), positions, detectColorSpace(colors));
88     }
89 
90     /**
91      * Base constructor. Assumes @param colors is a copy that this object can hold onto,
92      * and all colors share @param colorSpace.
93      */
SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors, @Nullable float[] positions, ColorSpace colorSpace)94     private SweepGradient(float cx, float cy, @NonNull @ColorLong long[] colors,
95             @Nullable float[] positions, ColorSpace colorSpace) {
96         super(colorSpace);
97 
98         if (positions != null && colors.length != positions.length) {
99             throw new IllegalArgumentException(
100                     "color and position arrays must be of equal length");
101         }
102         mCx = cx;
103         mCy = cy;
104         mColorLongs = colors;
105         mPositions = positions != null ? positions.clone() : null;
106     }
107 
108     /**
109      * A Shader that draws a sweep gradient around a center point.
110      *
111      * @param cx       The x-coordinate of the center
112      * @param cy       The y-coordinate of the center
113      * @param color0   The sRGB color to use at the start of the sweep
114      * @param color1   The sRGB color to use at the end of the sweep
115      */
SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1)116     public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {
117         this(cx, cy, Color.pack(color0), Color.pack(color1));
118     }
119 
120     /**
121      * A Shader that draws a sweep gradient around a center point.
122      *
123      * @param cx       The x-coordinate of the center
124      * @param cy       The y-coordinate of the center
125      * @param color0   The color to use at the start of the sweep
126      * @param color1   The color to use at the end of the sweep
127      *
128      * @throws IllegalArgumentException if the colors do
129      *      not share the same {@link ColorSpace} or do not use a valid one.
130      */
SweepGradient(float cx, float cy, @ColorLong long color0, @ColorLong long color1)131     public SweepGradient(float cx, float cy, @ColorLong long color0, @ColorLong long color1) {
132         this(cx, cy, new long[] {color0, color1}, null);
133     }
134 
135     /** @hide */
136     @Override
createNativeInstance(long nativeMatrix, boolean filterFromPaint)137     protected long createNativeInstance(long nativeMatrix, boolean filterFromPaint) {
138         return nativeCreate(nativeMatrix, mCx, mCy, mColorLongs, mPositions,
139                 colorSpace().getNativeInstance());
140     }
141 
nativeCreate(long matrix, float x, float y, long[] colors, float[] positions, long colorSpaceHandle)142     private static native long nativeCreate(long matrix, float x, float y,
143             long[] colors, float[] positions, long colorSpaceHandle);
144 }
145 
146