public class ColorUtils
extends java.lang.Object
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 and Description |
|---|
ColorUtils() |
| Modifier and Type | Method and Description |
|---|---|
static float |
channel(int color,
int channel)
Given a packed int color and a channel value from 0 to 3, gets the value of that channel as a float from 0.0f
to 1.0f .
|
static int |
channelInt(int color,
int channel)
Given a packed int color and a channel value from 0 to 3, gets the value of that channel as an int from 0 to
255 .
|
static int |
darken(int start,
float change)
Interpolates from the int color start towards black by change.
|
static int |
describe(java.lang.String description)
Parses a color description and returns the approximate color it describes, as an RGBA8888 int color.
|
static int |
dullen(int start,
float change)
Brings the chromatic components of
start closer to grayscale by change (desaturating them). |
static int |
enrich(int start,
float change)
Pushes the chromatic components of
start away from grayscale by change (saturating them). |
static int |
hsb2rgb(float h,
float s,
float b,
float a)
Converts the four HSBA/HSVA components, each in the 0.0 to 1.0 range, to an int in RGBA8888 format.
|
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 int |
lighten(int start,
float change)
Interpolates from the int color start towards white by change.
|
static int |
mix(int[] colors,
int offset,
int size)
Given several colors, this gets an even mix of all colors in equal measure.
|
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 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.
|
static int |
offset(int color,
float power)
Gets an "offset color" for the original
color where high red, green, or blue channels become low values
in that same channel, and vice versa, then blends the original with that offset, using more of the offset if
power is higher (closer to 1.0f). |
static int |
rgb2hsb(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 HSBA/HSVA format (hue,
saturation, brightness/value, alpha).
|
static int |
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).
|
public static int hsl2rgb(float h,
float s,
float l,
float a)
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.0public static int rgb2hsl(float r,
float g,
float b,
float a)
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.0public static int hsb2rgb(float h,
float s,
float b,
float a)
h - hue, from 0.0 to 1.0s - saturation, from 0.0 to 1.0b - brightness, from 0.0 to 1.0a - alpha, from 0.0 to 1.0public static int rgb2hsb(float r,
float g,
float b,
float a)
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.0public static float channel(int color,
int channel)
HSLA ints,
channel 1 refers to G or S, 2 refers to B or L, and 3 always refers to A.color - a packed int color in any 32-bit, 4-channel formatchannel - which channel to access, as an index from 0 to 3 inclusivepublic static int channelInt(int color,
int channel)
HSLA ints,
channel 1 refers to G or S, 2 refers to B or L, and 3 always refers to A.color - a packed int color in any 32-bit, 4-channel formatchannel - which channel to access, as an index from 0 to 3 inclusivepublic static int lerpColors(int s,
int e,
float change)
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 endpublic static int mix(int[] colors,
int offset,
int size)
colors is null or has no items, this returns 256 (a transparent placeholder used by
ColorLookup for "no color found").
This is mostly useful in conjunction with IntArray, using its items
for colors, typically 0 for offset, and its size for size.colors - an array of RGBA8888 int colors; all should use the same color spaceoffset - the index of the first item in colors to usesize - how many items from colors to usepublic static int lighten(int start,
float change)
lerpColors(int, int, float) to lerp towards
white. Unlike lerpColors(int, int, float), this keeps the alpha of start as-is.start - the starting color as an RGBA8888 intchange - how much to go from start toward white, as a float between 0 and 1; higher means closer to whitethe counterpart method that darkens an int colorpublic static int darken(int start,
float change)
lerpColors(int, int, float) to lerp towards
black. Unlike lerpColors(int, int, float), this keeps the alpha of start as-is.start - the starting color as an RGBA8888 intchange - how much to go from start toward black, as a float between 0 and 1; higher means closer to blackthe counterpart method that lightens an int colorpublic static int dullen(int start,
float change)
start closer to grayscale by change (desaturating them). While
change should be between 0f (return start as-is) and 1f (return fully gray), start should be an RGBA8888 int
color. This leaves alpha alone.
start - the starting color as an RGBA8888 intchange - how much to change start to a desaturated color, as a float between 0 and 1; higher means a less saturated resultthe counterpart method that makes an int color more saturatedpublic static int enrich(int start,
float change)
start away from grayscale by change (saturating them). While change
should be between 0f (return start as-is) and 1f (return maximally saturated), start should be an RGBA8888 int
color.
start - the starting color as an RGBA8888 intchange - how much to change start to a saturated color, as a float between 0 and 1; higher means a more saturated resultthe counterpart method that makes an int color less saturatedpublic static int offset(int color,
float power)
color where high red, green, or blue channels become low values
in that same channel, and vice versa, then blends the original with that offset, using more of the offset if
power is higher (closer to 1.0f). It is usually fine for power to be 0.5f .color - the original color as an RGBA8888 intpower - between 0.0f and 1.0f, this is how heavily the offset color should factor in to the resultcolor and its offset, with higher power using more of the offsetpublic static int multiplyAlpha(int color,
float multiplier)
color - an RGBA8888 or HSLA colormultiplier - a multiplier to apply to color's alphapublic static int[][] multiplyAllAlpha(int[][] colors,
float multiplier)
multiplyAlpha(int, float) internally, so its documentation applies.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 alphapublic static int describe(java.lang.String description)
Palette will be looked up in Palette.NAMED and tracked; if there is more than one of these color
name words, the colors will be mixed using mix(int[], int, int), or if there is just one color name
word, then the corresponding color will be used. The special adjectives "light" and "dark" change the lightness
of the described color; likewise, "rich" and "dull" change the saturation (how different the color is from
grayscale). All of these adjectives can have "-er" or "-est" appended to make their effect twice or three times
as strong. Technically, the chars appended to an adjective don't matter, only their count, so "lightaa" is the
same as "lighter" and "richcat" is the same as "richest". There's an unofficial fourth level as well, used when
any 4 characters are appended to an adjective (as in "darkmost"); it has four times the effect of the original
adjective. There are also the adjectives "bright" (equivalent to "light rich"), "pale" ("light dull"), "deep"
("dark rich"), and "weak" ("dark dull"). These can be amplified like the other four, except that "pale" goes to
"paler", "palest", and then to (equivalently) "palemax" or "palemost", where only the word length is checked.
ColorLookup).
description - a color description, as a lower-case String matching the above format