Java HSL Color Convert hueFromColor(Color c)

Here you can find the source of hueFromColor(Color c)

Description

hue From Color

License

Open Source License

Declaration

public static float hueFromColor(Color c) 

Method Source Code


//package com.java2s;
/* -*- tab-width: 4 -*-/*from  www .j  a  va2  s  .c  o m*/
 *
 * Electric(tm) VLSI Design System
 *
 * File: Util.java
 *
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
 *
 * Electric(tm) 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.
 *
 * Electric(tm) 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 Electric(tm); see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, Mass 02111-1307, USA.
 */

import java.awt.*;

public class Main {
    public static float hueFromColor(Color c) {
        double r = c.getRed() / 255.;
        double g = c.getGreen() / 255.;
        double b = c.getBlue() / 255.;
        double min, max;
        min = Math.min(r, Math.min(g, b));
        max = Math.max(r, Math.max(g, b));
        double v = max;
        double h = 0;
        if (v == 0)
            return (float) h;
        r /= v;
        g /= v;
        b /= v;
        min = Math.min(r, Math.min(g, b));
        max = Math.max(r, Math.max(g, b));
        double s = max - min;
        if (s == 0) {
            h = 0;
            return (float) h;
        }
        r = (r - min) / s;
        g = (g - min) / s;
        b = (b - min) / s;
        min = Math.min(r, Math.min(g, b));
        max = Math.max(r, Math.max(g, b));
        if (max == r) {
            h = 0.0 + (60. / 360.) * (g - b);
            if (h < 0.0)
                h += 1;
        } else if (max == g) {
            h = (120. / 360.) + (60. / 360.) * (b - r);
        } else /* max == b */ {
            h = (240. / 360.) + (60. / 360.) * (r - g);
        }
        return (float) h;
    }
}

Related

  1. HslToRgb(float h, float s, float l, float a)
  2. HSLtoRGB(float hue, float sat, float lum)
  3. hslToRgb(float[] hsl)
  4. HSLtoRGB(float[] hsl)
  5. hslToRGB(int h, float s, float l)