APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.graphics

public Class Color

extends Object
Class Inheritance
Imports
com.badlogic.gdx.math.MathUtils, com.badlogic.gdx.utils.NumberUtils

A color class, holding the r, g, b and alpha component as floats in the range [0,1]. All methods perform clamping on the internal values after execution.
Author
mzechner

Field Summary

Modifier and TypeField and Description
public float
a

the red, green, blue and alpha components

public float
b

the red, green, blue and alpha components

public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public float
g

the red, green, blue and alpha components

public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public float
r

the red, green, blue and alpha components

public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final Color
public static final float
WHITE_FLOAT_BITS

Convenience for frequently used WHITE.toFloatBits()

public static final Color

Constructor Summary

AccessConstructor and Description
public
Color()

Constructs a new Color with all components set to 0.

public
Color(int rgba8888)

public
Color(float
the red component
r
,
float
the green component
g
,
float
the blue component
b
,
float
the alpha component
a
)

Constructor, sets the components of the color

public
Color(Color
the color
color
)

Constructs a new color using the given color

Method Summary

Modifier and TypeMethod and Description
public static void
abgr8888ToColor(Color
The Color to be modified.
color
,
int value)

Sets the Color components using the specified integer value in the format ABGR8888.

public static void
abgr8888ToColor(Color
The Color to be modified.
color
,
float value)

Sets the Color components using the specified float value in the format ABGR8888.

public Color

Returns:

this color
add
(Color
the color
color
)

Adds the given color to this color.

public Color

Returns:

this Color for chaining
add
(float
Red component
r
,
float
Green component
g
,
float
Blue component
b
,
float
Alpha component
a
)

Adds the given color component values to this Color's values.

public static int
alpha(float alpha)

public static int
argb8888(float a, float r, float g, float b)

public static int
argb8888(Color color)

public static void
argb8888ToColor(Color
The Color to be modified.
color
,
int
An integer color value in ARGB8888 format.
value
)

Sets the Color components using the specified integer value in the format ARGB8888.

public Color

Returns:

this Color for chaining
clamp
()

Clamps this Color's components to a valid range [0 - 1]

public Color
cpy()

Returns: a copy of this color

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public Color

Returns:

The modified Color for chaining.
fromHsv
(float
The Hue in degree from 0 to 360
h
,
float
The Saturation from 0 to 1
s
,
float
The Value (brightness) from 0 to 1
v
)

Sets the RGB Color components using the specified Hue-Saturation-Value.

public Color

Returns:

The modified Color for chaining.
fromHsv
(float[]
The Hue, Saturation and Value components in that order.
hsv
)

Sets RGB components using the specified Hue-Saturation-Value.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public Color

Returns:

This color for chaining.
lerp
(final Color
The target color
target
,
final float
The interpolation coefficient
t
)

Linearly interpolates between this color and the target color by t which is in the range [0,1].

public Color

Returns:

This color for chaining.
lerp
(final float
The red component of the target color
r
,
final float
The green component of the target color
g
,
final float
The blue component of the target color
b
,
final float
The alpha component of the target color
a
,
final float
The interpolation coefficient
t
)

Linearly interpolates between this color and the target color by t which is in the range [0,1].

public static int
luminanceAlpha(float luminance, float alpha)

public Color

Returns:

this color.
mul
(Color
the color
color
)

Multiplies this color and the given color

public Color

Returns:

this color
mul
(float
the value
value
)

Multiplies all components of this Color with the given value.

public Color

Returns:

this Color for chaining
mul
(float
Red component
r
,
float
Green component
g
,
float
Blue component
b
,
float
Alpha component
a
)

Multiplies this Color's color components by the given ones.

public Color
premultiplyAlpha()

Multiplies the RGB values by the alpha.

public static int
rgb565(float r, float g, float b)

public static int
rgb565(Color color)

public static void
rgb565ToColor(Color
The Color to be modified.
color
,
int
An integer color value in RGB565 format.
value
)

Sets the Color components using the specified integer value in the format RGB565.

public static int
rgb888(float r, float g, float b)

public static int
rgb888(Color color)

public static void
rgb888ToColor(Color
The Color to be modified.
color
,
int
An integer color value in RGB888 format.
value
)

Sets the Color components using the specified integer value in the format RGB888.

public static int
rgba4444(float r, float g, float b, float a)

public static int
rgba4444(Color color)

public static void
rgba4444ToColor(Color
The Color to be modified.
color
,
int
An integer color value in RGBA4444 format.
value
)

Sets the Color components using the specified integer value in the format RGBA4444.

public static int
rgba8888(float r, float g, float b, float a)

public static int
rgba8888(Color color)

public static void
rgba8888ToColor(Color
The Color to be modified.
color
,
int
An integer color value in RGBA8888 format.
value
)

Sets the Color components using the specified integer value in the format RGBA8888.

public Color

Returns:

this color.
set
(Color
the Color
color
)

Sets this color to the given color.

public Color

Returns:

this color.
set
(Color
the desired red, green and blue values (alpha of that Color is ignored)
rgb
,
float
the desired alpha value (will be clamped to the range [0, 1])
alpha
)

Sets this color to the red, green and blue components of the provided Color and a deviating alpha value.

public Color

Returns:

this Color for chaining
set
(float
Red component
r
,
float
Green component
g
,
float
Blue component
b
,
float
Alpha component
a
)

Sets this Color's component values.

public Color

Returns:

this Color for chaining
set
(int rgba)

Sets this color's component values through an integer representation.

public Color

Returns:

this color
sub
(Color
the color
color
)

Subtracts the given color from this color

public Color

Returns:

this Color for chaining
sub
(float
Red component
r
,
float
Green component
g
,
float
Blue component
b
,
float
Alpha component
a
)

Subtracts the given values from this Color's component values.

public float

Returns:

the packed color as a 32-bit float
toFloatBits
()

Packs the color components into a 32-bit integer with the format ABGR and then converts it to a float.

public static float

Returns:

the packed color as a float
toFloatBits
(int
the red component, 0 - 255
r
,
int
the green component, 0 - 255
g
,
int
the blue component, 0 - 255
b
,
int
the alpha component, 0 - 255
a
)

Packs the color components into a 32-bit integer with the format ABGR and then converts it to a float.

public static float

Returns:

the packed color as a 32-bit float
toFloatBits
(float r, float g, float b, float a)

Packs the color components into a 32-bit integer with the format ABGR and then converts it to a float.

public float[]

Returns:

HSV components for chaining.
toHsv
(float[]
The HSV array to be modified.
hsv
)

Extract Hue-Saturation-Value.

public int

Returns:

the packed color as a 32-bit int.
toIntBits
()

Packs the color components into a 32-bit integer with the format ABGR.

public static int

Returns:

the packed color as a 32-bit int
toIntBits
(int
the red component, 0 - 255
r
,
int
the green component, 0 - 255
g
,
int
the blue component, 0 - 255
b
,
int
the alpha component, 0 - 255
a
)

Packs the color components into a 32-bit integer with the format ABGR.

public String
toString()

Overrides java.lang.Object.toString.

Returns the color encoded as hex string with the format RRGGBBAA.

public static Color
valueOf(String hex)

Returns a new color from a hex string with the format RRGGBBAA.

public static Color
valueOf(String hex, Color color)

Sets the specified color from a hex string with the format RRGGBBAA.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

aback to summary
public float a

the red, green, blue and alpha components

bback to summary
public float b

the red, green, blue and alpha components

BLACKback to summary
public static final Color BLACK
BLUEback to summary
public static final Color BLUE
BROWNback to summary
public static final Color BROWN
CHARTREUSEback to summary
public static final Color CHARTREUSE
CLEARback to summary
public static final Color CLEAR
CLEAR_WHITEback to summary
public static final Color CLEAR_WHITE
CORALback to summary
public static final Color CORAL
CYANback to summary
public static final Color CYAN
DARK_GRAYback to summary
public static final Color DARK_GRAY
FIREBRICKback to summary
public static final Color FIREBRICK
FORESTback to summary
public static final Color FOREST
gback to summary
public float g

the red, green, blue and alpha components

GOLDback to summary
public static final Color GOLD
GOLDENRODback to summary
public static final Color GOLDENROD
GRAYback to summary
public static final Color GRAY
GREENback to summary
public static final Color GREEN
LIGHT_GRAYback to summary
public static final Color LIGHT_GRAY
LIMEback to summary
public static final Color LIME
MAGENTAback to summary
public static final Color MAGENTA
MAROONback to summary
public static final Color MAROON
OLIVEback to summary
public static final Color OLIVE
ORANGEback to summary
public static final Color ORANGE
PINKback to summary
public static final Color PINK
PURPLEback to summary
public static final Color PURPLE
rback to summary
public float r

the red, green, blue and alpha components

REDback to summary
public static final Color RED
ROYALback to summary
public static final Color ROYAL
SALMONback to summary
public static final Color SALMON
SCARLETback to summary
public static final Color SCARLET
SKYback to summary
public static final Color SKY
SLATEback to summary
public static final Color SLATE
TANback to summary
public static final Color TAN
TEALback to summary
public static final Color TEAL
VIOLETback to summary
public static final Color VIOLET
WHITEback to summary
public static final Color WHITE
WHITE_FLOAT_BITSback to summary
public static final float WHITE_FLOAT_BITS

Convenience for frequently used WHITE.toFloatBits()

YELLOWback to summary
public static final Color YELLOW

Constructor Detail

Colorback to summary
public Color()

Constructs a new Color with all components set to 0.

Colorback to summary
public Color(int rgba8888)
See Also
rgba8888ToColor(Color, int)
Colorback to summary
public Color(float r, float g, float b, float a)

Constructor, sets the components of the color

Parameters
r:float

the red component

g:float

the green component

b:float

the blue component

a:float

the alpha component

Colorback to summary
public Color(Color color)

Constructs a new color using the given color

Parameters
color:Color

the color

Method Detail

abgr8888ToColorback to summary
public static void abgr8888ToColor(Color color, int value)

Sets the Color components using the specified integer value in the format ABGR8888.

Parameters
color:Color

The Color to be modified.

abgr8888ToColorback to summary
public static void abgr8888ToColor(Color color, float value)

Sets the Color components using the specified float value in the format ABGR8888.

Parameters
color:Color

The Color to be modified.

addback to summary
public Color add(Color color)

Adds the given color to this color.

Parameters
color:Color

the color

Returns:Color

this color

addback to summary
public Color add(float r, float g, float b, float a)

Adds the given color component values to this Color's values.

Parameters
r:float

Red component

g:float

Green component

b:float

Blue component

a:float

Alpha component

Returns:Color

this Color for chaining

alphaback to summary
public static int alpha(float alpha)
argb8888back to summary
public static int argb8888(float a, float r, float g, float b)
argb8888back to summary
public static int argb8888(Color color)
argb8888ToColorback to summary
public static void argb8888ToColor(Color color, int value)

Sets the Color components using the specified integer value in the format ARGB8888. This is the inverse to the argb8888(a, r, g, b) method

Parameters
color:Color

The Color to be modified.

value:int

An integer color value in ARGB8888 format.

clampback to summary
public Color clamp()

Clamps this Color's components to a valid range [0 - 1]

Returns:Color

this Color for chaining

cpyback to summary
public Color cpy()
Returns:Color

a copy of this color

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
fromHsvback to summary
public Color fromHsv(float h, float s, float v)

Sets the RGB Color components using the specified Hue-Saturation-Value. Note that HSV components are voluntary not clamped to preserve high range color and can range beyond typical values.

Parameters
h:float

The Hue in degree from 0 to 360

s:float

The Saturation from 0 to 1

v:float

The Value (brightness) from 0 to 1

Returns:Color

The modified Color for chaining.

fromHsvback to summary
public Color fromHsv(float[] hsv)

Sets RGB components using the specified Hue-Saturation-Value. This is a convenient method for fromHsv(float, float, float). This is the inverse of toHsv(float[]).

Parameters
hsv:float[]

The Hue, Saturation and Value components in that order.

Returns:Color

The modified Color for chaining.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
lerpback to summary
public Color lerp(final Color target, final float t)

Linearly interpolates between this color and the target color by t which is in the range [0,1]. The result is stored in this color.

Parameters
target:Color

The target color

t:float

The interpolation coefficient

Returns:Color

This color for chaining.

lerpback to summary
public Color lerp(final float r, final float g, final float b, final float a, final float t)

Linearly interpolates between this color and the target color by t which is in the range [0,1]. The result is stored in this color.

Parameters
r:float

The red component of the target color

g:float

The green component of the target color

b:float

The blue component of the target color

a:float

The alpha component of the target color

t:float

The interpolation coefficient

Returns:Color

This color for chaining.

luminanceAlphaback to summary
public static int luminanceAlpha(float luminance, float alpha)
mulback to summary
public Color mul(Color color)

Multiplies this color and the given color

Parameters
color:Color

the color

Returns:Color

this color.

mulback to summary
public Color mul(float value)

Multiplies all components of this Color with the given value.

Parameters
value:float

the value

Returns:Color

this color

mulback to summary
public Color mul(float r, float g, float b, float a)

Multiplies this Color's color components by the given ones.

Parameters
r:float

Red component

g:float

Green component

b:float

Blue component

a:float

Alpha component

Returns:Color

this Color for chaining

premultiplyAlphaback to summary
public Color premultiplyAlpha()

Multiplies the RGB values by the alpha.

rgb565back to summary
public static int rgb565(float r, float g, float b)
rgb565back to summary
public static int rgb565(Color color)
rgb565ToColorback to summary
public static void rgb565ToColor(Color color, int value)

Sets the Color components using the specified integer value in the format RGB565. This is inverse to the rgb565(r, g, b) method.

Parameters
color:Color

The Color to be modified.

value:int

An integer color value in RGB565 format.

rgb888back to summary
public static int rgb888(float r, float g, float b)
rgb888back to summary
public static int rgb888(Color color)
rgb888ToColorback to summary
public static void rgb888ToColor(Color color, int value)

Sets the Color components using the specified integer value in the format RGB888. This is inverse to the rgb888(r, g, b) method.

Parameters
color:Color

The Color to be modified.

value:int

An integer color value in RGB888 format.

rgba4444back to summary
public static int rgba4444(float r, float g, float b, float a)
rgba4444back to summary
public static int rgba4444(Color color)
rgba4444ToColorback to summary
public static void rgba4444ToColor(Color color, int value)

Sets the Color components using the specified integer value in the format RGBA4444. This is inverse to the rgba4444(r, g, b, a) method.

Parameters
color:Color

The Color to be modified.

value:int

An integer color value in RGBA4444 format.

rgba8888back to summary
public static int rgba8888(float r, float g, float b, float a)
rgba8888back to summary
public static int rgba8888(Color color)
rgba8888ToColorback to summary
public static void rgba8888ToColor(Color color, int value)

Sets the Color components using the specified integer value in the format RGBA8888. This is inverse to the rgba8888(r, g, b, a) method.

Parameters
color:Color

The Color to be modified.

value:int

An integer color value in RGBA8888 format.

setback to summary
public Color set(Color color)

Sets this color to the given color.

Parameters
color:Color

the Color

Returns:Color

this color.

setback to summary
public Color set(Color rgb, float alpha)

Sets this color to the red, green and blue components of the provided Color and a deviating alpha value.

Parameters
rgb:Color

the desired red, green and blue values (alpha of that Color is ignored)

alpha:float

the desired alpha value (will be clamped to the range [0, 1])

Returns:Color

this color.

setback to summary
public Color set(float r, float g, float b, float a)

Sets this Color's component values.

Parameters
r:float

Red component

g:float

Green component

b:float

Blue component

a:float

Alpha component

Returns:Color

this Color for chaining

setback to summary
public Color set(int rgba)

Sets this color's component values through an integer representation.

Returns:Color

this Color for chaining

See Also
rgba8888ToColor(Color, int)
subback to summary
public Color sub(Color color)

Subtracts the given color from this color

Parameters
color:Color

the color

Returns:Color

this color

subback to summary
public Color sub(float r, float g, float b, float a)

Subtracts the given values from this Color's component values.

Parameters
r:float

Red component

g:float

Green component

b:float

Blue component

a:float

Alpha component

Returns:Color

this Color for chaining

toFloatBitsback to summary
public float toFloatBits()

Packs the color components into a 32-bit integer with the format ABGR and then converts it to a float. Alpha is compressed from 0-255 to use only even numbers between 0-254 to avoid using float bits in the NaN range (see NumberUtils#intToFloatColor(int)). Converting a color to a float and back can be lossy for alpha.

Returns:float

the packed color as a 32-bit float

toFloatBitsback to summary
public static float toFloatBits(int r, int g, int b, int a)

Packs the color components into a 32-bit integer with the format ABGR and then converts it to a float. Note that no range checking is performed for higher performance.

Parameters
r:int

the red component, 0 - 255

g:int

the green component, 0 - 255

b:int

the blue component, 0 - 255

a:int

the alpha component, 0 - 255

Returns:float

the packed color as a float

See Also
NumberUtils#intToFloatColor(int)
toFloatBitsback to summary
public static float toFloatBits(float r, float g, float b, float a)

Packs the color components into a 32-bit integer with the format ABGR and then converts it to a float.

Returns:float

the packed color as a 32-bit float

See Also
NumberUtils#intToFloatColor(int)
toHsvback to summary
public float[] toHsv(float[] hsv)

Extract Hue-Saturation-Value. This is the inverse of fromHsv(float[]).

Parameters
hsv:float[]

The HSV array to be modified.

Returns:float[]

HSV components for chaining.

toIntBitsback to summary
public int toIntBits()

Packs the color components into a 32-bit integer with the format ABGR.

Returns:int

the packed color as a 32-bit int.

toIntBitsback to summary
public static int toIntBits(int r, int g, int b, int a)

Packs the color components into a 32-bit integer with the format ABGR. Note that no range checking is performed for higher performance.

Parameters
r:int

the red component, 0 - 255

g:int

the green component, 0 - 255

b:int

the blue component, 0 - 255

a:int

the alpha component, 0 - 255

Returns:int

the packed color as a 32-bit int

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the color encoded as hex string with the format RRGGBBAA.

valueOfback to summary
public static Color valueOf(String hex)

Returns a new color from a hex string with the format RRGGBBAA.

See Also
toString()
valueOfback to summary
public static Color valueOf(String hex, Color color)

Sets the specified color from a hex string with the format RRGGBBAA.

See Also
toString()