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 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(float value, 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(float f) {
055        if (isNull) {
056            return Float.isNaN(f);
057        }
058        return f == value;
059    }
060
061    @Override
062    public int getARGB(float f) {
063        if (isNull && Float.isNaN(f)) {
064            return iColor;
065        } else if (f == value) {
066            return iColor;
067        }
068        return 0;
069    }
070
071    @Override
072    public Color getColor(float f) {
073        if (isNull && Float.isNaN(f)) {
074            return color;
075        } else if (f == value) {
076            return color;
077        }
078        return null;
079    }
080
081    @Override
082    public boolean coversSingleEntry() {
083        return true;
084    }
085
086    @Override
087    public float getLowerBound() {
088        return value;
089    }
090
091    @Override
092    public float getUpperBound() {
093        return value;
094    }
095
096    @Override
097    public String toString() {
098        return "PaletteEntry for single value" + value;
099    }
100}