Class Colors
java.lang.Object
lol.trq.helios.color.Colors
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 TypeMethodDescriptionstatic intalpha(int c) Returns the alpha channel.static intargb(float r, float g, float b, float a) Packs float channels.static intargb(int r, int g, int b, int a) Packs channels.static intblue(int c) Returns the blue channel.static floatbrightness(int c) Returns the HSB brightness.static Stringformat(int c) Formats a colour as#AARRGGBB, or#RRGGBBwhen opaque.static intgreen(int c) Returns the green channel.static inthsb(float hue, float saturation, float brightness, float alpha) Builds a colour from hue, saturation and brightness.static inthsl(float hue, float saturation, float lightness, float alpha) Builds a colour from hue, saturation and lightness.static floathue(int c) Returns the hue.static intinvert(int c) Inverts the RGB channels, keeping alpha.static intlerp(int a, int b, float t) Mixes two colours per channel, alpha included, in sRGB.static intlerpHsb(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 intlerpLinear(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 floatlightness(int c) Returns the HSL lightness.static floatluminance(int c) Returns the relative luminance (Rec.static intmultiplyAlpha(int c, float factor) Scales the alpha channel, for fading something that already has an alpha.static intParses#RGB,#RRGGBBor#AARRGGBB, with or without the#.static intpingPong(long timeMillis, long periodMillis, float offset, int a, int b) A colour sweeping from one colour to another and back over time.static intrainbow(long timeMillis, long periodMillis, float offset, float saturation, float brightness, float alpha) A colour cycling through the hue circle over time.static intreadableOn(int background) Picks black or white text for a background, whichever contrasts more.static intred(int c) Returns the red channel.static floatsaturation(int c) Returns the HSB saturation.static intscale(int c, float factor) Scales the RGB channels, keeping alpha.static floattoLinear(int channel) Converts an 8-bit sRGB channel to linear light.static floattoSrgb(float linear) Converts linear light to sRGB.static intwithAlpha(int c, float alpha) Replaces the alpha channel.
-
Method Details
-
argb
public static int argb(int r, int g, int b, int a) Packs channels.- Parameters:
r- red 0-255g- green 0-255b- blue 0-255a- 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-1g- green 0-1b- blue 0-1a- 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 colouralpha- 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 colourfactor- 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 0b- the colour at 1t- 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 0b- the colour at 1t- 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 0b- the colour at 1t- 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 wrapsaturation- saturation 0-1brightness- brightness 0-1alpha- 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 wrapsaturation- HSL saturation 0-1lightness- lightness 0-1alpha- 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 colourfactor- 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:
0xFF000000or0xFFFFFFFF
-
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 timeperiodMillis- how long one full cycle takesoffset- phase offset 0-1, such asindex * 0.05ffor a wave down a listsaturation- saturation 0-1brightness- brightness 0-1alpha- 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 timeperiodMillis- how long a full there-and-back takesoffset- phase offset 0-1a- the first colourb- the second colour- Returns:
- the colour
-
parse
Parses#RGB,#RRGGBBor#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
Formats a colour as#AARRGGBB, or#RRGGBBwhen 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
-