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     * $Id: GrayPaintScale.java,v 1.1.2.1 2007/01/31 14:15:16 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 05-Jul-2006 : Version 1 (DG);
040     * 31-Jan-2007 : Renamed min and max to lowerBound and upperBound (DG);
041     * 
042     */
043    
044    package org.jfree.chart.renderer;
045    
046    import java.awt.Color;
047    import java.awt.Paint;
048    import java.io.Serializable;
049    
050    import org.jfree.util.PublicCloneable;
051    
052    /**
053     * A paint scale that returns shades of gray.
054     * 
055     * @since 1.0.4
056     */
057    public class GrayPaintScale 
058            implements PaintScale, PublicCloneable, Serializable {
059    
060        /** The lower bound. */
061        private double lowerBound;
062        
063        /** The upper bound. */
064        private double upperBound;
065        
066        /**
067         * Creates a new <code>GrayPaintScale</code> instance with default values.
068         */
069        public GrayPaintScale() {
070            this(0.0, 1.0);
071        }
072        
073        /**
074         * Creates a new paint scale for values in the specified range.
075         * 
076         * @param lowerBound  the lower bound.
077         * @param upperBound  the upper bound.
078         */
079        public GrayPaintScale(double lowerBound, double upperBound) {
080            if (lowerBound >= upperBound) {
081                throw new IllegalArgumentException(
082                        "Requires lowerBound < upperBound.");
083            }
084            this.lowerBound = lowerBound;
085            this.upperBound = upperBound;
086        }
087        
088        /**
089         * Returns the lower bound.
090         * 
091         * @return The lower bound.
092         */
093        public double getLowerBound() {
094            return this.lowerBound;
095        }
096    
097        /**
098         * Returns the upper bound.
099         * 
100         * @return The upper bound.
101         */
102        public double getUpperBound() {
103            return this.upperBound;
104        }
105    
106        /**
107         * Returns a paint for the specified value.
108         * 
109         * @param value  the value.
110         * 
111         * @return A paint for the specified value.
112         */
113        public Paint getPaint(double value) {
114            double v = Math.max(value, this.lowerBound);
115            v = Math.min(v, this.upperBound);
116            int g = (int) ((value - this.lowerBound) / (this.upperBound 
117                    - this.lowerBound) * 255.0);
118            return new Color(g, g, g);
119        }
120        
121        /**
122         * Tests this <code>GrayPaintScale</code> instance for equality with an
123         * arbitrary object.  This method returns <code>true</code> if and only
124         * if:
125         * <ul>
126         * <li><code>obj</code> is not <code>null</code>;</li>
127         * <li><code>obj</code> is an instance of <code>GrayPaintScale</code>;</li>
128         * </ul>
129         * 
130         * @param obj  the object (<code>null</code> permitted).
131         * 
132         * @return A boolean.
133         */
134        public boolean equals(Object obj) {
135            if (obj == this) {
136                return true;
137            }
138            if (!(obj instanceof GrayPaintScale)) {
139                return false;
140            }
141            GrayPaintScale that = (GrayPaintScale) obj;
142            if (this.lowerBound != that.lowerBound) {
143                return false;
144            }
145            if (this.upperBound != that.upperBound) {
146                return false;
147            }
148            return true;    
149        }
150        
151        /**
152         * Returns a clone of this <code>GrayPaintScale</code> instance.
153         * 
154         * @return A clone.
155         * 
156         * @throws CloneNotSupportedException if there is a problem cloning this
157         *     instance.
158         */
159        public Object clone() throws CloneNotSupportedException {
160            return super.clone();
161        }
162        
163    }