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:  * $Id: GrayPaintScale.java,v 1.1.2.1 2007/01/31 14:15:16 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 05-Jul-2006 : Version 1 (DG);
  40:  * 31-Jan-2007 : Renamed min and max to lowerBound and upperBound (DG);
  41:  * 
  42:  */
  43: 
  44: package org.jfree.chart.renderer;
  45: 
  46: import java.awt.Color;
  47: import java.awt.Paint;
  48: import java.io.Serializable;
  49: 
  50: import org.jfree.util.PublicCloneable;
  51: 
  52: /**
  53:  * A paint scale that returns shades of gray.
  54:  * 
  55:  * @since 1.0.4
  56:  */
  57: public class GrayPaintScale 
  58:         implements PaintScale, PublicCloneable, Serializable {
  59: 
  60:     /** The lower bound. */
  61:     private double lowerBound;
  62:     
  63:     /** The upper bound. */
  64:     private double upperBound;
  65:     
  66:     /**
  67:      * Creates a new <code>GrayPaintScale</code> instance with default values.
  68:      */
  69:     public GrayPaintScale() {
  70:         this(0.0, 1.0);
  71:     }
  72:     
  73:     /**
  74:      * Creates a new paint scale for values in the specified range.
  75:      * 
  76:      * @param lowerBound  the lower bound.
  77:      * @param upperBound  the upper bound.
  78:      */
  79:     public GrayPaintScale(double lowerBound, double upperBound) {
  80:         if (lowerBound >= upperBound) {
  81:             throw new IllegalArgumentException(
  82:                     "Requires lowerBound < upperBound.");
  83:         }
  84:         this.lowerBound = lowerBound;
  85:         this.upperBound = upperBound;
  86:     }
  87:     
  88:     /**
  89:      * Returns the lower bound.
  90:      * 
  91:      * @return The lower bound.
  92:      */
  93:     public double getLowerBound() {
  94:         return this.lowerBound;
  95:     }
  96: 
  97:     /**
  98:      * Returns the upper bound.
  99:      * 
 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: }