Java RGB Color Convert To RGBtoHSB(int red, int green, int blue, float hsb[])

Here you can find the source of RGBtoHSB(int red, int green, int blue, float hsb[])

Description

Fill and return the specified array with the specified RGB values converted to HSB.

License

Open Source License

Parameter

Parameter Description
red the red component
green the green component
blue the blue component
hsb the return value or <code>null</code>

Exception

Parameter Description
IllegalArgumentException if any of the components are invalid

Return

an float[] containing the converted HSB values

Declaration

public static float[] RGBtoHSB(int red, int green, int blue, float hsb[]) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w w w  .jav  a  2 s.  co m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Fill and return the specified array with the specified RGB values
     * converted to HSB. Create, fill and return a new array if <code>null</code>
     * is passed in.
     * <p>
     * Color components must be in the range (0 - 255).
     *
     * @version    initial
     *
     * @param   red      the red component
     * @param   green   the green component
     * @param   blue   the blue component
     * @param   hsb      the return value or <code>null</code>
     *
     * @return   an <code>float[]</code> containing the converted HSB values
     *
     * @throws   IllegalArgumentException   if any of the components are invalid
     */
    public static float[] RGBtoHSB(int red, int green, int blue, float hsb[]) {
        if ((red < 0 || red > 255) || (green < 0 || green > 255) || (blue < 0 || blue > 255))
            throw new IllegalArgumentException();

        float r = red / 255f;
        float g = green / 255f;
        float b = blue / 255f;

        float h = 0; // hue
        float s; // saturation
        float v; // brightnees

        float max = Math.max(r, Math.max(g, b));
        float min = Math.min(r, Math.min(g, b));

        // Calculate brightness
        v = max;

        // Calculate saturation
        if (max != 0)
            s = (max - min) / max;
        else
            s = 0;

        // Calculate hue
        if (s != 0) {
            float delta = max - min;
            if (r == max)
                h = (g - b) / delta;
            else if (g == max)
                h = 2 + (b - r) / delta;
            else if (b == max)
                h = 4 + (r - g) / delta;

            h = h * 60f;
            if (h < 0)
                h = h + 360f;
            h /= 360f;
        }

        // Fill return value
        if (hsb == null)
            return new float[] { h, s, v };
        else {
            hsb[0] = h;
            hsb[1] = s;
            hsb[2] = v;
            return hsb;
        }
    }
}

Related

  1. rgbToHex(int[] rgb)
  2. RGBToHex(int[] rgb)
  3. rgbToHex(String color)
  4. RGBtoHSB(int r, int g, int b, float hsbvals[])
  5. RGBtoHSB(int r, int g, int b, float[] hsbvals)
  6. rgbToHsl(byte red, byte green, byte blue)
  7. RgbToHsl(float r, float g, float b, float a)
  8. rgbToHsl(float[] rgb)
  9. RGBtoHSL(int r, int g, int b)