Class ColorUtils

java.lang.Object
com.github.tommyettinger.textra.utils.ColorUtils

public class ColorUtils extends Object
Just has methods to convert from HSL colors to RGB and back again, for hue-changing effects mainly.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    hsl2rgb​(float h, float s, float l, float a)
    Converts the four HSLA components, each in the 0.0 to 1.0 range, to an int in RGBA8888 format.
    static int
    lerpColors​(int s, int e, float change)
    Interpolates from the RGBA8888 int color start towards end by change.
    static float
    rgb2hsl​(float r, float g, float b, float a)
    Converts the four RGBA components, each in the 0.0 to 1.0 range, to an int in "HSLA format" (hue, saturation, lightness, alpha).

    Methods inherited from class java.lang.Object

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

    • ColorUtils

      public ColorUtils()
  • Method Details

    • hsl2rgb

      public static int hsl2rgb(float h, float s, float l, float a)
      Converts the four HSLA components, each in the 0.0 to 1.0 range, to an int in RGBA8888 format. I brought this over from colorful-gdx's FloatColors class. I can't recall where I got the original HSL(A) code from, but there's a strong chance it was written by cypherdare/cyphercove for their color space comparison.
      Parameters:
      h - hue, from 0.0 to 1.0
      s - saturation, from 0.0 to 1.0
      l - lightness, from 0.0 to 1.0
      a - alpha, from 0.0 to 1.0
      Returns:
      an RGBA8888-format int
    • rgb2hsl

      public static float rgb2hsl(float r, float g, float b, float a)
      Converts the four RGBA components, each in the 0.0 to 1.0 range, to an int in "HSLA format" (hue, saturation, lightness, alpha). This format is exactly like RGBA8888 but treats what would normally be red as hue, green as saturation, and blue as lightness; alpha is the same.
      Parameters:
      r - red, from 0.0 to 1.0
      g - green, from 0.0 to 1.0
      b - blue, from 0.0 to 1.0
      a - alpha, from 0.0 to 1.0
      Returns:
      an "HSLA-format" int
    • lerpColors

      public static int lerpColors(int s, int e, float change)
      Interpolates from the RGBA8888 int color start towards end by change. Both start and end should be RGBA8888 ints, and change can be between 0f (keep start) and 1f (only use end). This is a good way to reduce allocations of temporary Colors.
      Parameters:
      s - the starting color as a packed int
      e - the end/target color as a packed int
      change - how much to go from start toward end, as a float between 0 and 1; higher means closer to end
      Returns:
      an RGBA8888 int that represents a color between start and end