Class ColorUtils
java.lang.Object
com.github.tommyettinger.textra.utils.ColorUtils
A few static methods for commonly-used color handling tasks.
This has methods to convert from HSLA colors to RGBA and back again, for hue-changing effects mainly.
It also has
lerpColors(int, int, float) to blend RGBA colors, and multiplyAlpha(int, float) to
alter only the alpha channel on an RGBA or HSLA int color.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic inthsl2rgb(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 intlerpColors(int s, int e, float change) Interpolates from the RGBA8888 int color start towards end by change.static int[][]multiplyAllAlpha(int[][] colors, float multiplier) Given any purely-non-null 2D int array representing RGBA or HSLA colors, this multiplies the alpha channel of each color by multiplier, modifying the given array, and returns the changed array for chaining.static intmultiplyAlpha(int color, float multiplier) Given an RGBA8888 or HSLA color as an int, this multiplies the alpha of that color by multiplier and returns another int color of the same format passed in.static floatrgb2hsl(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).
-
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.0s- saturation, from 0.0 to 1.0l- lightness, from 0.0 to 1.0a- 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.0g- green, from 0.0 to 1.0b- blue, from 0.0 to 1.0a- 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 inte- the end/target color as a packed intchange- 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
-
multiplyAlpha
public static int multiplyAlpha(int color, float multiplier) Given an RGBA8888 or HSLA color as an int, this multiplies the alpha of that color by multiplier and returns another int color of the same format passed in. This clamps the alpha if it would go below 0 or above 255, and leaves the RGB or HSL channels alone.- Parameters:
color- an RGBA8888 or HSLA colormultiplier- a multiplier to apply to color's alpha- Returns:
- another color of the same format as the one given, with alpha multiplied
-
multiplyAllAlpha
public static int[][] multiplyAllAlpha(int[][] colors, float multiplier) Given any purely-non-null 2D int array representing RGBA or HSLA colors, this multiplies the alpha channel of each color by multiplier, modifying the given array, and returns the changed array for chaining. This usesmultiplyAlpha(int, float)internally, so its documentation applies.- Parameters:
colors- a 2D int array of RGBA or HSLA colors, none of which can include null arraysmultiplier- a multiplier to apply to each color's alpha- Returns:
- colors, after having each item's alpha multiplied
-