Package com.jme3.math

Class ColorRGBA

java.lang.Object
com.jme3.math.ColorRGBA
All Implemented Interfaces:
Savable, Serializable, Cloneable

public final class ColorRGBA extends Object implements Savable, Cloneable, Serializable
ColorRGBA defines a color made from a collection of red, green and blue values stored in Linear color space. An alpha value determines is transparency.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    float
    The alpha component of the color.
    float
    The blue component of the color.
    static final ColorRGBA
    The color black (0,0,0).
    static final ColorRGBA
    The black color with no alpha (0, 0, 0, 0).
    static final ColorRGBA
    The color blue (0,0,1).
    static final ColorRGBA
    The color brown (65/255, 40/255, 25/255).
    static final ColorRGBA
    The color cyan (0,1,1).
    static final ColorRGBA
    The color gray (.2,.2,.2).
    float
    The green component of the color.
    static final ColorRGBA
    The color gray (.5,.5,.5).
    static final ColorRGBA
    The color green (0,1,0).
    static final ColorRGBA
    The color gray (.8,.8,.8).
    static final ColorRGBA
    The color magenta (1,0,1).
    static final ColorRGBA
    The color orange (251/255, 130/255,0).
    static final ColorRGBA
    The color pink (1, 0.68, 0.68).
    float
    The red component of the color.
    static final ColorRGBA
    The color red (1,0,0).
    static final ColorRGBA
    The color white (1,1,1).
    static final ColorRGBA
    The color yellow (1,1,0).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor instantiates a new ColorRGBA object.
    ColorRGBA(float r, float g, float b, float a)
    Constructor instantiates a new ColorRGBA object.
    Copy constructor creates a new ColorRGBA object, based on a provided color.
    Constructor creates a new ColorRGBA object, based on a provided Vector3f, at full opacity with a 1.0 alpha value by default
    Constructor creates a new ColorRGBA object, based on a provided Vector4f.
  • Method Summary

    Modifier and Type
    Method
    Description
    Adds each r,g,b,a of this ColorRGBA by the corresponding r,g,b,a of the given color and returns the result as a new ColorRGBA.
    Adds each component to the corresponding component of the argument and returns the result (this).
    byte[]
    Retrieves the component values of this ColorRGBA as a four element byte array in the order: r,g,b,a.
    int
    Retrieves the component values of this ColorRGBA as an int in a,b,g,r order.
    int
    Retrieves the component values of this ColorRGBA as an int in a,r,g,b order.
    int
    Retrieves the component values of this ColorRGBA as an int in r,g,b,a order.
    void
    Saturate that color ensuring all channels have a value between 0 and 1
    Create a copy of this color.
    boolean
    equals returns true if this ColorRGBA is logically equivalent to a given color.
    fromIntABGR(int color)
    Sets the RGBA values of this ColorRGBA with the given combined ABGR value Bits 24-31 are alpha, bits 16-23 are blue, bits 8-15 are green, bits 0-7 are red.
    fromIntARGB(int color)
    Sets the component values of this ColorRGBA with the given combined ARGB int.
    fromIntRGBA(int color)
    Sets the RGBA values of this ColorRGBA with the given combined RGBA value Bits 24-31 are red, bits 16-23 are green, bits 8-15 are blue, bits 0-7 are alpha.
    static ColorRGBA
    fromRGBA255(int r, int g, int b, int a)
    Converts a color from RGBA 255 values.
    float
    Retrieves the alpha component value of this ColorRGBA.
    Get the color in sRGB color space as a ColorRGBA.
    float
    Retrieves the blue component value of this ColorRGBA.
    float[]
    getColorArray retrieves the color values of this ColorRGBA as a four element float array in the order: r,g,b,a.
    float[]
    getColorArray(float[] store)
    Stores the current r,g,b,a values into the given array.
    float
    Retrieves the green component value of this ColorRGBA.
    float
    Retrieves the red component value of this ColorRGBA.
    int
    hashCode returns a unique code for this ColorRGBA based on its values.
    interpolateLocal(ColorRGBA finalColor, float changeAmount)
    Sets this ColorRGBA to the interpolation by changeAmount from this to the finalColor: this=(1-changeAmount)*this + changeAmount * finalColor
    interpolateLocal(ColorRGBA beginColor, ColorRGBA finalColor, float changeAmount)
    Sets this ColorRGBA to the interpolation by changeAmount from beginColor to finalColor: this=(1-changeAmount)*beginColor + changeAmount * finalColor
    mult(float scalar)
    Multiplies each r,g,b,a of this ColorRGBA by the given scalar and returns the result as a new ColorRGBA.
    Multiplies each r,g,b,a of this ColorRGBA by the corresponding r,g,b,a of the given color and returns the result as a new ColorRGBA.
    multLocal(float scalar)
    Multiplies each r,g,b,a of this ColorRGBA by the given scalar and returns the result (this).
    static ColorRGBA
    randomColor is a utility method that generates a random opaque color.
    void
    read(JmeImporter importer)
    De-serialize this color from the specified importer, for example when loading from a J3O file.
    set(float r, float g, float b, float a)
    set sets the RGBA values of this ColorRGBA.
    set(ColorRGBA rgba)
    set sets the values of this ColorRGBA to those set by a parameter color.
    set(Vector3f vec3)
    set sets the values of this ColorRGBA to those set by a parameter Vector3f.
    set(Vector4f vec4)
    set sets the values of this ColorRGBA to those set by a parameter Vector4f.
    setAlpha(float value)
    Sets the alpha color to the specified value.
    setAsSrgb(float r, float g, float b, float a)
    Sets the rgba channels of this color in sRGB color space.
    setBlue(float value)
    Sets the blue color to the specified value.
    setGreen(float value)
    Sets the green color to the specified value.
    setRed(float value)
    Sets the red color to the specified value.
    float[]
    toArray(float[] floats)
    Saves this ColorRGBA into the given float array.
    toString returns the string representation of this ColorRGBA.
    Transform this ColorRGBA to a Vector3f using x = r, y = g, z = b.
    Transform this ColorRGBA to a Vector4f using x = r, y = g, z = b, w = a.
    void
    Serialize this color to the specified exporter, for example when saving to a J3O file.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • Black

      public static final ColorRGBA Black
      The color black (0,0,0).
    • White

      public static final ColorRGBA White
      The color white (1,1,1).
    • DarkGray

      public static final ColorRGBA DarkGray
      The color gray (.2,.2,.2).
    • Gray

      public static final ColorRGBA Gray
      The color gray (.5,.5,.5).
    • LightGray

      public static final ColorRGBA LightGray
      The color gray (.8,.8,.8).
    • Red

      public static final ColorRGBA Red
      The color red (1,0,0).
    • Green

      public static final ColorRGBA Green
      The color green (0,1,0).
    • Blue

      public static final ColorRGBA Blue
      The color blue (0,0,1).
    • Yellow

      public static final ColorRGBA Yellow
      The color yellow (1,1,0).
    • Magenta

      public static final ColorRGBA Magenta
      The color magenta (1,0,1).
    • Cyan

      public static final ColorRGBA Cyan
      The color cyan (0,1,1).
    • Orange

      public static final ColorRGBA Orange
      The color orange (251/255, 130/255,0).
    • Brown

      public static final ColorRGBA Brown
      The color brown (65/255, 40/255, 25/255).
    • Pink

      public static final ColorRGBA Pink
      The color pink (1, 0.68, 0.68).
    • BlackNoAlpha

      public static final ColorRGBA BlackNoAlpha
      The black color with no alpha (0, 0, 0, 0).
    • r

      public float r
      The red component of the color. 0 is none and 1 is maximum red.
    • g

      public float g
      The green component of the color. 0 is none and 1 is maximum green.
    • b

      public float b
      The blue component of the color. 0 is none and 1 is maximum blue.
    • a

      public float a
      The alpha component of the color. 0 is transparent and 1 is opaque.
  • Constructor Details

    • ColorRGBA

      public ColorRGBA()
      Constructor instantiates a new ColorRGBA object. This color is the default "white" with all values 1.
    • ColorRGBA

      public ColorRGBA(float r, float g, float b, float a)
      Constructor instantiates a new ColorRGBA object. The values are defined as passed parameters. these values are assumed to be in linear space and stored as is. If you want to assign sRGB values use setAsSrgb(float, float, float, float)
      Parameters:
      r - The red component of this color.
      g - The green component of this ColorRGBA.
      b - The blue component of this ColorRGBA.
      a - The alpha component of this ColorRGBA.
    • ColorRGBA

      public ColorRGBA(ColorRGBA rgba)
      Copy constructor creates a new ColorRGBA object, based on a provided color.
      Parameters:
      rgba - The ColorRGBA object to copy.
    • ColorRGBA

      public ColorRGBA(Vector4f vec4)
      Constructor creates a new ColorRGBA object, based on a provided Vector4f.
      Parameters:
      vec4 - The Vector4f object that will have its x, y, z, and w values copied to this color's r, g, b, and a values respectively.
    • ColorRGBA

      public ColorRGBA(Vector3f vec3)
      Constructor creates a new ColorRGBA object, based on a provided Vector3f, at full opacity with a 1.0 alpha value by default
      Parameters:
      vec3 - The Vector3f object that will have its x, y, and z values copied to this color's r, g, and b values respectively.
  • Method Details

    • set

      public ColorRGBA set(float r, float g, float b, float a)
      set sets the RGBA values of this ColorRGBA. these values are assumed to be in linear space and stored as is. If you want to assign sRGB values use setAsSrgb(float, float, float, float)
      Parameters:
      r - The red component of this color.
      g - The green component of this color.
      b - The blue component of this color.
      a - The alpha component of this color.
      Returns:
      this
    • set

      public ColorRGBA set(ColorRGBA rgba)
      set sets the values of this ColorRGBA to those set by a parameter color.
      Parameters:
      rgba - The color to set this ColorRGBA to.
      Returns:
      this
    • set

      public ColorRGBA set(Vector4f vec4)
      set sets the values of this ColorRGBA to those set by a parameter Vector4f.
      Parameters:
      vec4 - The 4 component vector that will have its x, y, z, and w values copied to this ColorRGBA's r, g, b, and a values respectively.
      Returns:
      this
    • set

      public ColorRGBA set(Vector3f vec3)
      set sets the values of this ColorRGBA to those set by a parameter Vector3f.
      Parameters:
      vec3 - The 3 component vector that will have its x, y, and z values copied to this ColorRGBA's r, g, and b values respectively.
      Returns:
      this
    • setRed

      public ColorRGBA setRed(float value)
      Sets the red color to the specified value.
      Parameters:
      value - the value to set the red channel.
      Returns:
      the ColorRGBA object with the modified value.
    • setGreen

      public ColorRGBA setGreen(float value)
      Sets the green color to the specified value.
      Parameters:
      value - the value to set the green channel.
      Returns:
      the ColorRGBA object with the modified value.
    • setBlue

      public ColorRGBA setBlue(float value)
      Sets the blue color to the specified value.
      Parameters:
      value - the value to set the blue channel.
      Returns:
      the ColorRGBA object with the modified value.
    • setAlpha

      public ColorRGBA setAlpha(float value)
      Sets the alpha color to the specified value.
      Parameters:
      value - the value to set the alpha channel.
      Returns:
      the ColorRGBA object with the modified value.
    • clamp

      public void clamp()
      Saturate that color ensuring all channels have a value between 0 and 1
    • getColorArray

      public float[] getColorArray()
      getColorArray retrieves the color values of this ColorRGBA as a four element float array in the order: r,g,b,a.
      Returns:
      The float array that contains the color components.
    • getColorArray

      public float[] getColorArray(float[] store)
      Stores the current r,g,b,a values into the given array. The given array must have a length of 4 or greater, or an array index out of bounds exception will be thrown.
      Parameters:
      store - The float array to store the values into.
      Returns:
      The float array after storage.
    • getAlpha

      public float getAlpha()
      Retrieves the alpha component value of this ColorRGBA.
      Returns:
      The alpha component value.
    • getRed

      public float getRed()
      Retrieves the red component value of this ColorRGBA.
      Returns:
      The red component value.
    • getBlue

      public float getBlue()
      Retrieves the blue component value of this ColorRGBA.
      Returns:
      The blue component value.
    • getGreen

      public float getGreen()
      Retrieves the green component value of this ColorRGBA.
      Returns:
      The green component value.
    • interpolateLocal

      public ColorRGBA interpolateLocal(ColorRGBA finalColor, float changeAmount)
      Sets this ColorRGBA to the interpolation by changeAmount from this to the finalColor: this=(1-changeAmount)*this + changeAmount * finalColor
      Parameters:
      finalColor - The final color to interpolate towards.
      changeAmount - An amount between 0.0 - 1.0 representing a percentage change from this towards finalColor.
      Returns:
      this ColorRGBA
    • interpolateLocal

      public ColorRGBA interpolateLocal(ColorRGBA beginColor, ColorRGBA finalColor, float changeAmount)
      Sets this ColorRGBA to the interpolation by changeAmount from beginColor to finalColor: this=(1-changeAmount)*beginColor + changeAmount * finalColor
      Parameters:
      beginColor - The beginning color (changeAmount=0).
      finalColor - The final color to interpolate towards (changeAmount=1).
      changeAmount - An amount between 0.0 - 1.0 representing a percentage change from beginColor towards finalColor.
      Returns:
      this ColorRGBA
    • randomColor

      public static ColorRGBA randomColor()
      randomColor is a utility method that generates a random opaque color.
      Returns:
      a random ColorRGBA with an alpha set to 1.
    • mult

      public ColorRGBA mult(ColorRGBA c)
      Multiplies each r,g,b,a of this ColorRGBA by the corresponding r,g,b,a of the given color and returns the result as a new ColorRGBA. Used as a way of combining colors and lights.
      Parameters:
      c - The color to multiply by.
      Returns:
      The new ColorRGBA. this*c
    • mult

      public ColorRGBA mult(float scalar)
      Multiplies each r,g,b,a of this ColorRGBA by the given scalar and returns the result as a new ColorRGBA. Used as a way of making colors dimmer or brighter.
      Parameters:
      scalar - The scalar to multiply by.
      Returns:
      The new ColorRGBA. this*scalar
    • multLocal

      public ColorRGBA multLocal(float scalar)
      Multiplies each r,g,b,a of this ColorRGBA by the given scalar and returns the result (this). Used as a way of making colors dimmer or brighter.
      Parameters:
      scalar - The scalar to multiply by.
      Returns:
      this*c
    • add

      public ColorRGBA add(ColorRGBA c)
      Adds each r,g,b,a of this ColorRGBA by the corresponding r,g,b,a of the given color and returns the result as a new ColorRGBA. Used as a way of combining colors and lights.
      Parameters:
      c - The color to add.
      Returns:
      The new ColorRGBA. this+c
    • addLocal

      public ColorRGBA addLocal(ColorRGBA c)
      Adds each component to the corresponding component of the argument and returns the result (this). Used as a way of combining colors and lights.
      Parameters:
      c - The color to add.
      Returns:
      this+c
    • toString

      public String toString()
      toString returns the string representation of this ColorRGBA. The format of the string is:
      Color[R.RRRR, G.GGGG, B.BBBB, A.AAAA]
      Overrides:
      toString in class Object
      Returns:
      The string representation of this ColorRGBA.
    • clone

      public ColorRGBA clone()
      Create a copy of this color.
      Overrides:
      clone in class Object
      Returns:
      a new instance, equivalent to this one
    • toArray

      public float[] toArray(float[] floats)
      Saves this ColorRGBA into the given float array.
      Parameters:
      floats - The float array to take this ColorRGBA. If null, a new float[4] is created.
      Returns:
      The array, with r,g,b,a float values in that order.
    • equals

      public boolean equals(Object o)
      equals returns true if this ColorRGBA is logically equivalent to a given color. That is, if all the components of the two colors are the same. False is returned otherwise.
      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare against.
      Returns:
      true if the colors are equal, false otherwise.
    • hashCode

      public int hashCode()
      hashCode returns a unique code for this ColorRGBA based on its values. If two colors are logically equivalent, they will return the same hash code value.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code value of this ColorRGBA.
    • write

      public void write(JmeExporter e) throws IOException
      Serialize this color to the specified exporter, for example when saving to a J3O file.
      Specified by:
      write in interface Savable
      Parameters:
      e - (not null)
      Throws:
      IOException - from the exporter
    • read

      public void read(JmeImporter importer) throws IOException
      De-serialize this color from the specified importer, for example when loading from a J3O file.
      Specified by:
      read in interface Savable
      Parameters:
      importer - (not null)
      Throws:
      IOException - from the importer
    • asBytesRGBA

      public byte[] asBytesRGBA()
      Retrieves the component values of this ColorRGBA as a four element byte array in the order: r,g,b,a.
      Returns:
      the byte array that contains the color components.
    • asIntARGB

      public int asIntARGB()
      Retrieves the component values of this ColorRGBA as an int in a,r,g,b order. Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue.
      Returns:
      The integer representation of this ColorRGBA in a,r,g,b order.
    • asIntRGBA

      public int asIntRGBA()
      Retrieves the component values of this ColorRGBA as an int in r,g,b,a order. Bits 24-31 are red, 16-23 are green, 8-15 are blue, 0-7 are alpha.
      Returns:
      The integer representation of this ColorRGBA in r,g,b,a order.
    • asIntABGR

      public int asIntABGR()
      Retrieves the component values of this ColorRGBA as an int in a,b,g,r order. Bits 24-31 are alpha, 16-23 are blue, 8-15 are green, 0-7 are red.
      Returns:
      The integer representation of this ColorRGBA in a,b,g,r order.
    • fromIntARGB

      public ColorRGBA fromIntARGB(int color)
      Sets the component values of this ColorRGBA with the given combined ARGB int. Bits 24-31 are alpha, bits 16-23 are red, bits 8-15 are green, bits 0-7 are blue.
      Parameters:
      color - The integer ARGB value used to set this ColorRGBA.
      Returns:
      this
    • fromIntRGBA

      public ColorRGBA fromIntRGBA(int color)
      Sets the RGBA values of this ColorRGBA with the given combined RGBA value Bits 24-31 are red, bits 16-23 are green, bits 8-15 are blue, bits 0-7 are alpha.
      Parameters:
      color - The integer RGBA value used to set this object.
      Returns:
      this
    • fromIntABGR

      public ColorRGBA fromIntABGR(int color)
      Sets the RGBA values of this ColorRGBA with the given combined ABGR value Bits 24-31 are alpha, bits 16-23 are blue, bits 8-15 are green, bits 0-7 are red.
      Parameters:
      color - The integer ABGR value used to set this object.
      Returns:
      this
    • fromRGBA255

      public static ColorRGBA fromRGBA255(int r, int g, int b, int a)
      Converts a color from RGBA 255 values.
      Parameters:
      r - the red value in 0-255 range.
      g - the green value in 0-255 range.
      b - the blue value in 0-255 range.
      a - the alpha value in 0-255 range.
      Returns:
      the ColorRGBA equivalent of the RGBA 255 color.
    • toVector3f

      public Vector3f toVector3f()
      Transform this ColorRGBA to a Vector3f using x = r, y = g, z = b. The Alpha value is not used. This method is useful for shader assignments.
      Returns:
      A Vector3f containing the RGB value of this ColorRGBA.
    • toVector4f

      public Vector4f toVector4f()
      Transform this ColorRGBA to a Vector4f using x = r, y = g, z = b, w = a. This method is useful for shader assignments.
      Returns:
      A Vector4f containing the RGBA value of this ColorRGBA.
    • setAsSrgb

      public ColorRGBA setAsSrgb(float r, float g, float b, float a)
      Sets the rgba channels of this color in sRGB color space. You probably want to use this method if the color is picked by the use in a color picker from a GUI. Note that the values will be gamma corrected to be stored in linear space GAMMA value is 2.2 Note that no correction will be performed on the alpha channel as it conventionally doesn't represent a color itself
      Parameters:
      r - the red value in sRGB color space
      g - the green value in sRGB color space
      b - the blue value in sRGB color space
      a - the alpha value
      Returns:
      this ColorRGBA with updated values.
    • getAsSrgb

      public ColorRGBA getAsSrgb()
      Get the color in sRGB color space as a ColorRGBA. Note that linear values stored in the ColorRGBA will be gamma corrected and returned as a ColorRGBA. The x attribute will be fed with the r channel in sRGB space. The y attribute will be fed with the g channel in sRGB space. The z attribute will be fed with the b channel in sRGB space. The w attribute will be fed with the a channel. Note that no correction will be performed on the alpha channel as it conventionally doesn't represent a color itself.
      Returns:
      the color in sRGB color space as a ColorRGBA.