Class HSLColor

java.lang.Object
org.jjazz.uiutilities.api.HSLColor

public class HSLColor extends Object
The HSLColor class provides methods to manipulate HSL (Hue, Saturation Luminance) values to create a corresponding Color object using the RGB ColorSpace.

The HUE is the color, the Saturation is the purity of the color (with respect to grey) and Luminance is the brightness of the color (with respect to black and white)

The Hue is specified as an angle between 0 - 360 degrees where red is 0, green is 120 and blue is 240. In between you have the colors of the rainbow. Saturation is specified as a percentage between 0 - 100 where 100 is fully saturated and 0 approaches gray. Luminance is specified as a percentage between 0 - 100 where 0 is black and 100 is white.

In particular the HSL color space makes it easier change the Tone or Shade of a color by adjusting the luminance value.

  • Constructor Summary

    Constructors
    Constructor
    Description
    HSLColor(float[] hsl)
    Create a HSLColor object using an an array containing the individual HSL values and with a default alpha value of 1.
    HSLColor(float[] hsl, float alpha)
    Create a HSLColor object using an an array containing the individual HSL values.
    HSLColor(float h, float s, float l)
    Create a HSLColor object using individual HSL values and a default alpha value of 1.0.
    HSLColor(float h, float s, float l, float alpha)
    Create a HSLColor object using individual HSL values.
    Create a HSLColor object using an RGB Color object.
  • Method Summary

    Modifier and Type
    Method
    Description
    adjustHue(float degrees)
    Create a RGB Color object based on this HSLColor with a different Hue value.
    adjustLuminance(float percent)
    Create a RGB Color object based on this HSLColor with a different Luminance value.
    adjustSaturation(float percent)
    Create a RGB Color object based on this HSLColor with a different Saturation value.
    adjustShade(float percent)
    Create a RGB Color object based on this HSLColor with a different Shade.
    adjustTone(float percent)
    Create a RGB Color object based on this HSLColor with a different Tone.
    static Color
    changeLuminance(Color c, int luminanceOffset)
    Get a new color by adjusting the luminance of Color c.
    static float[]
    fromRGB(Color color)
    Convert a RGB Color to it corresponding HSL values.
    float
    Get the Alpha value.
    Create a RGB Color object that is the complementary color of this HSLColor.
    float[]
    Get the HSL values.
    float
    Get the Hue value.
    float
    Get the Luminance value.
    Get the RGB Color object represented by this HDLColor.
    float
    Get the Saturation value.
    static Color
    toRGB(float[] hsl)
    Convert HSL values to a RGB Color with a default alpha value of 1.
    static Color
    toRGB(float[] hsl, float alpha)
    Convert HSL values to a RGB Color.
    static Color
    toRGB(float h, float s, float l)
    Convert HSL values to a RGB Color with a default alpha value of 1.
    static Color
    toRGB(float h, float s, float l, float alpha)
    Convert HSL values to a RGB Color.
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • HSLColor

      public HSLColor(Color rgb)
      Create a HSLColor object using an RGB Color object.
      Parameters:
      rgb - the RGB Color object
    • HSLColor

      public HSLColor(float h, float s, float l)
      Create a HSLColor object using individual HSL values and a default alpha value of 1.0.
      Parameters:
      h - is the Hue value in degrees between 0 - 360
      s - is the Saturation percentage between 0 - 100
      l - is the Lumanance percentage between 0 - 100
    • HSLColor

      public HSLColor(float h, float s, float l, float alpha)
      Create a HSLColor object using individual HSL values.
      Parameters:
      h - the Hue value in degrees between 0 - 360
      s - the Saturation percentage between 0 - 100
      l - the Lumanance percentage between 0 - 100
      alpha - the alpha value between 0 - 1
    • HSLColor

      public HSLColor(float[] hsl)
      Create a HSLColor object using an an array containing the individual HSL values and with a default alpha value of 1.
      Parameters:
      hsl - array containing HSL values
    • HSLColor

      public HSLColor(float[] hsl, float alpha)
      Create a HSLColor object using an an array containing the individual HSL values.
      Parameters:
      hsl - array containing HSL values
      alpha - the alpha value between 0 - 1
  • Method Details

    • adjustHue

      public Color adjustHue(float degrees)
      Create a RGB Color object based on this HSLColor with a different Hue value.

      The degrees specified is an absolute value.

      Parameters:
      degrees - - the Hue value between 0 - 360
      Returns:
      the RGB Color object
    • adjustLuminance

      public Color adjustLuminance(float percent)
      Create a RGB Color object based on this HSLColor with a different Luminance value.

      The percent specified is an absolute value.

      Parameters:
      percent - - the Luminance value between 0 - 100
      Returns:
      the RGB Color object
    • adjustSaturation

      public Color adjustSaturation(float percent)
      Create a RGB Color object based on this HSLColor with a different Saturation value. The percent specified is an absolute value.
      Parameters:
      percent - - the Saturation value between 0 - 100
      Returns:
      the RGB Color object
    • adjustShade

      public Color adjustShade(float percent)
      Create a RGB Color object based on this HSLColor with a different Shade.

      Changing the shade will return a darker color. The percent specified is a relative value.

      Parameters:
      percent - - the value between 0 - 100
      Returns:
      the RGB Color object
    • adjustTone

      public Color adjustTone(float percent)
      Create a RGB Color object based on this HSLColor with a different Tone.

      Changing the tone will return a lighter color. The percent specified is a relative value.

      Parameters:
      percent - - the value between 0 - 100
      Returns:
      the RGB Color object
    • getAlpha

      public float getAlpha()
      Get the Alpha value.
      Returns:
      the Alpha value.
    • getComplementary

      public Color getComplementary()
      Create a RGB Color object that is the complementary color of this HSLColor.

      This is a convenience method. The complementary color is determined by adding 180 degrees to the Hue value.

      Returns:
      the RGB Color object
    • getHue

      public float getHue()
      Get the Hue value.
      Returns:
      the Hue value.
    • getHSL

      public float[] getHSL()
      Get the HSL values.
      Returns:
      the HSL values.
    • getLuminance

      public float getLuminance()
      Get the Luminance value.
      Returns:
      the Luminance value [0-100]
    • getRGB

      public Color getRGB()
      Get the RGB Color object represented by this HDLColor.
      Returns:
      the RGB Color object.
    • getSaturation

      public float getSaturation()
      Get the Saturation value.
      Returns:
      the Saturation value [0-100]
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • fromRGB

      public static float[] fromRGB(Color color)
      Convert a RGB Color to it corresponding HSL values.
      Returns:
      an array containing the 3 HSL values.
    • changeLuminance

      public static Color changeLuminance(Color c, int luminanceOffset)
      Get a new color by adjusting the luminance of Color c.

      For example a luminanceOffset of 3 with c=GREEN will return a little brighter GREEN.

      Parameters:
      c -
      luminanceOffset - The value in the range [-100; 100] to be added to c's luminance. The resulting luminance is maintained in the [0-100] range.
      Returns:
    • toRGB

      public static Color toRGB(float[] hsl)
      Convert HSL values to a RGB Color with a default alpha value of 1.

      H (Hue) is specified as degrees in the range 0 - 360.
      S (Saturation) is specified as a percentage in the range 1 - 100.
      L (Lumanance) is specified as a percentage in the range 1 - 100.

      Parameters:
      hsl - an array containing the 3 HSL values
      Returns:
      the RGB Color object
    • toRGB

      public static Color toRGB(float[] hsl, float alpha)
      Convert HSL values to a RGB Color.

      H (Hue) is specified as degrees in the range 0 - 360.
      S (Saturation) is specified as a percentage in the range 1 - 100.
      L (Lumanance) is specified as a percentage in the range 1 - 100.

      Parameters:
      hsl - an array containing the 3 HSL values
      alpha - the alpha value between 0 - 1
      Returns:
      the RGB Color object
    • toRGB

      public static Color toRGB(float h, float s, float l)
      Convert HSL values to a RGB Color with a default alpha value of 1.

      Parameters:
      h - Hue is specified as degrees in the range 0 - 360.
      s - Saturation is specified as a percentage in the range 1 - 100.
      l - Lumanance is specified as a percentage in the range 1 - 100.
      Returns:
      the RGB Color object
    • toRGB

      public static Color toRGB(float h, float s, float l, float alpha)
      Convert HSL values to a RGB Color.

      Parameters:
      h - Hue is specified as degrees in the range 0 - 360.
      s - Saturation is specified as a percentage in the range 1 - 100.
      l - Lumanance is specified as a percentage in the range 1 - 100.
      alpha - the alpha value between 0 - 1
      Returns:
      the RGB Color object