Java HSV Color to HSB HSVToColor(float[] hsv)

Here you can find the source of HSVToColor(float[] hsv)

Description

HSV To Color

License

Open Source License

Declaration

public static int HSVToColor(float[] hsv) 

Method Source Code

//package com.java2s;
/*//w w  w  .ja va2 s  .c  o  m
 * Copyright appNativa Inc. All Rights Reserved.
 *
 * This file is part of the Real-time Application Rendering Engine (RARE).
 *
 * RARE 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/>.
 *
 */

public class Main {
    public static int HSVToColor(float[] hsv) {
        return HSVToColor(255, hsv);
    }

    public static int HSVToColor(int alpha, float[] hsv) {
        /*
         * Copyright (C) 2006 The Android Open Source Project
         *
         * Licensed under the Apache License, Version 2.0 (the "License");
         * you may not use this file except in compliance with the License.
         * You may obtain a copy of the License at
         *
         *      http://www.apache.org/licenses/LICENSE-2.0
         *
         * Unless required by applicable law or agreed to in writing, software
         * distributed under the License is distributed on an "AS IS" BASIS,
         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         * See the License for the specific language governing permissions and
         * limitations under the License.
         */
        float h = hsv[0];
        float s = hsv[1];
        float b = hsv[2];

        if (h < 0) {
            h = 0;
        }

        if (s < 0) {
            s = 0;
        }

        if (b < 0) {
            b = 0;
        }

        if (h > 1) {
            h = 1;
        }

        if (s > 1) {
            s = 1;
        }

        if (b > 1) {
            b = 1;
        }

        float red = 0.0f;
        float green = 0.0f;
        float blue = 0.0f;
        final float hf = (h - (int) h) * 6.0f;
        final int ihf = (int) hf;
        final float f = hf - ihf;
        final float pv = b * (1.0f - s);
        final float qv = b * (1.0f - s * f);
        final float tv = b * (1.0f - s * (1.0f - f));

        switch (ihf) {
        case 0: // Red is the dominant color
            red = b;
            green = tv;
            blue = pv;

            break;

        case 1: // Green is the dominant color
            red = qv;
            green = b;
            blue = pv;

            break;

        case 2:
            red = pv;
            green = b;
            blue = tv;

            break;

        case 3: // Blue is the dominant color
            red = pv;
            green = qv;
            blue = b;

            break;

        case 4:
            red = tv;
            green = pv;
            blue = b;

            break;

        case 5: // Red is the dominant color
            red = b;
            green = pv;
            blue = qv;

            break;
        }

        return ((alpha & 0xff) << 24) | (((int) (red * 255.0f)) << 16) | (((int) (green * 255.0f)) << 8)
                | ((int) (blue * 255.0f));
    }
}

Related

  1. hsv2rgb(float h, float s, float v)
  2. hsv2rgb(float h, float s, float v, float[] rgb)
  3. hsv2rgb_hex(float h, float s, float v)
  4. hsva(double hue, double saturation, double value, double alpha)
  5. hsvToRgb(double hue, double saturation, double value)
  6. HSVtoRGB(double hue, double saturation, double value)
  7. hsvToRgb(double[] hsv)
  8. HSVtoRGB(float h, float s, float v)