Class ColorHelper

java.lang.Object
de.gurkenlabs.litiengine.util.ColorHelper

public final class ColorHelper extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static Color
    decode(String colorHexString)
    Decodes the specified color string to an actual Color instance.
    static Color
    decode(String colorHexString, boolean solid)
    Decodes the specified color string to an actual Color instance, with an option to create a darker version of the base color.
    static String
    encode(Color color)
    Encodes the specified color to a hexadecimal string representation.
    static int
    ensureColorValueRange(float value)
    Ensures that the specified value lies within the accepted range for Color values (0-255).
    static int
    Ensures that the specified value lies within the accepted range for Color values (0-255).
    static Color
    getTransparentVariant(Color color, int newAlpha)
    Returns a transparent variant of the specified color with the given alpha value.
    static Color
    interpolate(Color color1, Color color2, double factor)
    Interpolates between two colors based on the given factor.
    static Color
    Premultiplies the alpha on the given color.

    Methods inherited from class java.lang.Object

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

    • encode

      public static String encode(Color color)
      Encodes the specified color to a hexadecimal string representation. The output format is:
      • #RRGGBB - For colors without alpha
      • #AARRGGBB - For colors with alpha

      Examples:
      Color.RED = "#ff0000"
      new Color(255, 0, 0, 200) = "#c8ff0000"

      Parameters:
      color - The color that is encoded.
      Returns:
      An hexadecimal string representation of the specified color.
      See Also:
    • decode

      public static Color decode(String colorHexString)
      Decodes the specified color string to an actual Color instance. The accepted format is:

      Note: This returns null if the format of the provided color string is invalid.

      • #RRGGBB - For colors without alpha
      • #AARRGGBB - For colors with alpha

      Examples:
      "#ff0000" = Color.RED
      "#c8ff0000" = new Color(255, 0, 0, 200)

      Parameters:
      colorHexString - The hexadecimal encodes color string representation.
      Returns:
      The decoded color.
      See Also:
    • decode

      public static Color decode(String colorHexString, boolean solid)
      Decodes the specified color string to an actual Color instance, with an option to create a darker version of the base color. The accepted format is:
      • #RRGGBB - For colors without alpha
      • #AARRGGBB - For colors with alpha

      If the `solid` parameter is true, the color alpha will create a darker version of the base color.

      Examples:
      "#ff0000" = Color.RED
      "#c8ff0000" = new Color(255, 0, 0, 200)

      Parameters:
      colorHexString - The hexadecimal encoded color string representation.
      solid - If true, the color alpha will create a darker version of the base color.
      Returns:
      The decoded Color object, or null if the input string is invalid.
      See Also:
    • ensureColorValueRange

      public static int ensureColorValueRange(float value)
      Ensures that the specified value lies within the accepted range for Color values (0-255). Smaller values will be forced to be 0 and larger values will result in 255.
      Parameters:
      value - The value to check for.
      Returns:
      An integer value that fits the color value restrictions.
    • ensureColorValueRange

      public static int ensureColorValueRange(int value)
      Ensures that the specified value lies within the accepted range for Color values (0-255). Smaller values will be forced to be 0 and larger values will result in 255.
      Parameters:
      value - The value to check for.
      Returns:
      An integer value that fits the color value restrictions.
    • premultiply

      public static Color premultiply(Color color)
      Premultiplies the alpha on the given color.
      Parameters:
      color - The color to premultiply
      Returns:
      The color given, with alpha replaced with a black background.
    • interpolate

      public static Color interpolate(Color color1, Color color2, double factor)
      Interpolates between two colors based on the given factor. The factor determines the weight of each color in the interpolation. A factor of 0.0 will return the first color, and a factor of 1.0 will return the second color. Intermediate values will return a blend of the two colors.
      Parameters:
      color1 - The first color.
      color2 - The second color.
      factor - The interpolation factor, ranging from 0.0 to 1.0.
      Returns:
      A new Color object that is the result of interpolating between the two colors.
    • getTransparentVariant

      public static Color getTransparentVariant(Color color, int newAlpha)
      Returns a transparent variant of the specified color with the given alpha value. The red, green, and blue components of the color remain unchanged.
      Parameters:
      color - The original color.
      newAlpha - The new alpha value to be applied to the color.
      Returns:
      A new Color object with the specified alpha value.