Color rgb To HSV Color - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Color rgb To HSV Color

Demo Code

/*/*from  w  ww . j a va 2  s .c o m*/
 * (C) Copyright 2000-2011, by Scott Preston and Preston Research LLC
 *
 *  Project Info:  http://www.scottsbots.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import com.scottsbots.core.utils.Utils;

public class Main{
    public static int[] rgbToHSV(Color rgb) {
        double r = rgb.getRed();
        double g = rgb.getGreen();
        double b = rgb.getBlue();
        double max = Math.max(Math.max(r, g), b);
        double min = Math.min(Math.min(r, b), b);
        double h = 0;
        if (r == max) {
            h = ((g - b) / (max - min)) * 60;
        } else if (g == max) {
            h = (((b - r) / (max - min)) * 60) + 120;
        } else if (b == max) {
            h = (((r - g) / (max - min)) * 60) + 240;
        }
        return new int[] { (int) ((h / 360) * 240),
                (int) ((max - min) * 240), (int) (max * 240) };
    }
}

Related Tutorials