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 a color assignment to a single value. This class will assign a color to a value only if it is an exact match for the input. This
23   * class will also support Float.NaN
24   */
25  public class PaletteEntryForValue implements PaletteEntry {
26  
27      private final float value;
28      private final int iColor;
29      private final Color color;
30      private final boolean isNull;
31  
32      /**
33       * Constructs a palette entry for a single value.
34       * <p>
35       * This class will support color-assignments for Float.NaN.
36       *
37       * @param value the color value associated with this palette entry; a Float&#46;NaN is allowed.
38       * @param color the color assigned to value
39       */
40      public PaletteEntryForValue(final float value, final Color color) {
41          if (color == null) {
42              throw new IllegalArgumentException("Null colors not allowed");
43          }
44          this.value = value;
45          this.color = color;
46          this.iColor = color.getRGB();
47          isNull = Float.isNaN(value);
48  
49      }
50  
51      @Override
52      public boolean coversSingleEntry() {
53          return true;
54      }
55  
56      @Override
57      public int getArgb(final float f) {
58          if (isNull && Float.isNaN(f)) {
59              return iColor;
60          }
61          if (f == value) {
62              return iColor;
63          }
64          return 0;
65      }
66  
67      @Override
68      public Color getColor(final float f) {
69          if (isNull && Float.isNaN(f)) {
70              return color;
71          }
72          if (f == value) {
73              return color;
74          }
75          return null;
76      }
77  
78      @Override
79      public float getLowerBound() {
80          return value;
81      }
82  
83      @Override
84      public float getUpperBound() {
85          return value;
86      }
87  
88      @Override
89      public boolean isCovered(final float f) {
90          if (isNull) {
91              return Float.isNaN(f);
92          }
93          return f == value;
94      }
95  
96      @Override
97      public String toString() {
98          return "PaletteEntry for single value" + value;
99      }
100 }