Class ColorModel

java.lang.Object
com.iizigo.prop.editor.color.swt.ColorModel

public final class ColorModel extends Object
The color arithmetic of the SWT color chooser (CC-COLOR-CHOOSER-SWT-PANELS, 2026-10-10): the conversions between an ARGB value and the HSV, HSL and CMYK channel models, the hex notation of the chooser's hex field, and the SWT RGB/RGBA forms.

Pure functions without any widget or device dependency, so that every conversion is tested headless. Hue is in degrees [0,360), every other channel in [0,1]. A color is an int in the IIZI convention 0xAARRGGBB; the model conversions ignore the alpha byte and return an opaque color, the caller keeps the alpha it wants.

Author:
Christopher Mindus
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    alpha(int argb)
    Gets the alpha byte of a color, 255 for opaque.
    static int
    argb(int a, int r, int g, int b)
    Builds a color from its bytes, each clamped to 0..255.
    static int
    blue(int argb)
    Gets the blue byte of a color.
    static int
    clampByte(int v)
    Clamps a value to a byte.
    static float
    clampUnit(float v)
    Clamps a channel value to [0,1]; NaN becomes 0.
    static int
    cmykToRgb(float c, float m, float y, float k)
    Converts cyan, magenta, yellow and key to an opaque color.
    static int
    fromRGB(RGB rgb)
    Gets the opaque color of an SWT RGB.
    static int
    fromRGBA(RGBA rgba)
    Gets the color of an SWT RGBA.
    static int
    green(int argb)
    Gets the green byte of a color.
    static int
    hslToRgb(float h, float s, float l)
    Converts hue, saturation and lightness to an opaque color.
    static int
    hsvToRgb(float h, float s, float v)
    Converts hue, saturation and value to an opaque color.
    static float
    normalizeHue(float h)
    Normalizes a hue to [0,360); NaN becomes 0.
    static int
    opaque(int argb)
    Makes a color opaque: the alpha byte becomes 255, the RGB bytes are kept.
    static Integer
    parseHex(String text, boolean allowAlpha)
    Parses the hex field: six digits RRGGBB (an opaque color) or, when alpha is allowed, eight digits AARRGGBB; a leading # and surrounding blanks are accepted.
    static int
    red(int argb)
    Gets the red byte of a color.
    static int
    rgb(int r, int g, int b)
    Builds an opaque color from its bytes, each clamped to 0..255.
    static float[]
    rgbToCmyk(int argb)
    Converts a color to cyan, magenta, yellow and key (black).
    static float[]
    rgbToHsl(int argb)
    Converts a color to hue, saturation and lightness.
    static float[]
    rgbToHsv(int argb)
    Converts a color to hue, saturation and value.
    static String
    toHex(int argb, boolean withAlpha)
    Formats a color for the hex field: six upper-case digits RRGGBB for an opaque color or when the alpha is not wanted, eight digits AARRGGBB otherwise.
    static String
    tooltipFor(int argb)
    Formats the tooltip of a color: the hex value and the RGB components, as #RRGGBB r,g,b.
    static RGB
    toRGB(int argb)
    Gets the SWT RGB of a color (the alpha byte is dropped).
    static RGBA
    toRGBA(int argb)
    Gets the SWT RGBA of a color.
    static int
    withAlpha(int argb, int alpha)
    Replaces the alpha byte of a color.
  • Method Details

    • alpha

      public static int alpha(int argb)
      Gets the alpha byte of a color, 255 for opaque.
    • red

      public static int red(int argb)
      Gets the red byte of a color.
    • green

      public static int green(int argb)
      Gets the green byte of a color.
    • blue

      public static int blue(int argb)
      Gets the blue byte of a color.
    • argb

      public static int argb(int a, int r, int g, int b)
      Builds a color from its bytes, each clamped to 0..255.
    • rgb

      public static int rgb(int r, int g, int b)
      Builds an opaque color from its bytes, each clamped to 0..255.
    • opaque

      public static int opaque(int argb)
      Makes a color opaque: the alpha byte becomes 255, the RGB bytes are kept.
    • withAlpha

      public static int withAlpha(int argb, int alpha)
      Replaces the alpha byte of a color.
      Parameters:
      argb - The color.
      alpha - The new alpha, clamped to 0..255.
    • clampByte

      public static int clampByte(int v)
      Clamps a value to a byte.
    • clampUnit

      public static float clampUnit(float v)
      Clamps a channel value to [0,1]; NaN becomes 0.
    • normalizeHue

      public static float normalizeHue(float h)
      Normalizes a hue to [0,360); NaN becomes 0.
    • rgbToHsv

      public static float[] rgbToHsv(int argb)
      Converts a color to hue, saturation and value.
      Parameters:
      argb - The color; the alpha byte is ignored.
      Returns:
      {h,s,v} with the hue in degrees [0,360) (0 for a gray) and the others in [0,1].
    • hsvToRgb

      public static int hsvToRgb(float h, float s, float v)
      Converts hue, saturation and value to an opaque color.
      Parameters:
      h - The hue in degrees, any value (normalized).
      s - The saturation [0,1] (clamped).
      v - The value [0,1] (clamped).
      Returns:
      The opaque color 0xFFRRGGBB.
    • rgbToHsl

      public static float[] rgbToHsl(int argb)
      Converts a color to hue, saturation and lightness.
      Parameters:
      argb - The color; the alpha byte is ignored.
      Returns:
      {h,s,l} with the hue in degrees [0,360) (0 for a gray) and the others in [0,1].
    • hslToRgb

      public static int hslToRgb(float h, float s, float l)
      Converts hue, saturation and lightness to an opaque color.
      Parameters:
      h - The hue in degrees, any value (normalized).
      s - The saturation [0,1] (clamped).
      l - The lightness [0,1] (clamped).
      Returns:
      The opaque color 0xFFRRGGBB.
    • rgbToCmyk

      public static float[] rgbToCmyk(int argb)
      Converts a color to cyan, magenta, yellow and key (black).
      Parameters:
      argb - The color; the alpha byte is ignored.
      Returns:
      {c,m,y,k} in [0,1]; black is {0,0,0,1}.
    • cmykToRgb

      public static int cmykToRgb(float c, float m, float y, float k)
      Converts cyan, magenta, yellow and key to an opaque color.
      Parameters:
      c - Cyan [0,1] (clamped).
      m - Magenta [0,1] (clamped).
      y - Yellow [0,1] (clamped).
      k - Key [0,1] (clamped).
      Returns:
      The opaque color 0xFFRRGGBB.
    • toHex

      public static String toHex(int argb, boolean withAlpha)
      Formats a color for the hex field: six upper-case digits RRGGBB for an opaque color or when the alpha is not wanted, eight digits AARRGGBB otherwise.
      Parameters:
      argb - The color.
      withAlpha - true to write the alpha byte when it is not 255.
    • tooltipFor

      public static String tooltipFor(int argb)
      Formats the tooltip of a color: the hex value and the RGB components, as #RRGGBB r,g,b.
    • parseHex

      public static Integer parseHex(String text, boolean allowAlpha)
      Parses the hex field: six digits RRGGBB (an opaque color) or, when alpha is allowed, eight digits AARRGGBB; a leading # and surrounding blanks are accepted.
      Parameters:
      text - The text.
      allowAlpha - true to accept eight digits.
      Returns:
      The color, or null when the text is not a color.
    • toRGB

      public static RGB toRGB(int argb)
      Gets the SWT RGB of a color (the alpha byte is dropped).
    • toRGBA

      public static RGBA toRGBA(int argb)
      Gets the SWT RGBA of a color.
    • fromRGB

      public static int fromRGB(RGB rgb)
      Gets the opaque color of an SWT RGB.
    • fromRGBA

      public static int fromRGBA(RGBA rgba)
      Gets the color of an SWT RGBA.