1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.tiff.photometricinterpreters.floatingpoint;
18
19 import java.awt.Color;
20
21
22
23
24
25
26
27
28 public class PaletteEntryForRange implements PaletteEntry {
29
30 private final float v0;
31 private final float v1;
32 private final float r0;
33 private final float r1;
34 private final float g0;
35 private final float g1;
36 private final float b0;
37 private final float b1;
38 private final float a0;
39 private final float a1;
40
41
42
43
44
45
46
47
48 public PaletteEntryForRange(final float v0, final float v1, final Color color) {
49 this.v0 = v0;
50 this.v1 = v1;
51 final float deltaV = v1 - v0;
52
53 if (deltaV <= 0 || Float.isNaN(deltaV)) {
54 throw new IllegalArgumentException("Specified values must be v0<v1");
55 }
56 if (color == null) {
57 throw new IllegalArgumentException("Null colors not allowed");
58 }
59
60 final int argb0 = color.getRGB();
61 a0 = argb0 >> 24 & 0xff;
62 r0 = argb0 >> 16 & 0xff;
63 g0 = argb0 >> 8 & 0xff;
64 b0 = argb0 & 0xff;
65
66 final int argb1 = color.getRGB();
67 a1 = argb1 >> 24 & 0xff;
68 r1 = argb1 >> 16 & 0xff;
69 g1 = argb1 >> 8 & 0xff;
70 b1 = argb1 & 0xff;
71 }
72
73
74
75
76
77
78
79
80
81 public PaletteEntryForRange(final float v0, final float v1, final Color color0, final Color color1) {
82 this.v0 = v0;
83 this.v1 = v1;
84 final float deltaV = v1 - v0;
85
86 if (deltaV <= 0 || Float.isNaN(deltaV)) {
87 throw new IllegalArgumentException("Specified values must be v0<v1");
88 }
89 if (color0 == null || color1 == null) {
90 throw new IllegalArgumentException("Null colors not allowed");
91 }
92 final int argb0 = color0.getRGB();
93 a0 = argb0 >> 24 & 0xff;
94 r0 = argb0 >> 16 & 0xff;
95 g0 = argb0 >> 8 & 0xff;
96 b0 = argb0 & 0xff;
97
98 final int argb1 = color1.getRGB();
99 a1 = argb1 >> 24 & 0xff;
100 r1 = argb1 >> 16 & 0xff;
101 g1 = argb1 >> 8 & 0xff;
102 b1 = argb1 & 0xff;
103 }
104
105 @Override
106 public boolean coversSingleEntry() {
107 return false;
108 }
109
110 @Override
111 public int getArgb(final float f) {
112 if (v0 <= f && f <= v1) {
113 final float t = (f - v0) / (v1 - v0);
114 final int a = (int) (t * (a1 - a0) + a0 + 0.5);
115 final int r = (int) (t * (r1 - r0) + r0 + 0.5);
116 final int g = (int) (t * (g1 - g0) + g0 + 0.5);
117 final int b = (int) (t * (b1 - b0) + b0 + 0.5);
118 return ((a << 8 | r) << 8 | g) << 8 | b;
119 }
120 return 0;
121 }
122
123 @Override
124 public Color getColor(final float f) {
125 if (v0 <= f && f <= v1) {
126 final float t = (f - v0) / (v1 - v0);
127 final int a = (int) (t * (a1 - a0) + a0 + 0.5);
128 final int r = (int) (t * (r1 - r0) + r0 + 0.5);
129 final int g = (int) (t * (g1 - g0) + g0 + 0.5);
130 final int b = (int) (t * (b1 - b0) + b0 + 0.5);
131 return new Color(r, g, b, a);
132 }
133 return null;
134 }
135
136 @Override
137 public float getLowerBound() {
138 return v0;
139 }
140
141 @Override
142 public float getUpperBound() {
143 return v1;
144 }
145
146 @Override
147 public boolean isCovered(final float f) {
148 return v0 <= f && f < v1;
149 }
150
151 @Override
152 public String toString() {
153 return "PaletteEntry for range " + v0 + ", " + v1;
154 }
155 }