001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, by Object Refinery Limited 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 *
027 * -------------------
028 * GrayPaintScale.java
029 * -------------------
030 * (C) Copyright 2006, 2007, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 05-Jul-2006 : Version 1 (DG);
038 * 31-Jan-2007 : Renamed min and max to lowerBound and upperBound (DG);
039 * 26-Sep-2007 : Fixed bug 1767315, problem in getPaint() method (DG);
040 * 
041 */
042
043package org.jfree.chart.renderer;
044
045import java.awt.Color;
046import java.awt.Paint;
047import java.io.Serializable;
048
049import org.jfree.util.PublicCloneable;
050
051/**
052 * A paint scale that returns shades of gray.
053 * 
054 * @since 1.0.4
055 */
056public class GrayPaintScale 
057        implements PaintScale, PublicCloneable, Serializable {
058
059    /** The lower bound. */
060    private double lowerBound;
061    
062    /** The upper bound. */
063    private double upperBound;
064    
065    /**
066     * Creates a new <code>GrayPaintScale</code> instance with default values.
067     */
068    public GrayPaintScale() {
069        this(0.0, 1.0);
070    }
071    
072    /**
073     * Creates a new paint scale for values in the specified range.
074     * 
075     * @param lowerBound  the lower bound.
076     * @param upperBound  the upper bound.
077     * 
078     * @throws IllegalArgumentException if <code>lowerBound</code> is not
079     *       less than <code>upperBound</code>.
080     */
081    public GrayPaintScale(double lowerBound, double upperBound) {
082        if (lowerBound >= upperBound) {
083            throw new IllegalArgumentException(
084                    "Requires lowerBound < upperBound.");
085        }
086        this.lowerBound = lowerBound;
087        this.upperBound = upperBound;
088    }
089    
090    /**
091     * Returns the lower bound.
092     * 
093     * @return The lower bound.
094     * 
095     * @see #getUpperBound()
096     */
097    public double getLowerBound() {
098        return this.lowerBound;
099    }
100
101    /**
102     * Returns the upper bound.
103     * 
104     * @return The upper bound.
105     * 
106     * @see #getLowerBound()
107     */
108    public double getUpperBound() {
109        return this.upperBound;
110    }
111
112    /**
113     * Returns a paint for the specified value.
114     * 
115     * @param value  the value (must be within the range specified by the
116     *         lower and upper bounds for the scale).
117     * 
118     * @return A paint for the specified value.
119     */
120    public Paint getPaint(double value) {
121        double v = Math.max(value, this.lowerBound);
122        v = Math.min(v, this.upperBound);
123        int g = (int) ((v - this.lowerBound) / (this.upperBound 
124                - this.lowerBound) * 255.0);
125        return new Color(g, g, g);
126    }
127    
128    /**
129     * Tests this <code>GrayPaintScale</code> instance for equality with an
130     * arbitrary object.  This method returns <code>true</code> if and only
131     * if:
132     * <ul>
133     * <li><code>obj</code> is not <code>null</code>;</li>
134     * <li><code>obj</code> is an instance of <code>GrayPaintScale</code>;</li>
135     * </ul>
136     * 
137     * @param obj  the object (<code>null</code> permitted).
138     * 
139     * @return A boolean.
140     */
141    public boolean equals(Object obj) {
142        if (obj == this) {
143            return true;
144        }
145        if (!(obj instanceof GrayPaintScale)) {
146            return false;
147        }
148        GrayPaintScale that = (GrayPaintScale) obj;
149        if (this.lowerBound != that.lowerBound) {
150            return false;
151        }
152        if (this.upperBound != that.upperBound) {
153            return false;
154        }
155        return true;    
156    }
157    
158    /**
159     * Returns a clone of this <code>GrayPaintScale</code> instance.
160     * 
161     * @return A clone.
162     * 
163     * @throws CloneNotSupportedException if there is a problem cloning this
164     *     instance.
165     */
166    public Object clone() throws CloneNotSupportedException {
167        return super.clone();
168    }
169    
170}