001: /*
002:
003: Copyright 2001,2003 The Apache Software Foundation
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016:
017: */
018: package com.xoetrope.batik.ext.awt;
019:
020: import java.awt.Color;
021: import java.awt.Paint;
022: import java.awt.geom.AffineTransform;
023:
024: /** This is the superclass for Paints which use a multiple color
025: * gradient to fill in their raster. It provides storage for variables and
026: * enumerated values common to LinearGradientPaint and RadialGradientPaint.
027: *
028: *
029: * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
030: * @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
031: * @version $Id: MultipleGradientPaint.java,v 1.2 2006/08/31 09:28:47 val Exp $
032: *
033: */
034:
035: public abstract class MultipleGradientPaint implements Paint {
036:
037: /** Transparency. */
038: protected int transparency;
039:
040: /** Gradient keyframe values in the range 0 to 1. */
041: protected float[] fractions;
042:
043: /** Gradient colors. */
044: protected Color[] colors;
045:
046: /** Transform to apply to gradient. */
047: protected AffineTransform gradientTransform;
048:
049: /** The method to use when painting out of the gradient bounds. */
050: protected CycleMethodEnum cycleMethod;
051:
052: /** The colorSpace in which to perform the interpolation. */
053: protected ColorSpaceEnum colorSpace;
054:
055: /** Inner class to allow for typesafe enumerated ColorSpace values. */
056: public static class ColorSpaceEnum {
057: }
058:
059: /** Inner class to allow for typesafe enumerated CycleMethod values. */
060: public static class CycleMethodEnum {
061: }
062:
063: /** Indicates (if the gradient starts or ends inside the target region)
064: * to use the terminal colors to fill the remaining area. (default)
065: */
066: public static final CycleMethodEnum NO_CYCLE = new CycleMethodEnum();
067:
068: /** Indicates (if the gradient starts or ends inside the target region),
069: * to cycle the gradient colors start-to-end, end-to-start to fill the
070: * remaining area.
071: */
072: public static final CycleMethodEnum REFLECT = new CycleMethodEnum();
073:
074: /** Indicates (if the gradient starts or ends inside the target region),
075: * to cycle the gradient colors start-to-end, start-to-end to fill the
076: * remaining area.
077: */
078: public static final CycleMethodEnum REPEAT = new CycleMethodEnum();
079:
080: /** Indicates that the color interpolation should occur in sRGB space.
081: * (default)
082: */
083: public static final ColorSpaceEnum SRGB = new ColorSpaceEnum();
084:
085: /** Indicates that the color interpolation should occur in linearized
086: * RGB space.
087: */
088: public static final ColorSpaceEnum LINEAR_RGB = new ColorSpaceEnum();
089:
090: /**
091: * Superclass constructor, typical user should never have to call this.
092: *
093: * @param fractions numbers ranging from 0.0 to 1.0 specifying the
094: * distribution of colors along the gradient
095: *
096: * @param colors array of colors corresponding to each fractional value
097: *
098: * @param cycleMethod either NO_CYCLE, REFLECT, or REPEAT
099: *
100: * @param colorSpace which colorspace to use for interpolation,
101: * either SRGB or LINEAR_RGB
102: *
103: * @param gradientTransform transform to apply to the gradient
104: *
105: * @throws NullPointerException if arrays are null, or
106: * gradientTransform is null
107: *
108: * @throws IllegalArgumentException if fractions.length != colors.length,
109: * or if colors is less than 2 in size, or if an enumerated value is bad.
110: */
111: public MultipleGradientPaint(float[] fractions, Color[] colors,
112: CycleMethodEnum cycleMethod, ColorSpaceEnum colorSpace,
113: AffineTransform gradientTransform) {
114:
115: if (fractions == null) {
116: throw new IllegalArgumentException(
117: "Fractions array cannot be " + "null");
118: }
119:
120: if (colors == null) {
121: throw new IllegalArgumentException(
122: "Colors array cannot be null");
123: }
124:
125: if (fractions.length != colors.length) {
126: throw new IllegalArgumentException(
127: "Colors and fractions must " + "have equal size");
128: }
129:
130: if (colors.length < 2) {
131: throw new IllegalArgumentException(
132: "User must specify at least " + "2 colors");
133: }
134:
135: if ((colorSpace != LINEAR_RGB) && (colorSpace != SRGB)) {
136: throw new IllegalArgumentException(
137: "Invalid colorspace for " + "interpolation.");
138: }
139:
140: if ((cycleMethod != NO_CYCLE) && (cycleMethod != REFLECT)
141: && (cycleMethod != REPEAT)) {
142: throw new IllegalArgumentException("Invalid cycle method.");
143: }
144:
145: if (gradientTransform == null) {
146: throw new IllegalArgumentException(
147: "Gradient transform cannot be " + "null.");
148: }
149:
150: //copy the fractions array
151: this .fractions = new float[fractions.length];
152: System.arraycopy(fractions, 0, this .fractions, 0,
153: fractions.length);
154:
155: //copy the colors array
156: this .colors = new Color[colors.length];
157: System.arraycopy(colors, 0, this .colors, 0, colors.length);
158:
159: //copy some flags
160: this .colorSpace = colorSpace;
161: this .cycleMethod = cycleMethod;
162:
163: //copy the gradient transform
164: this .gradientTransform = (AffineTransform) gradientTransform
165: .clone();
166:
167: // Process transparency
168: boolean opaque = true;
169: for (int i = 0; i < colors.length; i++) {
170: opaque = opaque && (colors[i].getAlpha() == 0xff);
171: }
172:
173: if (opaque) {
174: transparency = OPAQUE;
175: }
176:
177: else {
178: transparency = TRANSLUCENT;
179: }
180: }
181:
182: /**
183: * Returns a copy of the array of colors used by this gradient.
184: * @return a copy of the array of colors used by this gradient
185: *
186: */
187: public Color[] getColors() {
188: Color colors[] = new Color[this .colors.length];
189: System.arraycopy(this .colors, 0, colors, 0, this .colors.length);
190: return colors;
191: }
192:
193: /**
194: * Returns a copy of the array of floats used by this gradient
195: * to calculate color distribution.
196: * @return a copy of the array of floats used by this gradient to
197: * calculate color distribution
198: *
199: */
200: public float[] getFractions() {
201: float fractions[] = new float[this .fractions.length];
202: System.arraycopy(this .fractions, 0, fractions, 0,
203: this .fractions.length);
204: return fractions;
205: }
206:
207: /**
208: * Returns the transparency mode for this LinearGradientPaint.
209: * @return an integer value representing this LinearGradientPaint object's
210: * transparency mode.
211: */
212: public int getTransparency() {
213: return transparency;
214: }
215:
216: /**
217: * Returns the enumerated type which specifies cycling behavior.
218: * @return the enumerated type which specifies cycling behavior
219: */
220: public CycleMethodEnum getCycleMethod() {
221: return cycleMethod;
222: }
223:
224: /**
225: * Returns the enumerated type which specifies color space for
226: * interpolation.
227: * @return the enumerated type which specifies color space for
228: * interpolation
229: */
230: public ColorSpaceEnum getColorSpace() {
231: return colorSpace;
232: }
233:
234: /**
235: * Returns a copy of the transform applied to the gradient.
236: * @return a copy of the transform applied to the gradient.
237: */
238: public AffineTransform getTransform() {
239: return (AffineTransform) gradientTransform.clone();
240: }
241: }
|