Java HSV Color to HSB hsv2rgb_hex(float h, float s, float v)

Here you can find the source of hsv2rgb_hex(float h, float s, float v)

Description

hsvrghex

License

Apache License

Declaration

static String hsv2rgb_hex(float h, float s, float v) 

Method Source Code

//package com.java2s;
/* //from ww w .  java  2s .  c om
 * Copyright 2015 Torridity.
 *
 * 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 {
    static String hsv2rgb_hex(float h, float s, float v) {

        while (h < 0) {
            h += 360;
        }
        while (h > 360) {
            h -= 360f;
        }

        s = (s <= 0) ? 0f : (s > 100f) ? 100f : s;

        v = (v <= 0) ? 0f : (v > 100f) ? 100f : v;

        h /= 60f;
        s /= 100f;
        v /= 100f;

        float hi = (float) Math.floor(h);
        float f = h - hi;
        float p = v * (1 - s);
        float q = v * (1 - s * f);
        float t = v * (1 - s * (1 - f));

        float r, g, b;
        if (hi == 0) {
            r = v;
            g = t;
            b = p;
        } else if (hi == 1) {
            r = q;
            g = v;
            b = p;
        } else if (hi == 2) {
            r = p;
            g = v;
            b = t;
        } else if (hi == 3) {
            r = p;
            g = q;
            b = v;
        } else if (hi == 4) {
            r = t;
            g = p;
            b = v;
        } else {
            r = v;
            g = p;
            b = q;
        }

        return "#" + Integer.toHexString((int) Math.rint(r * 255)) + Integer.toHexString((int) Math.rint(g * 255))
                + Integer.toHexString((int) Math.rint(b * 255));
    }
}

Related

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