View Javadoc

1   package org.catacomb.interlish.content;
2   
3   
4   import java.awt.Color;
5   
6   
7   public class ColorTable {
8   
9   
10      String name;
11  
12      int nColor;
13  
14      Color[] colors;
15  
16      double rangeMin;
17      double rangeMax;
18      double rangeDelta;
19  
20  
21      public ColorTable(String snm) {
22          name = snm;
23          nColor = 100;
24          colors = new Color[nColor];
25          Color c = Color.gray;
26          for (int i = 0; i < nColor; i++) {
27              colors[i] = c;
28          }
29          rangeMin = 0.;
30          rangeMax = 1.;
31          rangeDelta = 1.;
32      }
33  
34      public static ColorTable makeDefaultColorTable() {
35          return new ColorTable("default");
36      }
37  
38  
39      public String toString() {
40          return name;
41      }
42  
43      public String getName() {
44          return name;
45      }
46  
47      public int getNColor() {
48          return nColor;
49      }
50  
51      public Color getColor(int i) {
52          return colors[i];
53      }
54  
55      public void setNColor(int nc) {
56          Color[] ac = new Color[nc];
57          for (int i = 0; i < nc; i++) {
58              ac[i] = interpColorAt((1. * i) / (nc - 1.));
59          }
60  
61          colors = ac;
62          nColor = nc;
63      }
64  
65  
66      public void setColor(int ic, Color c) {
67          colors[ic] = c;
68      }
69  
70  
71      public Color[] getColors() {
72          return colors;
73      }
74  
75  
76  
77      public Color getColor(double f) {
78          return getFractionalColor((f - rangeMin) / rangeDelta);
79      }
80  
81  
82      public Color getFractionalColor(double f) {
83          double fr = f * nColor;
84          int ic = (int)(fr + 0.5);
85          if (ic < 0) {
86              ic = 0;
87          }
88          if (ic >= nColor) {
89              ic = nColor - 1;
90          }
91          return colors[ic];
92      }
93  
94  
95  
96      public Color interpColorAt(double fracin) {
97          double frac = fracin;
98          if (frac < 0.) {
99              frac = 0.;
100         }
101         if (frac > 1.) {
102             frac = 1.;
103         }
104         double fr = frac * (nColor-1);
105         int ipr = (int)fr;
106         double f = fr - ipr;
107         int inx = ipr+1;
108 
109         if (inx >= nColor) {
110             inx = nColor - 1;
111         }
112 
113 
114         Color ca = colors[ipr];
115         Color cb = colors[inx];
116 
117         int car = ca.getRed();
118         int cag = ca.getGreen();
119         int cab = ca.getBlue();
120 
121         int dr = cb.getRed() - car;
122         int dg = cb.getGreen() - cag;
123         int db = cb.getBlue() - cab;
124 
125         Color ret = new Color((int)(car + f * dr), (int)(cag + f * dg), (int)(cab + f * db));
126         return ret;
127     }
128 
129 
130 
131 
132 
133 
134     public void interp(int a, int b) {
135         Color ca = getColor(a);
136         Color cb = getColor(b);
137 
138         int car = ca.getRed();
139         int cag = ca.getGreen();
140         int cab = ca.getBlue();
141 
142         int dr = cb.getRed() - car;
143         int dg = cb.getGreen() - cag;
144         int db = cb.getBlue() - cab;
145 
146         for (int i = a; i <= b; i++) {
147             double f = (i - a) / (1. * (b - a));
148             setColor(i, new Color((int)(car + f * dr), (int)(cag + f * dg), (int)(cab + f * db)));
149         }
150     }
151 
152     public void setRange(double dmin, double dmax) {
153         rangeMin = dmin;
154         rangeMax = dmax;
155         syncRangeDelta();
156     }
157 
158     private void syncRangeDelta() {
159         rangeDelta = rangeMax - rangeMin;
160         if (rangeDelta == 0) {
161             rangeDelta = 1.;
162         }
163     }
164 
165     public void setRangeMin(double d) {
166         rangeMin = d;
167         syncRangeDelta();
168     }
169 
170     public void setRangeMax(double d) {
171         rangeMax = d;
172         syncRangeDelta();
173     }
174 
175 
176     public double getRangeMin() {
177         return rangeMin;
178     }
179 
180 
181     public double getRangeMax() {
182         return rangeMax;
183     }
184 
185 
186 }