001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.image.colormap;
020
021import java.awt.Color;
022
023/**
024 * @author stephane
025 */
026public class HSVColorMap extends IcyColorMap
027{
028
029    /**
030     * Creates a HSV color map with default saturation of 1 and value of 1
031     */
032    public HSVColorMap()
033    {
034        this(1f, 1f);
035    }
036
037    /**
038     * Creates a HSV color map using a periodic variation the hue parameter, i.e. the actual RGB
039     * color is the same (red) at both ends of the map.<br>
040     * The implementation is actually similar to that of the java.awt.Color class, but this method
041     * is optimized to produce floating point RGB values between 0 and 1 for directly use in a
042     * ColorSpace.
043     * 
044     * @param saturation
045     *        the color saturation between 0 and 1 (0 = weak, 1 = strong)
046     * @param value
047     *        the color value between 0 and 1 (0 = dark, 1 = light)
048     * @throws IllegalArgumentException
049     *         if any of the parameters is out of range
050     */
051
052    public HSVColorMap(float saturation, float value) throws IllegalArgumentException
053    {
054        super("HSV [" + saturation + "," + value + "]");
055
056        if (saturation < 0 || saturation > 1)
057            throw new IllegalArgumentException("HSV: Saturation must be in the range [0,1]");
058        if (value < 0 || value > 1)
059            throw new IllegalArgumentException("HSV: Value must be in the range [0,1]");
060
061        final float min = (1f - saturation) * value;
062        int index;
063
064        beginUpdate();
065        try
066        {
067            index = Math.round((MAX_INDEX / 6f) * 0f);
068            setRGBControlPoint(index, new Color(value, min, min));
069
070            index = Math.round((MAX_INDEX / 6f) * 1f);
071            setRGBControlPoint(index, new Color(value, value, min));
072
073            index = Math.round((MAX_INDEX / 6f) * 2f);
074            setRGBControlPoint(index, new Color(min, value, min));
075
076            index = Math.round((MAX_INDEX / 6f) * 3f);
077            setRGBControlPoint(index, new Color(min, value, value));
078
079            index = Math.round((MAX_INDEX / 6f) * 4f);
080            setRGBControlPoint(index, new Color(min, min, value));
081
082            index = Math.round((MAX_INDEX / 6f) * 5f);
083            setRGBControlPoint(index, new Color(value, min, value));
084
085            index = Math.round((MAX_INDEX / 6f) * 6f);
086            setRGBControlPoint(index, new Color(value, min, min));
087        }
088        finally
089        {
090            endUpdate();
091        }
092    }
093}