    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 will assign a color to a value only if it is an exact match for the input. This
    023 * class will also support Float.NaN
    024 */
    025public final class PaletteEntryForValue implements PaletteEntry {
    026
    027    private final float value;
    028    private final int iColor;
    029    private final Color color;
    030    private final boolean isNull;
    031
    032    /**
    033     * Constructs a palette entry for a single value.
    034     * <p>
    035     * This class will support color-assignments for Float.NaN.
    036     *
    037     * @param value the color value associated with this palette entry; a Float&#46;NaN is allowed.
    038     * @param color the color assigned to value
    039     */
    040    public PaletteEntryForValue(final float value, final Color color) {
    041        if (color == null) {
    042            throw new IllegalArgumentException("Null colors not allowed");
    043        }
    044        this.value = value;
    045        this.color = color;
    046        this.iColor = color.getRGB();
    047        isNull = Float.isNaN(value);
    048
    049    }
    050
    051    @Override
    052    public boolean coversSingleEntry() {
    053        return true;
    054    }
    055
    056    @Override
    057    public int getArgb(final float f) {
    058        if (isNull && Float.isNaN(f)) {
    059            return iColor;
    060        }
    061        if (f == value) {
    062            return iColor;
    063        }
    064        return 0;
    065    }
    066
    067    @Override
    068    public Color getColor(final float f) {
    069        if (isNull && Float.isNaN(f)) {
    070            return color;
    071        }
    072        if (f == value) {
    073            return color;
    074        }
    075        return null;
    076    }
    077
    078    @Override
    079    public float getLowerBound() {
    080        return value;
    081    }
    082
    083    @Override
    084    public float getUpperBound() {
    085        return value;
    086    }
    087
    088    @Override
    089    public boolean isCovered(final float f) {
    090        if (isNull) {
    091            return Float.isNaN(f);
    092        }
    093        return f == value;
    094    }
    095
    096    @Override
    097    public String toString() {
    098        return "PaletteEntry for single value" + value;
    099    }
    100}