Class Color

All Implemented Interfaces:
HasHandlers

public class Color extends DataClass
A structured color object returned by Colors.getColor(). Contains RGB, HSL, and oklch properties pre-computed, a hex string, and convenience methods for manipulation that delegate to Colors classMethods.

Properties are organized by color space: RGB (r, g, b ), HSL (h, s, l), and oklch (ok_L, ok_C, ok_h). The ok_ prefix distinguishes oklch from HSL (both have a hue component). Oklch is the perceptually uniform color space that all adjust() operations work in - equal numeric changes produce equal perceived visual differences.

For raw numeric components without the full Color object, use Colors.getValues() which returns a plain unstructured object ({r, g, b, alpha}, {h, s, l, alpha}, or {L, C, h, alpha}) for a single requested format.

getColor() always returns a Color object. If the input was unparseable, isValid() returns false and properties default to black. Color objects are immutable - manipulation methods return new instances.

Instance methods return new Color objects, enabling chaining:

  • Constructor Details

  • Method Details

    • getOrCreateRef

      public static Color getOrCreateRef(JavaScriptObject jsObj)
    • getAlpha

      public Double getAlpha()
      Opacity, 0 (fully transparent) to 1 (fully opaque).
      Returns:
      Current alpha value. Default value is IR
    • getB

      public int getB()
      Blue component, 0-255.
      Returns:
      Current b value. Default value is IR
    • getFormat

      public ColorFormat getFormat()
      The format detected from the source string used to create this Color (e.g. "hex", "rgb", "hsl", "oklch"). Used as the default output format by getString().
      Returns:
      Current format value. Default value is IR
    • getG

      public int getG()
      Green component, 0-255.
      Returns:
      Current g value. Default value is IR
    • getH

      public int getH()
      HSL hue angle, 0-360 degrees. Standard CSS hue wheel: red 0/360, yellow 60, green 120, cyan 180, blue 240, magenta 300.
      Returns:
      Current h value. Default value is IR
    • getHex

      public String getHex()
      Pre-computed hex string representation of this color (e.g. "#3366cc"). Includes an alpha component ("#3366ccbf") only when semi-transparent. This is the most commonly needed output format and avoids repeated getString("hex") calls.
      Returns:
      Current hex value. Default value is IR
    • getL

      public int getL()
      HSL lightness, 0 (black) to 100 (white), as a percentage.
      Returns:
      Current l value. Default value is IR
    • getOk_C

      public Double getOk_C()
      Oklch chroma (color intensity), typically 0 (gray) to ~0.4 (vivid). This is the value that saturate()/desaturate() and the saturation key in adjust() operate on.
      Returns:
      Current ok_C value. Default value is IR
    • getOk_h

      public Double getOk_h()
      Oklch hue angle, 0-360 degrees. This is the value that the hue key in adjust() and complement() operate on. The oklch hue wheel: red ~30, yellow ~90, green ~145, cyan ~195, blue ~265, purple ~310.

      This differs from the HSL h because oklch redistributes hue angles for perceptual uniformity - equal angular steps produce equal perceived color changes.

      Returns:
      Current ok_h value. Default value is IR
    • getOk_L

      public Double getOk_L()
      Oklch perceptual lightness, 0 (black) to 1 (white). This is the value that lighten()/darken() and the lightness key in adjust() operate on. Oklch lightness is perceptually uniform - equal numeric steps produce equal perceived brightness changes, unlike HSL l.
      Returns:
      Current ok_L value. Default value is IR
    • getR

      public int getR()
      Red component, 0-255.
      Returns:
      Current r value. Default value is IR
    • getS

      public int getS()
      HSL saturation, 0 (gray) to 100 (fully saturated), as a percentage.
      Returns:
      Current s value. Default value is IR
    • adjust

      public Color adjust(Map deltas)
      Returns a new Color with the specified oklch deltas applied. Supports both friendly keys (lightness, saturation, hue, alpha) and raw oklch keys (L, C, h).
      Parameters:
      deltas - adjustments to apply - see Colors.adjust()
      Returns:
      new Color object
      See Also:
    • complement

      public Color complement()
      Returns a new Color with hue rotated 180 degrees (oklch h + 180).
      Returns:
      new Color object
      See Also:
    • contrast

      public Double contrast(String other)
      Returns the approximate perceptual contrast ratio between this color and another (oklch-based).
      Parameters:
      other - color to compare against
      Returns:
      approximate contrast ratio (>= 1.0), or null if other is invalid
      See Also:
    • contrast

      public Double contrast(Color other)
      Returns the approximate perceptual contrast ratio between this color and another (oklch-based).
      Parameters:
      other - color to compare against
      Returns:
      approximate contrast ratio (>= 1.0), or null if other is invalid
      See Also:
    • darken

      public Color darken(int amount)
      Returns a new Color darkened by the given amount (oklch L decreased).
      Parameters:
      amount - percentage to darken, 0-100
      Returns:
      new Color object
      See Also:
    • desaturate

      public Color desaturate(int amount)
      Returns a new Color with chroma decreased (oklch C decreased).
      Parameters:
      amount - percentage to desaturate, 0-100
      Returns:
      new Color object
      See Also:
    • equals

      public boolean equals(String other)
      Returns true if this color is visually identical to another (same RGB + alpha).
      Parameters:
      other - color to compare
      Returns:
      See Also:
    • equals

      public boolean equals(Color other)
      Returns true if this color is visually identical to another (same RGB + alpha).
      Parameters:
      other - color to compare
      Returns:
      See Also:
    • getString

      public String getString()
      Returns a CSS color string in the specified format, defaulting to the source format detected when this Color was created.
      Returns:
      CSS color string
    • getString

      public String getString(ColorFormat format)
      Returns a CSS color string in the specified format, defaulting to the source format detected when this Color was created.
      Parameters:
      format - output format - defaults to format
      Returns:
      CSS color string
    • isDark

      public boolean isDark()
      Returns true if this color is perceptually dark (oklch L < 0.6).
      Returns:
      See Also:
    • isValid

      public boolean isValid()
      Returns true if this Color was successfully parsed from valid input. Invalid Colors (created from unparseable input) have all properties defaulting to black (#000000). Use this to check user-supplied color strings without needing null checks.
      Returns:
      true if the color was parsed successfully
    • lighten

      public Color lighten(int amount)
      Returns a new Color lightened by the given amount (oklch L increased).
      Parameters:
      amount - percentage to lighten, 0-100
      Returns:
      new Color object
      See Also:
    • saturate

      public Color saturate(int amount)
      Returns a new Color with chroma increased (oklch C increased).
      Parameters:
      amount - percentage to saturate, 0-100
      Returns:
      new Color object
      See Also: