606N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
606N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
606N/A *
606N/A * This code is free software; you can redistribute it and/or modify it
606N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
606N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
606N/A *
606N/A * This code is distributed in the hope that it will be useful, but WITHOUT
606N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
606N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
606N/A * version 2 for more details (a copy is included in the LICENSE file that
606N/A * accompanied this code).
606N/A *
606N/A * You should have received a copy of the GNU General Public License version
606N/A * 2 along with this work; if not, write to the Free Software Foundation,
606N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
606N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
606N/A */
606N/A
606N/Apackage javax.swing.colorchooser;
606N/A
606N/Afinal class ColorModelHSV extends ColorModel {
606N/A
606N/A ColorModelHSV() {
606N/A super("hsv", "Hue", "Saturation", "Value", "Transparency"); // NON-NLS: components
606N/A }
606N/A
606N/A @Override
606N/A void setColor(int color, float[] space) {
606N/A super.setColor(color, space);
606N/A RGBtoHSV(space, space);
606N/A space[3] = 1.0f - space[3];
606N/A }
606N/A
606N/A @Override
606N/A int getColor(float[] space) {
606N/A space[3] = 1.0f - space[3];
606N/A HSVtoRGB(space, space);
606N/A return super.getColor(space);
606N/A }
606N/A
606N/A @Override
606N/A int getMaximum(int index) {
606N/A return (index == 0) ? 360 : 100;
606N/A }
606N/A
606N/A @Override
606N/A float getDefault(int index) {
606N/A return (index == 0) ? -1.0f : 1.0f;
606N/A }
606N/A
606N/A /**
606N/A * Converts HSV components of a color to a set of RGB components.
606N/A *
606N/A * @param hsv a float array with length equal to
606N/A * the number of HSV components
606N/A * @param rgb a float array with length of at least 3
606N/A * that contains RGB components of a color
606N/A * @return a float array that contains RGB components
606N/A */
606N/A private static float[] HSVtoRGB(float[] hsv, float[] rgb) {
606N/A if (rgb == null) {
606N/A rgb = new float[3];
606N/A }
606N/A float hue = hsv[0];
606N/A float saturation = hsv[1];
606N/A float value = hsv[2];
606N/A
606N/A rgb[0] = value;
606N/A rgb[1] = value;
606N/A rgb[2] = value;
606N/A
606N/A if (saturation > 0.0f) {
606N/A hue = (hue < 1.0f) ? hue * 6.0f : 0.0f;
606N/A int integer = (int) hue;
606N/A float f = hue - (float) integer;
606N/A switch (integer) {
606N/A case 0:
606N/A rgb[1] *= 1.0f - saturation * (1.0f - f);
606N/A rgb[2] *= 1.0f - saturation;
606N/A break;
606N/A case 1:
606N/A rgb[0] *= 1.0f - saturation * f;
606N/A rgb[2] *= 1.0f - saturation;
606N/A break;
606N/A case 2:
606N/A rgb[0] *= 1.0f - saturation;
606N/A rgb[2] *= 1.0f - saturation * (1.0f - f);
606N/A break;
606N/A case 3:
606N/A rgb[0] *= 1.0f - saturation;
606N/A rgb[1] *= 1.0f - saturation * f;
606N/A break;
606N/A case 4:
606N/A rgb[0] *= 1.0f - saturation * (1.0f - f);
606N/A rgb[1] *= 1.0f - saturation;
606N/A break;
606N/A case 5:
606N/A rgb[1] *= 1.0f - saturation;
606N/A rgb[2] *= 1.0f - saturation * f;
606N/A break;
606N/A }
606N/A }
606N/A return rgb;
606N/A }
606N/A
606N/A /**
606N/A * Converts RGB components of a color to a set of HSV components.
606N/A *
606N/A * @param rgb a float array with length of at least 3
606N/A * that contains RGB components of a color
606N/A * @param hsv a float array with length equal to
606N/A * the number of HSV components
606N/A * @return a float array that contains HSV components
606N/A */
606N/A private static float[] RGBtoHSV(float[] rgb, float[] hsv) {
606N/A if (hsv == null) {
606N/A hsv = new float[3];
606N/A }
606N/A float max = ColorModelHSL.max(rgb[0], rgb[1], rgb[2]);
606N/A float min = ColorModelHSL.min(rgb[0], rgb[1], rgb[2]);
606N/A
606N/A float saturation = max - min;
606N/A if (saturation > 0.0f) {
606N/A saturation /= max;
606N/A }
606N/A hsv[0] = ColorModelHSL.getHue(rgb[0], rgb[1], rgb[2], max, min);
606N/A hsv[1] = saturation;
606N/A hsv[2] = max;
606N/A return hsv;
606N/A }
606N/A}