samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.tools;
|
samer@0
|
11 import java.awt.Color;
|
samer@0
|
12 import java.awt.image.ColorModel;
|
samer@0
|
13 import java.awt.image.IndexColorModel;
|
samer@0
|
14
|
samer@0
|
15 /**
|
samer@0
|
16 Objects of these class could be used directly,
|
samer@0
|
17 but main purpose is to help construct an IndexColorModel
|
samer@0
|
18 for an image
|
samer@0
|
19 */
|
samer@0
|
20
|
samer@0
|
21 public class ColorRamp implements java.io.Serializable
|
samer@0
|
22 {
|
samer@0
|
23 private byte r[], g[], b[], a[];
|
samer@0
|
24 private int size;
|
samer@0
|
25 private int lasti;
|
samer@0
|
26 private Color lastc;
|
samer@0
|
27
|
samer@0
|
28
|
samer@0
|
29 public ColorRamp(int sz)
|
samer@0
|
30 {
|
samer@0
|
31 size=sz;
|
samer@0
|
32 r=new byte[size];
|
samer@0
|
33 g=new byte[size];
|
samer@0
|
34 b=new byte[size];
|
samer@0
|
35 a=new byte[size];
|
samer@0
|
36 }
|
samer@0
|
37
|
samer@0
|
38 public Color getColor(int i) {
|
samer@0
|
39 int ir = ((int)r[i] & 0xff);
|
samer@0
|
40 int ig = ((int)g[i] & 0xff);
|
samer@0
|
41 int ib = ((int)b[i] & 0xff);
|
samer@0
|
42 return new Color(ir,ig,ib);
|
samer@0
|
43 }
|
samer@0
|
44
|
samer@0
|
45 public Color[] getColorArray() {
|
samer@0
|
46 Color[] clut=new Color[size];
|
samer@0
|
47 for (int i=0; i<size; i++) clut[i]=getColor(i);
|
samer@0
|
48 return clut;
|
samer@0
|
49 }
|
samer@0
|
50
|
samer@0
|
51 public int getSize() { return size; }
|
samer@0
|
52 public IndexColorModel getColorModel() {
|
samer@0
|
53 return new IndexColorModel(8,size,r,g,b);
|
samer@0
|
54 }
|
samer@0
|
55
|
samer@0
|
56 public void set( int i, Color c) { set(i,c,1); }
|
samer@0
|
57 public void set( int i, Color c, double alpha)
|
samer@0
|
58 {
|
samer@0
|
59 r[i]=(byte)c.getRed();
|
samer@0
|
60 g[i]=(byte)c.getGreen();
|
samer@0
|
61 b[i]=(byte)c.getBlue();
|
samer@0
|
62 a[i]=clip(256.0*alpha);
|
samer@0
|
63 lasti=i; lastc=c;
|
samer@0
|
64 }
|
samer@0
|
65
|
samer@0
|
66 public void gradientTo( int i, Color c) {
|
samer@0
|
67 gradient(lasti,lastc,1.0,i,c,1.0);
|
samer@0
|
68 }
|
samer@0
|
69
|
samer@0
|
70 public void gradient( Color c1, Color c2) {
|
samer@0
|
71 gradient(0,c1,1.0,size-1,c2,1.0);
|
samer@0
|
72 }
|
samer@0
|
73
|
samer@0
|
74 public void gradient( Color c1, double a1, Color c2, double a2) {
|
samer@0
|
75 gradient(0,c1,a1,size-1,c2,a2);
|
samer@0
|
76 }
|
samer@0
|
77
|
samer@0
|
78 public void gradient( int i1, Color c1, int i2, Color c2) {
|
samer@0
|
79 gradient(i1,c1,1,i2,c2,1);
|
samer@0
|
80 }
|
samer@0
|
81
|
samer@0
|
82 public void gradient( int i1, Color c1, double a1, int i2, Color c2, double a2)
|
samer@0
|
83 {
|
samer@0
|
84 double _a, _r, _g, _b;
|
samer@0
|
85 double da, dr, dg, db;
|
samer@0
|
86 double di;
|
samer@0
|
87 int i;
|
samer@0
|
88
|
samer@0
|
89 di = i2-i1-1;
|
samer@0
|
90
|
samer@0
|
91 _a = 256*a1; da = (256*a2-_a)/di;
|
samer@0
|
92 _r = c1.getRed(); dr = (c2.getRed()-_r)/di;
|
samer@0
|
93 _g = c1.getGreen(); dg = (c2.getGreen()-_g)/di;
|
samer@0
|
94 _b = c1.getBlue(); db = (c2.getBlue()-_b)/di;
|
samer@0
|
95
|
samer@0
|
96 for (i=i1; i<=i2; i++) {
|
samer@0
|
97 a[i] = clip(_a);
|
samer@0
|
98 r[i] = clip(_r);
|
samer@0
|
99 g[i] = clip(_g);
|
samer@0
|
100 b[i] = clip(_b);
|
samer@0
|
101 _a+=da; _r+=dr; _g+=dg; _b+=db;
|
samer@0
|
102 }
|
samer@0
|
103
|
samer@0
|
104 lasti=i2; lastc=c2;
|
samer@0
|
105 }
|
samer@0
|
106
|
samer@0
|
107 private static byte clip(double t)
|
samer@0
|
108 {
|
samer@0
|
109 if (t<0) return 0;
|
samer@0
|
110 else if (t>=256.0) return (byte)255;
|
samer@0
|
111 else return (byte)t;
|
samer@0
|
112 }
|
samer@0
|
113 }
|
samer@0
|
114 |