Java HSV Color to HSB hsvToRgb(float hue, float saturation, float value)

Here you can find the source of hsvToRgb(float hue, float saturation, float value)

Description

hsv To Rgb

License

Apache License

Declaration

public static String hsvToRgb(float hue, float saturation, float value) 

Method Source Code

//package com.java2s;
/*/*  w  w  w.jav a2 s  .  co  m*/
 * Copyright 2011 gitblit.com.
 *
 * 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.
 */

public class Main {
    public static String hsvToRgb(float hue, float saturation, float value) {
        int h = (int) (hue * 6);
        float f = hue * 6 - h;
        float p = value * (1 - saturation);
        float q = value * (1 - f * saturation);
        float t = value * (1 - (1 - f) * saturation);

        switch (h) {
        case 0:
            return rgbToString(value, t, p);
        case 1:
            return rgbToString(q, value, p);
        case 2:
            return rgbToString(p, value, t);
        case 3:
            return rgbToString(p, q, value);
        case 4:
            return rgbToString(t, p, value);
        case 5:
            return rgbToString(value, p, q);
        default:
            throw new RuntimeException(
                    "Something went wrong when converting from HSV to RGB. Input was "
                            + hue + ", " + saturation + ", " + value);
        }
    }

    public static String rgbToString(float r, float g, float b) {
        String rs = Integer.toHexString((int) (r * 256));
        String gs = Integer.toHexString((int) (g * 256));
        String bs = Integer.toHexString((int) (b * 256));
        return "#" + rs + gs + bs;
    }
}

Related

  1. hsvToRgb(double[] hsv)
  2. HSVtoRGB(float h, float s, float v)
  3. HSVtoRGB(float h, float s, float v, float[] result)
  4. hsvToRgb(float h, float s, float v, int[] rgb)
  5. hsvToRGB(float hue, float saturation, float value)
  6. hsvToRGB(float p_181758_0_, float p_181758_1_, float p_181758_2_)
  7. HSVToRGB(float... hsv)
  8. HSVtoRGB(float[] hsv, float[] rgb)
  9. hsvToRgb(int hue, int saturation, int value)