View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class DefaultColorLookupTable
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:05 $
11   * 
12   * $Id: DefaultColorLookupTable.java,v 1.3 2004/03/03 11:53:05 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.data;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import java.awt.*;
27  import javax.swing.*;
28  
29  /*====================================================================
30             Implementation of class DefaultColorLookupTable            
31  ====================================================================*/
32  /***
33   * DefaultColorLookupTable is an object that maps
34   * scalar values into rgba (red-green-blue-alpha transparency) color
35   * specification, or rgba into scalar values. The color table can be created
36   * by direct insertion of color values, or by specifying  hue, saturation,
37   * value, and alpha range and generating a table.
38   * 
39   * @version $Revision: 1.3 $
40   * @author Masahiro Takatsuka (masa@jbeans.net)
41   * @see LookupTable
42   */
43  
44  public class DefaultColorLookupTable extends LookupTable {
45  	private double[] hueRange = new double[2];
46  	private double[] saturationRange = new double[2];
47  	private double[] valueRange = new double[2];
48  	private double[] alphaRange = new double[2];
49  
50  	/***
51  	 * Construct with range=(0,1); and hsv ranges set up for rainbow color
52  	 * table (from blue to red).
53  	 */
54  	public DefaultColorLookupTable(int dataType, int dim, int size) {
55  		super(dataType, dim, size);
56  		this.data.allocate(4 * size);
57  
58  		this.hueRange[0] = 0.0;
59  		this.hueRange[1] = 0.66667;
60  
61  		this.saturationRange[0] = 1.0;
62  		this.saturationRange[1] = 1.0;
63  
64  		this.valueRange[0] = 1.0;
65  		this.valueRange[1] = 1.0;
66  
67  		this.alphaRange[0] = 1.0;
68  		this.alphaRange[1] = 1.0;
69  
70  		build();				// numberOfLookups == 256;		
71  	}
72  
73  	public DefaultColorLookupTable(int dataType, int dim) {
74  		this(dataType, dim, 256);
75  	}
76  
77  	public DefaultColorLookupTable() {
78  		this(DataType.DOUBLE, 4, 256);
79  	}
80  
81  	/***
82  	 * Virtual constructor creates object of same type as this object.
83  	 */
84  	public AttributeArray makeObject() {
85  		return new DefaultColorLookupTable(getDataType(), this.data.getDimension());
86  	}
87  
88  	/***
89  	 * Generate lookup table.
90  	 */
91  	protected void build() {
92  		int hueCase;
93  		double hue, sat, val, lx, ly, lz, frac, hinc, sinc, vinc, ainc;
94  		double[] rgba = new double[4];
95  		double alpha;
96  		double[] c_rgba;
97  
98  		if (this.data.getNumberOfTuples() < 1 ||
99  			this.insertTime.compare(this.buildTime) > 0) {
100 			hinc = (this.hueRange[1] - this.hueRange[0])/(this.numberOfLookups-1);
101 			sinc = (this.saturationRange[1] - this.saturationRange[0])/(this.numberOfLookups-1);
102 			vinc = (this.valueRange[1] - this.valueRange[0])/(this.numberOfLookups-1);
103 			ainc = (this.alphaRange[1] - this.alphaRange[0])/(this.numberOfLookups-1);
104 			for (int i = 0; i < this.numberOfLookups; i++) {
105 				int j = this.numberOfLookups - 1 - i;
106 				hue = this.hueRange[0] + j * hinc;
107 				sat = this.saturationRange[0] + j * sinc;
108 				val = this.valueRange[0] + j * vinc;
109 				alpha = this.alphaRange[0] + j * ainc;
110 
111 				hueCase = (int)(hue * 6);
112 				frac = 6*hue - hueCase;
113 				lx = val*(1.0 - sat);
114 				ly = val*(1.0 - sat*frac);
115 				lz = val*(1.0 - sat*(1.0 - frac));
116 
117 				switch (hueCase) {
118 								/* 0<hue<1/6 */
119 				case 0:
120 				case 6:
121 					rgba[0] = val;
122 					rgba[1] = lz;
123 					rgba[2] = lx;
124 					break;
125 								/* 1/6<hue<2/6 */
126 				case 1:
127 					rgba[0] = ly;
128 					rgba[1] = val;
129 					rgba[2] = lx;
130 					break;
131 								/* 2/6<hue<3/6 */
132 				case 2:
133 					rgba[0] = lx;
134 					rgba[1] = val;
135 					rgba[2] = lz;
136 					break;
137 								/* 3/6<hue/4/6 */
138 				case 3:
139 					rgba[0] = lx;
140 					rgba[1] = ly;
141 					rgba[2] = val;
142 					break;
143 								/* 4/6<hue<5/6 */
144 				case 4:
145 					rgba[0] = lz;
146 					rgba[1] = lx;
147 					rgba[2] = val;
148 					break;
149 								/* 5/6<hue<1 */
150 				case 5:
151 					rgba[0] = val;
152 					rgba[1] = lx;
153 					rgba[2] = ly;
154 					break;
155 				}
156 				int offset = 4 * i;
157 				c_rgba = (double[]) this.data.getData();
158 				c_rgba[offset + 0] = rgba[0];
159 				c_rgba[offset + 1] = rgba[1];
160 				c_rgba[offset + 2] = rgba[2];
161 				c_rgba[offset + 3] = alpha;
162 			}
163 		}
164 		this.buildTime.modified();
165 	}
166 }
167