001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.formats.tiff.photometricinterpreters.floatingpoint;
018
019import java.awt.Color;
020
021/**
022 * Provides a palette entry for a color assignment to a single value. This class
023 * will assign a color to a value only if it is an exact match for the input.
024 * This class will also support Float.NaN
025 */
026public class PaletteEntryForValue implements PaletteEntry {
027
028    private final float value;
029    private final int iColor;
030    private final Color color;
031    private final boolean isNull;
032
033    /**
034     * Constructs a palette entry for a single value.
035     * <p>
036     * This class will support color-assignments for Float.NaN.
037     *
038     * @param value the color value associated with this palette entry; a
039     * Float&#46;NaN is allowed.
040     * @param color the color assigned to value
041     */
042    public PaletteEntryForValue(final float value, final Color color) {
043        if (color == null) {
044            throw new IllegalArgumentException("Null colors not allowed");
045        }
046        this.value = value;
047        this.color = color;
048        this.iColor = color.getRGB();
049        isNull = Float.isNaN(value);
050
051    }
052
053    @Override
054    public boolean isCovered(final float f) {
055        if (isNull) {
056            return Float.isNaN(f);
057        }
058        return f == value;
059    }
060
061    @Override
062    public int getARGB(final float f) {
063        if (isNull && Float.isNaN(f)) {
064            return iColor;
065        }
066        if (f == value) {
067            return iColor;
068        }
069        return 0;
070    }
071
072    @Override
073    public Color getColor(final float f) {
074        if (isNull && Float.isNaN(f)) {
075            return color;
076        }
077        if (f == value) {
078            return color;
079        }
080        return null;
081    }
082
083    @Override
084    public boolean coversSingleEntry() {
085        return true;
086    }
087
088    @Override
089    public float getLowerBound() {
090        return value;
091    }
092
093    @Override
094    public float getUpperBound() {
095        return value;
096    }
097
098    @Override
099    public String toString() {
100        return "PaletteEntry for single value" + value;
101    }
102}