Package com.badlogic.gdx.graphics
Class Color
- java.lang.Object
-
- com.badlogic.gdx.graphics.Color
-
public class Color extends java.lang.Object
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.
-
-
Field Summary
Fields Modifier and Type Field Description float
a
the red, green, blue and alpha componentsfloat
b
the red, green, blue and alpha componentsstatic Color
BLACK
static Color
BLUE
static Color
BROWN
static Color
CHARTREUSE
static Color
CLEAR
static Color
CORAL
static Color
CYAN
static Color
DARK_GRAY
static Color
FIREBRICK
static Color
FOREST
float
g
the red, green, blue and alpha componentsstatic Color
GOLD
static Color
GOLDENROD
static Color
GRAY
static Color
GREEN
static Color
LIGHT_GRAY
static Color
LIME
static Color
MAGENTA
static Color
MAROON
static Color
NAVY
static Color
OLIVE
static Color
ORANGE
static Color
PINK
static Color
PURPLE
float
r
the red, green, blue and alpha componentsstatic Color
RED
static Color
ROYAL
static Color
SALMON
static Color
SCARLET
static Color
SKY
static Color
SLATE
static Color
TAN
static Color
TEAL
static Color
VIOLET
static Color
WHITE
static float
WHITE_FLOAT_BITS
Convenience for frequently usedWHITE.toFloatBits()
static Color
YELLOW
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
abgr8888ToColor(Color color, float value)
Sets the Color components using the specified float value in the format ABGR8888.static void
abgr8888ToColor(Color color, int value)
Sets the Color components using the specified integer value in the format ABGR8888.Color
add(float r, float g, float b, float a)
Adds the given color component values to this Color's values.Color
add(Color color)
Adds the given color to this color.static int
alpha(float alpha)
static int
argb8888(float a, float r, float g, float b)
static int
argb8888(Color color)
static void
argb8888ToColor(Color color, int value)
Sets the Color components using the specified integer value in the format ARGB8888.Color
clamp()
Clamps this Color's components to a valid range [0 - 1]Color
cpy()
boolean
equals(java.lang.Object o)
Color
fromHsv(float[] hsv)
Sets RGB components using the specified Hue-Saturation-Value.Color
fromHsv(float h, float s, float v)
Sets the RGB Color components using the specified Hue-Saturation-Value.int
hashCode()
Color
lerp(float r, float g, float b, float a, float t)
Linearly interpolates between this color and the target color by t which is in the range [0,1].Color
lerp(Color target, float t)
Linearly interpolates between this color and the target color by t which is in the range [0,1].static int
luminanceAlpha(float luminance, float alpha)
Color
mul(float value)
Multiplies all components of this Color with the given value.Color
mul(float r, float g, float b, float a)
Multiplies this Color's color components by the given ones.Color
mul(Color color)
Multiplies the this color and the given colorColor
premultiplyAlpha()
Multiplies the RGB values by the alpha.static int
rgb565(float r, float g, float b)
static int
rgb565(Color color)
static void
rgb565ToColor(Color color, int value)
Sets the Color components using the specified integer value in the format RGB565.static int
rgb888(float r, float g, float b)
static int
rgb888(Color color)
static void
rgb888ToColor(Color color, int value)
Sets the Color components using the specified integer value in the format RGB888.static int
rgba4444(float r, float g, float b, float a)
static int
rgba4444(Color color)
static void
rgba4444ToColor(Color color, int value)
Sets the Color components using the specified integer value in the format RGBA4444.static int
rgba8888(float r, float g, float b, float a)
static int
rgba8888(Color color)
static void
rgba8888ToColor(Color color, int value)
Sets the Color components using the specified integer value in the format RGBA8888.Color
set(float r, float g, float b, float a)
Sets this Color's component values.Color
set(int rgba)
Sets this color's component values through an integer representation.Color
set(Color color)
Sets this color to the given color.Color
sub(float r, float g, float b, float a)
Subtracts the given values from this Color's component values.Color
sub(Color color)
Subtracts the given color from this colorfloat
toFloatBits()
Packs the color components into a 32-bit integer with the format ABGR and then converts it to a float.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.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.float[]
toHsv(float[] hsv)
Extract Hue-Saturation-Value.int
toIntBits()
Packs the color components into a 32-bit integer with the format ABGR.static int
toIntBits(int r, int g, int b, int a)
Packs the color components into a 32-bit integer with the format ABGR.java.lang.String
toString()
Returns the color encoded as hex string with the format RRGGBBAA.static Color
valueOf(java.lang.String hex)
Returns a new color from a hex string with the format RRGGBBAA.static Color
valueOf(java.lang.String hex, Color color)
Sets the specified color from a hex string with the format RRGGBBAA.
-
-
-
Field Detail
-
WHITE
public static final Color WHITE
-
LIGHT_GRAY
public static final Color LIGHT_GRAY
-
GRAY
public static final Color GRAY
-
DARK_GRAY
public static final Color DARK_GRAY
-
BLACK
public static final Color BLACK
-
WHITE_FLOAT_BITS
public static final float WHITE_FLOAT_BITS
Convenience for frequently usedWHITE.toFloatBits()
-
CLEAR
public static final Color CLEAR
-
BLUE
public static final Color BLUE
-
NAVY
public static final Color NAVY
-
ROYAL
public static final Color ROYAL
-
SLATE
public static final Color SLATE
-
SKY
public static final Color SKY
-
CYAN
public static final Color CYAN
-
TEAL
public static final Color TEAL
-
GREEN
public static final Color GREEN
-
CHARTREUSE
public static final Color CHARTREUSE
-
LIME
public static final Color LIME
-
FOREST
public static final Color FOREST
-
OLIVE
public static final Color OLIVE
-
YELLOW
public static final Color YELLOW
-
GOLD
public static final Color GOLD
-
GOLDENROD
public static final Color GOLDENROD
-
ORANGE
public static final Color ORANGE
-
BROWN
public static final Color BROWN
-
TAN
public static final Color TAN
-
FIREBRICK
public static final Color FIREBRICK
-
RED
public static final Color RED
-
SCARLET
public static final Color SCARLET
-
CORAL
public static final Color CORAL
-
SALMON
public static final Color SALMON
-
PINK
public static final Color PINK
-
MAGENTA
public static final Color MAGENTA
-
PURPLE
public static final Color PURPLE
-
VIOLET
public static final Color VIOLET
-
MAROON
public static final Color MAROON
-
r
public float r
the red, green, blue and alpha components
-
g
public float g
the red, green, blue and alpha components
-
b
public float b
the red, green, blue and alpha components
-
a
public float a
the red, green, blue and alpha components
-
-
Constructor Detail
-
Color
public Color()
Constructs a new Color with all components set to 0.
-
Color
public Color(int rgba8888)
- See Also:
rgba8888ToColor(Color, int)
-
Color
public Color(float r, float g, float b, float a)
Constructor, sets the components of the color- Parameters:
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha component
-
Color
public Color(Color color)
Constructs a new color using the given color- Parameters:
color
- the color
-
-
Method Detail
-
set
public Color set(Color color)
Sets this color to the given color.- Parameters:
color
- the Color
-
mul
public Color mul(Color color)
Multiplies the this color and the given color- Parameters:
color
- the color- Returns:
- this color.
-
mul
public Color mul(float value)
Multiplies all components of this Color with the given value.- Parameters:
value
- the value- Returns:
- this color
-
add
public Color add(Color color)
Adds the given color to this color.- Parameters:
color
- the color- Returns:
- this color
-
sub
public Color sub(Color color)
Subtracts the given color from this color- Parameters:
color
- the color- Returns:
- this color
-
clamp
public Color clamp()
Clamps this Color's components to a valid range [0 - 1]- Returns:
- this Color for chaining
-
set
public Color set(float r, float g, float b, float a)
Sets this Color's component values.- Parameters:
r
- Red componentg
- Green componentb
- Blue componenta
- Alpha component- Returns:
- this Color for chaining
-
set
public Color set(int rgba)
Sets this color's component values through an integer representation.- Returns:
- this Color for chaining
- See Also:
rgba8888ToColor(Color, int)
-
add
public Color add(float r, float g, float b, float a)
Adds the given color component values to this Color's values.- Parameters:
r
- Red componentg
- Green componentb
- Blue componenta
- Alpha component- Returns:
- this Color for chaining
-
sub
public Color sub(float r, float g, float b, float a)
Subtracts the given values from this Color's component values.- Parameters:
r
- Red componentg
- Green componentb
- Blue componenta
- Alpha component- Returns:
- this Color for chaining
-
mul
public Color mul(float r, float g, float b, float a)
Multiplies this Color's color components by the given ones.- Parameters:
r
- Red componentg
- Green componentb
- Blue componenta
- Alpha component- Returns:
- this Color for chaining
-
lerp
public Color lerp(Color target, 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
- The target colort
- The interpolation coefficient- Returns:
- This color for chaining.
-
lerp
public Color lerp(float r, float g, float b, float a, 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
- The red component of the target colorg
- The green component of the target colorb
- The blue component of the target colora
- The alpha component of the target colort
- The interpolation coefficient- Returns:
- This color for chaining.
-
premultiplyAlpha
public Color premultiplyAlpha()
Multiplies the RGB values by the alpha.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toFloatBits
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 (seeNumberUtils.intToFloatColor(int)
). Converting a color to a float and back can be lossy for alpha.- Returns:
- the packed color as a 32-bit float
-
toIntBits
public int toIntBits()
Packs the color components into a 32-bit integer with the format ABGR.- Returns:
- the packed color as a 32-bit int.
-
toString
public java.lang.String toString()
Returns the color encoded as hex string with the format RRGGBBAA.- Overrides:
toString
in classjava.lang.Object
-
valueOf
public static Color valueOf(java.lang.String hex)
Returns a new color from a hex string with the format RRGGBBAA.- See Also:
toString()
-
valueOf
public static Color valueOf(java.lang.String hex, Color color)
Sets the specified color from a hex string with the format RRGGBBAA.- See Also:
toString()
-
toFloatBits
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
- the red component, 0 - 255g
- the green component, 0 - 255b
- the blue component, 0 - 255a
- the alpha component, 0 - 255- Returns:
- the packed color as a float
- See Also:
NumberUtils.intToFloatColor(int)
-
toFloatBits
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:
- the packed color as a 32-bit float
- See Also:
NumberUtils.intToFloatColor(int)
-
toIntBits
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
- the red component, 0 - 255g
- the green component, 0 - 255b
- the blue component, 0 - 255a
- the alpha component, 0 - 255- Returns:
- the packed color as a 32-bit int
-
alpha
public static int alpha(float alpha)
-
luminanceAlpha
public static int luminanceAlpha(float luminance, float alpha)
-
rgb565
public static int rgb565(float r, float g, float b)
-
rgba4444
public static int rgba4444(float r, float g, float b, float a)
-
rgb888
public static int rgb888(float r, float g, float b)
-
rgba8888
public static int rgba8888(float r, float g, float b, float a)
-
argb8888
public static int argb8888(float a, float r, float g, float b)
-
rgb565
public static int rgb565(Color color)
-
rgba4444
public static int rgba4444(Color color)
-
rgb888
public static int rgb888(Color color)
-
rgba8888
public static int rgba8888(Color color)
-
argb8888
public static int argb8888(Color color)
-
rgb565ToColor
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
- The Color to be modified.value
- An integer color value in RGB565 format.
-
rgba4444ToColor
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
- The Color to be modified.value
- An integer color value in RGBA4444 format.
-
rgb888ToColor
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
- The Color to be modified.value
- An integer color value in RGB888 format.
-
rgba8888ToColor
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
- The Color to be modified.value
- An integer color value in RGBA8888 format.
-
argb8888ToColor
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
- The Color to be modified.value
- An integer color value in ARGB8888 format.
-
abgr8888ToColor
public static void abgr8888ToColor(Color color, int value)
Sets the Color components using the specified integer value in the format ABGR8888.- Parameters:
color
- The Color to be modified.
-
abgr8888ToColor
public static void abgr8888ToColor(Color color, float value)
Sets the Color components using the specified float value in the format ABGR8888.- Parameters:
color
- The Color to be modified.
-
fromHsv
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
- The Hue in degree from 0 to 360s
- The Saturation from 0 to 1v
- The Value (brightness) from 0 to 1- Returns:
- The modified Color for chaining.
-
fromHsv
public Color fromHsv(float[] hsv)
Sets RGB components using the specified Hue-Saturation-Value. This is a convenient method forfromHsv(float, float, float)
. This is the inverse oftoHsv(float[])
.- Parameters:
hsv
- The Hue, Saturation and Value components in that order.- Returns:
- The modified Color for chaining.
-
toHsv
public float[] toHsv(float[] hsv)
Extract Hue-Saturation-Value. This is the inverse offromHsv(float[])
.- Parameters:
hsv
- The HSV array to be modified.- Returns:
- HSV components for chaining.
-
cpy
public Color cpy()
- Returns:
- a copy of this color
-
-