View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.tiff.photometricinterpreters.floatingpoint;
18  
19  import java.awt.Color;
20  
21  /**
22   * Provides a palette entry for colors associated with a range of values. The return value will be interpolated between the minimum and maximum value for this
23   * entry.
24   * <p>
25   * In keeping with the conventions of many Geographic Information Systems (GIS) and art applications, this instance "covered" values in the range v0 &le; f &lt;
26   * v1. Thus, a value that exactly matches the upper bound of the range is not considered "covered".
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       * Constructs a palette entry for the range of values v0 &le; f &lt; v1. A single color will be returned for all values in range
43       *
44       * @param v0    the lower bounds (inclusive) of the covered range of values
45       * @param v1    the upper bounds (non-inclusive) of the covered range of value
46       * @param color the color assigned to value v0
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          // check for range volation
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       * Constructs a palette entry for the range of values v0 &le; f &lt; v1. The return color value will be interpolated between the two specified colors.
75       *
76       * @param v0     the lower bounds (inclusive) of the covered range of values
77       * @param v1     the upper bounds (non-inclusive) of the covered range of value
78       * @param color0 the color assigned to value v0
79       * @param color1 the color assigned to value v1
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          // check for range volation
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 }