Debian Codegraph @

src/com/github/weisj/jsvg/paint/impl/jdk/SVGRadialGradientPaint.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2023-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.paint.impl.jdk;
23
24 import java.awt.*;
25 import java.awt.geom.AffineTransform;
26 import java.awt.geom.Point2D;
27 import java.awt.geom.Rectangle2D;
28 import java.awt.image.ColorModel;
29
30 import org.jetbrains.annotations.NotNull;
31
32 /*
33 * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved. DO NOT ALTER OR
34 * REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35 *
36 * This code is free software; you can redistribute it and/or modify it under the terms of the GNU
37 * General Public License version 2 only, as published by the Free Software Foundation. Oracle
38 * designates this particular file as subject to the "Classpath" exception as provided by Oracle in
39 * the LICENSE file that accompanied this code.
40 *
41 * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
42 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * General Public License version 2 for more details (a copy is included in the LICENSE file that
44 * accompanied this code).
45 *
46 * You should have received a copy of the GNU General Public License version 2 along with this work;
47 * if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
48 * 02110-1301 USA.
49 *
50 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA or visit www.oracle.com
51 * if you need additional information or have any questions.
52 */
53
54 /**
55 * The {@code RadialGradientPaint} class provides a way to fill a shape with
56 * a circular radial color gradient pattern. The user may specify 2 or more
57 * gradient colors, and this paint will provide an interpolation between
58 * each color.
59 * <p>
60 * The user must specify the circle controlling the gradient pattern,
61 * which is described by a center point and a radius. The user can also
62 * specify a separate focus point within that circle, which controls the
63 * location of the first color of the gradient. By default the focus is
64 * set to be the center of the circle.
65 * <p>
66 * This paint will map the first color of the gradient to the focus point,
67 * and the last color to the perimeter of the circle, interpolating
68 * smoothly for any in-between colors specified by the user. Any line drawn
69 * from the focus point to the circumference will thus span all the gradient
70 * colors.
71 * <p>
72 * Specifying a focus point outside of the radius of the circle will cause
73 * the rings of the gradient pattern to be centered on the point just inside
74 * the edge of the circle in the direction of the focus point.
75 * The rendering will internally use this modified location as if it were
76 * the specified focus point.
77 * <p>
78 * The user must provide an array of floats specifying how to distribute the
79 * colors along the gradient. These values should range from 0.0 to 1.0 and
80 * act like keyframes along the gradient (they mark where the gradient should
81 * be exactly a particular color).
82 * <p>
83 * In the event that the user does not set the first keyframe value equal
84 * to 0 and/or the last keyframe value equal to 1, keyframes will be created
85 * at these positions and the first and last colors will be replicated there.
86 * So, if a user specifies the following arrays to construct a gradient:<br>
87 * <pre>
88 * {Color.BLUE, Color.RED}, {.3f, .7f}
89 * </pre>
90 * this will be converted to a gradient with the following keyframes:<br>
91 * <pre>
92 * {Color.BLUE, Color.BLUE, Color.RED, Color.RED}, {0f, .3f, .7f, 1f}
93 * </pre>
94 *
95 * <p>
96 * The user may also select what action the {@code RadialGradientPaint} object
97 * takes when it is filling the space outside the circle's radius by
98 * setting {@code CycleMethod} to either {@code REFLECTION} or {@code REPEAT}.
99 * The gradient color proportions are equal for any particular line drawn
100 * from the focus point. The following figure shows that the distance AB
101 * is equal to the distance BC, and the distance AD is equal to the distance DE.
102 * <p style="text-align:center">
103 * <img src = "doc-files/RadialGradientPaint-3.png" alt="image showing the
104 * distance AB=BC, and AD=DE">
105 * <p>
106 * If the gradient and graphics rendering transforms are uniformly scaled and
107 * the user sets the focus so that it coincides with the center of the circle,
108 * the gradient color proportions are equal for any line drawn from the center.
109 * The following figure shows the distances AB, BC, AD, and DE. They are all equal.
110 * <p style="text-align:center">
111 * <img src = "doc-files/RadialGradientPaint-4.png" alt="image showing the
112 * distance of AB, BC, AD, and DE are all equal">
113 * <p>
114 * Note that some minor variations in distances may occur due to sampling at
115 * the granularity of a pixel.
116 * If no cycle method is specified, {@code NO_CYCLE} will be chosen by
117 * default, which means the last keyframe color will be used to fill the
118 * remaining area.
119 * <p>
120 * The colorSpace parameter allows the user to specify in which colorspace
121 * the interpolation should be performed, default sRGB or linearized RGB.
122 *
123 * <p>
124 * The following code demonstrates typical usage of
125 * {@code RadialGradientPaint}, where the center and focus points are
126 * the same:
127 * <pre>
128 * Point2D center = new Point2D.Float(50, 50);
129 * float radius = 25;
130 * float[] dist = {0.0f, 0.2f, 1.0f};
131 * Color[] colors = {Color.RED, Color.WHITE, Color.BLUE};
132 * RadialGradientPaint p =
133 * new RadialGradientPaint(center, radius, dist, colors);
134 * </pre>
135 *
136 * <p>
137 * This image demonstrates the example code above, with default
138 * (centered) focus for each of the three cycle methods:
139 * <p style="text-align:center">
140 * <img src = "doc-files/RadialGradientPaint-1.png" alt="image showing the
141 * output of the sameple code">
142 * <p>
143 * It is also possible to specify a non-centered focus point, as
144 * in the following code:
145 * <pre>
146 * Point2D center = new Point2D.Float(50, 50);
147 * float radius = 25;
148 * Point2D focus = new Point2D.Float(40, 40);
149 * float[] dist = {0.0f, 0.2f, 1.0f};
150 * Color[] colors = {Color.RED, Color.WHITE, Color.BLUE};
151 * RadialGradientPaint p =
152 * new RadialGradientPaint(center, radius, focus,
153 * dist, colors,
154 * CycleMethod.NO_CYCLE);
155 * </pre>
156 *
157 * <p>
158 * This image demonstrates the previous example code, with non-centered
159 * focus for each of the three cycle methods:
160 * <p style="text-align:center">
161 * <img src = "doc-files/RadialGradientPaint-2.png" alt="image showing the
162 * output of the sample code">
163 *
164 * @see java.awt.Paint
165 * @see java.awt.Graphics2D#setPaint
166 * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
167 * @since 1.6
168 */
169 public final class SVGRadialGradientPaint extends SVGMultipleGradientPaint {
170
171 /** Focus point which defines the 0% gradient stop X coordinate. */
172 private final @NotNull Point2D focus;
173
174 /** Center of the circle defining the 100% gradient stop X coordinate. */
175 private final @NotNull Point2D center;
176
177 /** Radius of the outermost circle defining the 100% gradient stop. */
178 private final float radius;
179 /** Radius of the innermost circle defining the circular area of the 0% gradient stop. */
180 private final float focusRadius;
181
182 /**
183 * Constructs a {@code RadialGradientPaint}.
184 *
185 * @param center the center point in user space of the circle defining the
186 * gradient. The last color of the gradient is mapped to
187 * the perimeter of this circle.
188 * @param radius the radius of the circle defining the extents of the
189 * color gradient
190 * @param focus the point in user space to which the first color is mapped
191 * @param focusRadius the radius around the focus defining the circle in which the first color is mapped
192 * @param fractions numbers ranging from 0.0 to 1.0 specifying the
193 * distribution of colors along the gradient
194 * @param colors array of colors to use in the gradient. The first color
195 * is used at the focus point, the last color around the
196 * perimeter of the circle.
197 * @param cycleMethod either {@code NO_CYCLE}, {@code REFLECT},
198 * or {@code REPEAT}
199 * @param colorSpace which color space to use for interpolation,
200 * either {@code SRGB} or {@code LINEAR_RGB}
201 * @param gradientTransform transform to apply to the gradient
202 *
203 * @throws NullPointerException
204 * if one of the points is null,
205 * or {@code fractions} array is null,
206 * or {@code colors} array is null,
207 * or {@code cycleMethod} is null,
208 * or {@code colorSpace} is null,
209 * or {@code gradientTransform} is null
210 * @throws IllegalArgumentException
211 * if {@code radius} is non-positive,
212 * or {@code fractions.length != colors.length},
213 * or {@code colors} is less than 2 in size,
214 * or a {@code fractions} value is less than 0.0 or greater than 1.0,
215 * or the {@code fractions} are not provided in strictly increasing order
216 */
217 public SVGRadialGradientPaint(@NotNull Point2D center, float radius, @NotNull Point2D focus, float focusRadius,
218 float[] fractions, Color[] colors,
219 MultipleGradientPaint.CycleMethod cycleMethod, MultipleGradientPaint.ColorSpaceType colorSpace,
220 @NotNull AffineTransform gradientTransform) {
221 super(fractions, colors, cycleMethod, colorSpace, gradientTransform);
222
223 if (radius <= 0) {
224 throw new IllegalArgumentException("Radius must be greater than zero");
225 }
226
227 if (focusRadius < 0) {
228 throw new IllegalArgumentException("Radius must be greater than zero");
229 }
230
231 this.center = new Point2D.Double(center.getX(), center.getY());
232 this.focus = new Point2D.Double(focus.getX(), focus.getY());
233 this.radius = radius;
234 this.focusRadius = focusRadius;
235 }
236
237 @Override
238 public PaintContext createContext(ColorModel cm,
239 Rectangle deviceBounds,
240 Rectangle2D userBounds,
241 AffineTransform transform,
242 RenderingHints hints) {
243 // avoid modifying the user's transform...
244 transform = new AffineTransform(transform);
245 // incorporate the gradient transform
246 transform.concatenate(gradientTransform);
247
248 return new SVGRadialGradientPaintContext(this, transform,
249 (float) center.getX(), (float) center.getY(), radius,
250 (float) focus.getX(), (float) focus.getY(), focusRadius,
251 fractions, colors, cycleMethod, colorSpace);
252 }
253 }