Class Colors

java.lang.Object
lol.trq.helios.color.Colors

public final class Colors extends Object
Packed 0xAARRGGBB colour maths. Every method is static and allocation-free, except where an output array is passed in.

Channels are 0-255 ints or 0-1 floats as named. Mixing happens in sRGB unless a method says linear.

  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    alpha(int c)
    Returns the alpha channel.
    static int
    argb(float r, float g, float b, float a)
    Packs float channels.
    static int
    argb(int r, int g, int b, int a)
    Packs channels.
    static int
    blue(int c)
    Returns the blue channel.
    static float
    brightness(int c)
    Returns the HSB brightness.
    static String
    format(int c)
    Formats a colour as #AARRGGBB, or #RRGGBB when opaque.
    static int
    green(int c)
    Returns the green channel.
    static int
    hsb(float hue, float saturation, float brightness, float alpha)
    Builds a colour from hue, saturation and brightness.
    static int
    hsl(float hue, float saturation, float lightness, float alpha)
    Builds a colour from hue, saturation and lightness.
    static float
    hue(int c)
    Returns the hue.
    static int
    invert(int c)
    Inverts the RGB channels, keeping alpha.
    static int
    lerp(int a, int b, float t)
    Mixes two colours per channel, alpha included, in sRGB.
    static int
    lerpHsb(int a, int b, float t)
    Mixes two colours through HSB, taking the shorter way round the hue circle, so a red to blue blend passes through magenta rather than grey.
    static int
    lerpLinear(int a, int b, float t)
    Mixes two colours in linear light, which keeps the midpoint of a red to green blend from going muddy.
    static float
    lightness(int c)
    Returns the HSL lightness.
    static float
    luminance(int c)
    Returns the relative luminance (Rec.
    static int
    multiplyAlpha(int c, float factor)
    Scales the alpha channel, for fading something that already has an alpha.
    static int
    Parses #RGB, #RRGGBB or #AARRGGBB, with or without the #.
    static int
    pingPong(long timeMillis, long periodMillis, float offset, int a, int b)
    A colour sweeping from one colour to another and back over time.
    static int
    rainbow(long timeMillis, long periodMillis, float offset, float saturation, float brightness, float alpha)
    A colour cycling through the hue circle over time.
    static int
    readableOn(int background)
    Picks black or white text for a background, whichever contrasts more.
    static int
    red(int c)
    Returns the red channel.
    static float
    saturation(int c)
    Returns the HSB saturation.
    static int
    scale(int c, float factor)
    Scales the RGB channels, keeping alpha.
    static float
    toLinear(int channel)
    Converts an 8-bit sRGB channel to linear light.
    static float
    toSrgb(float linear)
    Converts linear light to sRGB.
    static int
    withAlpha(int c, float alpha)
    Replaces the alpha channel.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • argb

      public static int argb(int r, int g, int b, int a)
      Packs channels.
      Parameters:
      r - red 0-255
      g - green 0-255
      b - blue 0-255
      a - alpha 0-255
      Returns:
      the colour; channels are clamped
    • argb

      public static int argb(float r, float g, float b, float a)
      Packs float channels.
      Parameters:
      r - red 0-1
      g - green 0-1
      b - blue 0-1
      a - alpha 0-1
      Returns:
      the colour; channels are clamped
    • alpha

      public static int alpha(int c)
      Returns the alpha channel.
      Parameters:
      c - the colour
      Returns:
      alpha 0-255
    • red

      public static int red(int c)
      Returns the red channel.
      Parameters:
      c - the colour
      Returns:
      red 0-255
    • green

      public static int green(int c)
      Returns the green channel.
      Parameters:
      c - the colour
      Returns:
      green 0-255
    • blue

      public static int blue(int c)
      Returns the blue channel.
      Parameters:
      c - the colour
      Returns:
      blue 0-255
    • withAlpha

      public static int withAlpha(int c, float alpha)
      Replaces the alpha channel.
      Parameters:
      c - the colour
      alpha - the new alpha 0-1
      Returns:
      the colour with that alpha
    • multiplyAlpha

      public static int multiplyAlpha(int c, float factor)
      Scales the alpha channel, for fading something that already has an alpha.
      Parameters:
      c - the colour
      factor - the multiplier 0-1
      Returns:
      the faded colour
    • lerp

      public static int lerp(int a, int b, float t)
      Mixes two colours per channel, alpha included, in sRGB.
      Parameters:
      a - the colour at 0
      b - the colour at 1
      t - the mix, clamped to [0, 1]
      Returns:
      the mixed colour
    • lerpLinear

      public static int lerpLinear(int a, int b, float t)
      Mixes two colours in linear light, which keeps the midpoint of a red to green blend from going muddy. Alpha mixes linearly.
      Parameters:
      a - the colour at 0
      b - the colour at 1
      t - the mix, clamped to [0, 1]
      Returns:
      the mixed colour
    • lerpHsb

      public static int lerpHsb(int a, int b, float t)
      Mixes two colours through HSB, taking the shorter way round the hue circle, so a red to blue blend passes through magenta rather than grey.
      Parameters:
      a - the colour at 0
      b - the colour at 1
      t - the mix, clamped to [0, 1]
      Returns:
      the mixed colour
    • hsb

      public static int hsb(float hue, float saturation, float brightness, float alpha)
      Builds a colour from hue, saturation and brightness.
      Parameters:
      hue - hue, any value; whole turns wrap
      saturation - saturation 0-1
      brightness - brightness 0-1
      alpha - alpha 0-1
      Returns:
      the colour
    • hue

      public static float hue(int c)
      Returns the hue.
      Parameters:
      c - the colour
      Returns:
      hue in [0, 1); 0 for greys
    • saturation

      public static float saturation(int c)
      Returns the HSB saturation.
      Parameters:
      c - the colour
      Returns:
      saturation 0-1
    • brightness

      public static float brightness(int c)
      Returns the HSB brightness.
      Parameters:
      c - the colour
      Returns:
      brightness 0-1
    • hsl

      public static int hsl(float hue, float saturation, float lightness, float alpha)
      Builds a colour from hue, saturation and lightness.
      Parameters:
      hue - hue, any value; whole turns wrap
      saturation - HSL saturation 0-1
      lightness - lightness 0-1
      alpha - alpha 0-1
      Returns:
      the colour
    • lightness

      public static float lightness(int c)
      Returns the HSL lightness.
      Parameters:
      c - the colour
      Returns:
      lightness 0-1
    • scale

      public static int scale(int c, float factor)
      Scales the RGB channels, keeping alpha.
      Parameters:
      c - the colour
      factor - below 1 darkens, above 1 brightens
      Returns:
      the scaled colour
    • invert

      public static int invert(int c)
      Inverts the RGB channels, keeping alpha.
      Parameters:
      c - the colour
      Returns:
      the opposite colour
    • luminance

      public static float luminance(int c)
      Returns the relative luminance (Rec. 709 weights on linear light).
      Parameters:
      c - the colour
      Returns:
      luminance 0-1
    • readableOn

      public static int readableOn(int background)
      Picks black or white text for a background, whichever contrasts more.
      Parameters:
      background - the background colour
      Returns:
      0xFF000000 or 0xFFFFFFFF
    • rainbow

      public static int rainbow(long timeMillis, long periodMillis, float offset, float saturation, float brightness, float alpha)
      A colour cycling through the hue circle over time.
      Parameters:
      timeMillis - the current time
      periodMillis - how long one full cycle takes
      offset - phase offset 0-1, such as index * 0.05f for a wave down a list
      saturation - saturation 0-1
      brightness - brightness 0-1
      alpha - alpha 0-1
      Returns:
      the colour
    • pingPong

      public static int pingPong(long timeMillis, long periodMillis, float offset, int a, int b)
      A colour sweeping from one colour to another and back over time.
      Parameters:
      timeMillis - the current time
      periodMillis - how long a full there-and-back takes
      offset - phase offset 0-1
      a - the first colour
      b - the second colour
      Returns:
      the colour
    • parse

      public static int parse(String hex)
      Parses #RGB, #RRGGBB or #AARRGGBB, with or without the #.
      Parameters:
      hex - the text
      Returns:
      the colour; opaque unless alpha is given
      Throws:
      IllegalArgumentException - if the text is not a colour
    • format

      public static String format(int c)
      Formats a colour as #AARRGGBB, or #RRGGBB when opaque.
      Parameters:
      c - the colour
      Returns:
      the text, upper case
    • toLinear

      public static float toLinear(int channel)
      Converts an 8-bit sRGB channel to linear light.
      Parameters:
      channel - 0-255
      Returns:
      linear value 0-1
    • toSrgb

      public static float toSrgb(float linear)
      Converts linear light to sRGB.
      Parameters:
      linear - 0-1
      Returns:
      sRGB value 0-1