View Javadoc

1   /*
2    * @(#) ColorInterpolation.java May 12, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  
11  package nl.tudelft.simulation.dsol.animation.interpolation;
12  
13  import java.awt.Color;
14  
15  /***
16   * ColorInterpolation. This class works on two given colors and will, based on a
17   * start and end time transform from starting color to the destination color.
18   * 
19   * <br>
20   * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl">Delft
21   * University of Technology </a>, the Netherlands. <br>
22   * See for project information <a href="http://www.simulation.tudelft.nl">
23   * www.simulation.tudelft.nl </a> <br>
24   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
25   * License (GPL) </a>, no warranty <br>
26   * 
27   * @version 1.0 May 12, 2004 <br>
28   * @author <a
29   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
30   *         van Houten </a>
31   */
32  public class ColorInterpolation
33  {
34  	/*** the origin */
35  	private Color origin = null;
36  
37  	/*** the destination */
38  	private Color destination = null;
39  
40  	/*** the calculated RGB values */
41  	private int[] calculatedRGBValues = {0, 0, 0};
42  
43  	/*** the original starting RGB values */
44  	private int[] originalStartingRGBValues = {0, 0, 0};
45  
46  	/*** the original destination RGB values */
47  	private int[] originalDestinationRGBValues = {0, 0, 0};
48  
49  	/*** the start time */
50  	private double startTime = Double.NaN;
51  
52  	/*** the end time */
53  	private double endTime = Double.NaN;
54  
55  	/***
56  	 * constructs a new SimulatedLinearInterpolation
57  	 * 
58  	 * @param originalStartingColor the origin
59  	 * @param originalDestinationColor the destination
60  	 * @param startTime the startTime for the interpolation
61  	 * @param endTime the endTime of the interpolation
62  	 */
63  	public ColorInterpolation(final Color originalStartingColor,
64  			final Color originalDestinationColor, final double startTime,
65  			final double endTime)
66  	{
67  		super();
68  		if (endTime < startTime)
69  		{
70  			throw new IllegalArgumentException("endTime < startTime");
71  		}
72  		this.origin = originalStartingColor;
73  		this.destination = originalDestinationColor;
74  		this.startTime = startTime;
75  		this.endTime = endTime;
76  		//save rgb values of origin and destination
77  		this.originalStartingRGBValues[0] = originalStartingColor.getRed();
78  		this.originalStartingRGBValues[1] = originalStartingColor.getGreen();
79  		this.originalStartingRGBValues[2] = originalStartingColor.getBlue();
80  		this.originalDestinationRGBValues[0] = originalDestinationColor
81  				.getRed();
82  		this.originalDestinationRGBValues[1] = originalDestinationColor
83  				.getGreen();
84  		this.originalDestinationRGBValues[2] = originalDestinationColor
85  				.getBlue();
86  		for (int i = 0; i < this.calculatedRGBValues.length; i++)
87  		{
88  			if (this.originalStartingRGBValues[i] == this.originalDestinationRGBValues[i])
89  			{
90  				this.calculatedRGBValues[i] = this.originalStartingRGBValues[i];
91  			}
92  		}
93  	}
94  
95  	/***
96  	 * returns a color based on the interpolation between the original and end
97  	 * destination
98  	 * 
99  	 * @param time the time
100 	 * @return the color to return
101 	 */
102 	public Color getColor(final double time)
103 	{
104 		if (time <= this.startTime)
105 		{
106 			return this.origin;
107 		}
108 		if (time >= this.endTime)
109 		{
110 			return this.destination;
111 		}
112 		double fraction = (time - this.startTime)
113 				/ (this.endTime - this.startTime);
114 		for (int i = 0; i < this.calculatedRGBValues.length; i++)
115 		{
116 			//0 --> 255 (if you choose black -> white)
117 			if (this.originalStartingRGBValues[i] < this.originalDestinationRGBValues[i])
118 			{
119 				this.calculatedRGBValues[i] = this.originalStartingRGBValues[i]
120 						+ (int) ((this.originalDestinationRGBValues[i] - this.originalStartingRGBValues[0]) * fraction);
121 			}
122 			//255 -> 0 (if you choose white -> black)
123 			if (this.originalStartingRGBValues[i] > this.originalDestinationRGBValues[i])
124 			{
125 				this.calculatedRGBValues[i] = this.originalStartingRGBValues[i]
126 						- (int) ((this.originalStartingRGBValues[i] - this.originalDestinationRGBValues[i]) * fraction);
127 			}
128 		}
129 		//calculate alpha
130 		int alpha = this.origin.getAlpha();
131 		//only if there is a difference a new value must be calculated
132 		if (this.origin.getAlpha() != this.destination.getAlpha())
133 		{
134 			if (this.origin.getAlpha() < this.destination.getAlpha())
135 			{
136 				alpha = this.origin.getAlpha()
137 						+ (int) ((this.destination.getAlpha() - this.origin
138 								.getAlpha()) * fraction);
139 			}
140 			if (this.origin.getAlpha() > this.destination.getAlpha())
141 			{
142 				alpha = this.origin.getAlpha()
143 						- (int) ((this.origin.getAlpha() - this.destination
144 								.getAlpha()) * fraction);
145 			}
146 		}
147 		return new Color(this.calculatedRGBValues[0],
148 				this.calculatedRGBValues[1], this.calculatedRGBValues[2], alpha);
149 	}
150 }