Source for org.jfree.chart.renderer.GrayPaintScale

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------
  28:  * GrayPaintScale.java
  29:  * -------------------
  30:  * (C) Copyright 2006, 2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 05-Jul-2006 : Version 1 (DG);
  38:  * 31-Jan-2007 : Renamed min and max to lowerBound and upperBound (DG);
  39:  * 26-Sep-2007 : Fixed bug 1767315, problem in getPaint() method (DG);
  40:  * 
  41:  */
  42: 
  43: package org.jfree.chart.renderer;
  44: 
  45: import java.awt.Color;
  46: import java.awt.Paint;
  47: import java.io.Serializable;
  48: 
  49: import org.jfree.util.PublicCloneable;
  50: 
  51: /**
  52:  * A paint scale that returns shades of gray.
  53:  * 
  54:  * @since 1.0.4
  55:  */
  56: public class GrayPaintScale 
  57:         implements PaintScale, PublicCloneable, Serializable {
  58: 
  59:     /** The lower bound. */
  60:     private double lowerBound;
  61:     
  62:     /** The upper bound. */
  63:     private double upperBound;
  64:     
  65:     /**
  66:      * Creates a new <code>GrayPaintScale</code> instance with default values.
  67:      */
  68:     public GrayPaintScale() {
  69:         this(0.0, 1.0);
  70:     }
  71:     
  72:     /**
  73:      * Creates a new paint scale for values in the specified range.
  74:      * 
  75:      * @param lowerBound  the lower bound.
  76:      * @param upperBound  the upper bound.
  77:      * 
  78:      * @throws IllegalArgumentException if <code>lowerBound</code> is not
  79:      *       less than <code>upperBound</code>.
  80:      */
  81:     public GrayPaintScale(double lowerBound, double upperBound) {
  82:         if (lowerBound >= upperBound) {
  83:             throw new IllegalArgumentException(
  84:                     "Requires lowerBound < upperBound.");
  85:         }
  86:         this.lowerBound = lowerBound;
  87:         this.upperBound = upperBound;
  88:     }
  89:     
  90:     /**
  91:      * Returns the lower bound.
  92:      * 
  93:      * @return The lower bound.
  94:      * 
  95:      * @see #getUpperBound()
  96:      */
  97:     public double getLowerBound() {
  98:         return this.lowerBound;
  99:     }
 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: }