Class ColorSpace

java.lang.Object
com.iizix.prop.ColorSpace

public final class ColorSpace extends Object
Color space arithmetic: sRGB, linear sRGB and OKLab (Bjorn Ottosson, 2020; the space of CSS Color 4's oklab() and oklch()), for the contrast quick fix of the theme editor (ColorContrast.adjust(int,int,double)) and the color ramp generator.

OKLab is perceptual: its lightness L (0 black to 1 white) tracks how light a color looks, and its a and b axes keep the hue when the lightness moves, which HSL does not. A color is passed as an ARGB int, the form of GColor.getColor(); a point of OKLab as a double[3] of L, a and b. Converting back clamps a point outside the sRGB gamut channel by channel; inGamut(double[]) tells whether that happens.

Author:
Christopher Mindus
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    fromLinear(double c)
    Converts one linear channel back to sRGB.
    static int
    fromOKLab(double[] lab, int alpha)
    Converts an OKLab point to a color, each channel clamped to the gamut and rounded.
    static double[]
    fromOKLCh(double[] lch)
    Converts a polar OKLCh point back to OKLab.
    static boolean
    inGamut(double[] lab)
    Checks if an OKLab point is inside the sRGB gamut.
    static double
    toLinear(double c)
    Converts one sRGB channel to linear light (the sRGB transfer function, IEC 61966-2-1).
    static double[]
    toLinearRGB(double[] lab)
    Converts an OKLab point to linear sRGB, unclamped.
    static double[]
    toOKLab(int argb)
    Converts a color to OKLab; the alpha is ignored.
    static double[]
    toOKLCh(double[] lab)
    Converts an OKLab point to its polar form.
  • Method Details

    • toLinear

      public static double toLinear(double c)
      Converts one sRGB channel to linear light (the sRGB transfer function, IEC 61966-2-1).
      Parameters:
      c - The channel, 0 to 1.
      Returns:
      The linear channel, 0 to 1.
    • fromLinear

      public static double fromLinear(double c)
      Converts one linear channel back to sRGB.
      Parameters:
      c - The linear channel, 0 to 1.
      Returns:
      The sRGB channel, 0 to 1.
    • toOKLab

      public static double[] toOKLab(int argb)
      Converts a color to OKLab; the alpha is ignored.
      Parameters:
      argb - The color.
      Returns:
      L, a and b.
    • toLinearRGB

      public static double[] toLinearRGB(double[] lab)
      Converts an OKLab point to linear sRGB, unclamped.
      Parameters:
      lab - L, a and b.
      Returns:
      The linear red, green and blue, possibly outside 0..1.
    • inGamut

      public static boolean inGamut(double[] lab)
      Checks if an OKLab point is inside the sRGB gamut.
      Parameters:
      lab - L, a and b.
      Returns:
      true when every linear channel is within 0..1.
    • fromOKLab

      public static int fromOKLab(double[] lab, int alpha)
      Converts an OKLab point to a color, each channel clamped to the gamut and rounded.
      Parameters:
      lab - L, a and b.
      alpha - The alpha of the result, 0 to 255.
      Returns:
      The color as ARGB.
    • toOKLCh

      public static double[] toOKLCh(double[] lab)
      Converts an OKLab point to its polar form.
      Parameters:
      lab - L, a and b.
      Returns:
      L, the chroma C (0 for a grey) and the hue h in degrees, 0 to 360.
    • fromOKLCh

      public static double[] fromOKLCh(double[] lch)
      Converts a polar OKLCh point back to OKLab.
      Parameters:
      lch - L, C and h in degrees.
      Returns:
      L, a and b.