001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------
028 * GrayPaintScale.java
029 * -------------------
030 * (C) Copyright 2006-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.renderer;
038
039import java.awt.Color;
040import java.awt.Paint;
041import java.io.Serializable;
042
043import org.jfree.chart.HashUtils;
044import org.jfree.chart.util.PublicCloneable;
045
046/**
047 * A paint scale that returns shades of gray.
048 */
049public class GrayPaintScale
050        implements PaintScale, PublicCloneable, Serializable {
051
052    /** The lower bound. */
053    private double lowerBound;
054
055    /** The upper bound. */
056    private double upperBound;
057
058    /**
059     * The alpha transparency (0-255).
060     */
061    private int alpha;
062
063    /**
064     * Creates a new {@code GrayPaintScale} instance with default values.
065     */
066    public GrayPaintScale() {
067        this(0.0, 1.0);
068    }
069
070    /**
071     * Creates a new paint scale for values in the specified range.
072     *
073     * @param lowerBound  the lower bound.
074     * @param upperBound  the upper bound.
075     *
076     * @throws IllegalArgumentException if {@code lowerBound} is not
077     *       less than {@code upperBound}.
078     */
079    public GrayPaintScale(double lowerBound, double upperBound) {
080        this(lowerBound, upperBound, 255);
081    }
082
083    /**
084     * Creates a new paint scale for values in the specified range.
085     *
086     * @param lowerBound  the lower bound.
087     * @param upperBound  the upper bound.
088     * @param alpha  the alpha transparency (0-255).
089     *
090     * @throws IllegalArgumentException if {@code lowerBound} is not
091     *       less than {@code upperBound}, or {@code alpha} is not in
092     *       the range 0 to 255.
093     */
094    public GrayPaintScale(double lowerBound, double upperBound, int alpha) {
095        if (lowerBound >= upperBound) {
096            throw new IllegalArgumentException(
097                    "Requires lowerBound < upperBound.");
098        }
099        if (alpha < 0 || alpha > 255) {
100            throw new IllegalArgumentException(
101                    "Requires alpha in the range 0 to 255.");
102
103        }
104        this.lowerBound = lowerBound;
105        this.upperBound = upperBound;
106        this.alpha = alpha;
107    }
108
109    /**
110     * Returns the lower bound.
111     *
112     * @return The lower bound.
113     *
114     * @see #getUpperBound()
115     */
116    @Override
117    public double getLowerBound() {
118        return this.lowerBound;
119    }
120
121    /**
122     * Returns the upper bound.
123     *
124     * @return The upper bound.
125     *
126     * @see #getLowerBound()
127     */
128    @Override
129    public double getUpperBound() {
130        return this.upperBound;
131    }
132
133    /**
134     * Returns the alpha transparency that was specified in the constructor.
135     * 
136     * @return The alpha transparency (in the range 0 to 255).
137     */
138    public int getAlpha() {
139        return this.alpha;
140    }
141
142    /**
143     * Returns a paint for the specified value.
144     *
145     * @param value  the value (must be within the range specified by the
146     *         lower and upper bounds for the scale).
147     *
148     * @return A paint for the specified value.
149     */
150    @Override
151    public Paint getPaint(double value) {
152        double v = Math.max(value, this.lowerBound);
153        v = Math.min(v, this.upperBound);
154        int g = (int) ((v - this.lowerBound) / (this.upperBound
155                - this.lowerBound) * 255.0);
156        // FIXME:  it probably makes sense to allocate an array of 256 Colors
157        // and lazily populate this array...
158        return new Color(g, g, g, this.alpha);
159    }
160
161    /**
162     * Tests this {@code GrayPaintScale} instance for equality with an
163     * arbitrary object.  This method returns {@code true} if and only
164     * if:
165     * <ul>
166     * <li>{@code obj} is not {@code null};</li>
167     * <li>{@code obj} is an instance of {@code GrayPaintScale};</li>
168     * </ul>
169     *
170     * @param obj  the object ({@code null} permitted).
171     *
172     * @return A boolean.
173     */
174    @Override
175    public boolean equals(Object obj) {
176        if (obj == this) {
177            return true;
178        }
179        if (!(obj instanceof GrayPaintScale)) {
180            return false;
181        }
182        GrayPaintScale that = (GrayPaintScale) obj;
183        if (this.lowerBound != that.lowerBound) {
184            return false;
185        }
186        if (this.upperBound != that.upperBound) {
187            return false;
188        }
189        if (this.alpha != that.alpha) {
190            return false;
191        }
192        return true;
193    }
194
195    /**
196     * Returns a hash code for this instance.
197     *
198     * @return A hash code.
199     */
200    @Override
201    public int hashCode() {
202        int hash = 7;
203        hash = HashUtils.hashCode(hash, this.lowerBound);
204        hash = HashUtils.hashCode(hash, this.upperBound);
205        hash = 43 * hash + this.alpha;
206        return hash;
207    }
208
209    /**
210     * Returns a clone of this {@code GrayPaintScale} instance.
211     *
212     * @return A clone.
213     *
214     * @throws CloneNotSupportedException if there is a problem cloning this
215     *     instance.
216     */
217    @Override
218    public Object clone() throws CloneNotSupportedException {
219        return super.clone();
220    }
221
222}