Class Color

java.lang.Object
org.eclipse.swt.graphics.Resource
org.eclipse.swt.graphics.Color

public final class Color extends Resource
Instances of this store color information. To create a color you can either specify the individual color components as integers in the range 0 to 255 or provide an instance of an RGB or RGBA.

Colors do not need to be disposed, however to maintain compatibility with older code, disposing a Color is not an error. As Colors do not require disposal, the constructors which do not require a Device are recommended.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    the handle to the OS color resource (Warning: This field is platform dependent)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Color(int red, int green, int blue)
    Constructs a new instance of this class given the desired red, green and blue values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
    Color(int red, int green, int blue, int alpha)
    Constructs a new instance of this class given the desired red, green, blue & alpha values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
    Color(Device device, int red, int green, int blue)
    Constructs a new instance of this class given a device and the desired red, green and blue values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
    Color(Device device, int red, int green, int blue, int alpha)
    Constructs a new instance of this class given a device and the desired red, green, blue & alpha values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
    Color(Device device, RGB rgb)
    Constructs a new instance of this class given a device and an RGB describing the desired red, green and blue values.
    Color(Device device, RGBA rgba)
    Constructs a new instance of this class given a device and an RGBA describing the desired red, green, blue & alpha values.
    Color(Device device, RGB rgb, int alpha)
    Constructs a new instance of this class given a device, an RGB describing the desired red, green and blue values, alpha specifying the level of transparency.
    Color(RGB rgb)
    Constructs a new instance of this class given an RGB describing the desired red, green and blue values.
    Color(RGBA rgba)
    Constructs a new instance of this class given an RGBA describing the desired red, green, blue & alpha values.
    Color(RGB rgb, int alpha)
    Constructs a new instance of this class given an RGB describing the desired red, green and blue values, alpha specifying the level of transparency.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Colors do not need to be disposed, however to maintain compatibility with older code, disposing a Color is not an error.
    boolean
    equals(Object object)
    Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
    int
    Returns the amount of alpha in the color, from 0 (transparent) to 255 (opaque).
    int
    Returns the amount of blue in the color, from 0 to 255.
    Returns the Device where this resource was created.
    int
    Returns the amount of green in the color, from 0 to 255.
    int
    Returns the amount of red in the color, from 0 to 255.
    Returns an RGB representing the receiver.
    Returns an RGBA representing the receiver.
    int
    Returns an integer hash code for the receiver.
    boolean
    Returns true if the color has been disposed, and false otherwise.
    Returns a string containing a concise, human-readable description of the receiver.
    static Color
    win32_new(Device device, int handle)
    Invokes platform specific functionality to allocate a new color.
    static Color
    win32_new(Device device, int handle, int alpha)
    Invokes platform specific functionality to allocate a new color.

    Methods inherited from class org.eclipse.swt.graphics.Resource

    setNonDisposeHandler

    Methods inherited from class java.lang.Object

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

    • handle

      public int handle
      the handle to the OS color resource (Warning: This field is platform dependent)

      IMPORTANT: This field is not part of the SWT public API. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms and should never be accessed from application code.

      Restriction:
      This field is not intended to be referenced by clients.
  • Constructor Details

    • Color

      public Color(Device device, int red, int green, int blue)
      Constructs a new instance of this class given a device and the desired red, green and blue values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
      Parameters:
      device - the device on which to allocate the color
      red - the amount of red in the color
      green - the amount of green in the color
      blue - the amount of blue in the color
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if device is null and there is no current device
      • ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255
      See Also:
    • Color

      public Color(int red, int green, int blue)
      Constructs a new instance of this class given the desired red, green and blue values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
      Parameters:
      red - the amount of red in the color
      green - the amount of green in the color
      blue - the amount of blue in the color
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255
      Since:
      3.115
    • Color

      public Color(Device device, int red, int green, int blue, int alpha)
      Constructs a new instance of this class given a device and the desired red, green, blue & alpha values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
      Parameters:
      device - the device on which to allocate the color
      red - the amount of red in the color
      green - the amount of green in the color
      blue - the amount of blue in the color
      alpha - the amount of alpha in the color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque).
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if device is null and there is no current device
      • ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255
      Since:
      3.104
      See Also:
    • Color

      public Color(int red, int green, int blue, int alpha)
      Constructs a new instance of this class given the desired red, green, blue & alpha values expressed as ints in the range 0 to 255 (where 0 is black and 255 is full brightness).
      Parameters:
      red - the amount of red in the color
      green - the amount of green in the color
      blue - the amount of blue in the color
      alpha - the amount of alpha in the color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque).
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255
      Since:
      3.115
    • Color

      public Color(Device device, RGB rgb)
      Constructs a new instance of this class given a device and an RGB describing the desired red, green and blue values.
      Parameters:
      device - the device on which to allocate the color
      rgb - the RGB values of the desired color
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if device is null and there is no current device
      • ERROR_NULL_ARGUMENT - if the rgb argument is null
      • ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255
      See Also:
    • Color

      public Color(RGB rgb)
      Constructs a new instance of this class given an RGB describing the desired red, green and blue values.
      Parameters:
      rgb - the RGB values of the desired color
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the rgb argument is null
      • ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255
      Since:
      3.115
    • Color

      public Color(Device device, RGBA rgba)
      Constructs a new instance of this class given a device and an RGBA describing the desired red, green, blue & alpha values.
      Parameters:
      device - the device on which to allocate the color
      rgba - the RGBA values of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque).
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if device is null and there is no current device
      • ERROR_NULL_ARGUMENT - if the rgba argument is null
      • ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255
      Since:
      3.104
      See Also:
    • Color

      public Color(RGBA rgba)
      Constructs a new instance of this class given an RGBA describing the desired red, green, blue & alpha values.
      Parameters:
      rgba - the RGBA values of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque).
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the rgba argument is null
      • ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255
      Since:
      3.115
    • Color

      public Color(Device device, RGB rgb, int alpha)
      Constructs a new instance of this class given a device, an RGB describing the desired red, green and blue values, alpha specifying the level of transparency.
      Parameters:
      device - the device on which to allocate the color
      rgb - the RGB values of the desired color
      alpha - the alpha value of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque).
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if device is null and there is no current device
      • ERROR_NULL_ARGUMENT - if the rgb argument is null
      • ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255
      Since:
      3.104
      See Also:
    • Color

      public Color(RGB rgb, int alpha)
      Constructs a new instance of this class given an RGB describing the desired red, green and blue values, alpha specifying the level of transparency.
      Parameters:
      rgb - the RGB values of the desired color
      alpha - the alpha value of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque).
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the rgb argument is null
      • ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255
      Since:
      3.115
  • Method Details

    • dispose

      public void dispose()
      Colors do not need to be disposed, however to maintain compatibility with older code, disposing a Color is not an error.
      Overrides:
      dispose in class Resource
    • getDevice

      public Device getDevice()
      Returns the Device where this resource was created. In cases where no Device was used at creation, returns the current or default Device.

      As Color does not require a Device it is recommended to not use getDevice().

      Overrides:
      getDevice in class Resource
      Returns:
      Device the device of the receiver
      Since:
      3.2
    • equals

      public boolean equals(Object object)
      Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare with this object
      Returns:
      true if the object is the same as this object and false otherwise
      See Also:
    • getAlpha

      public int getAlpha()
      Returns the amount of alpha in the color, from 0 (transparent) to 255 (opaque).
      Returns:
      the alpha component of the color
      Throws:
      SWTException -
      • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
      Since:
      3.104
    • getBlue

      public int getBlue()
      Returns the amount of blue in the color, from 0 to 255.
      Returns:
      the blue component of the color
      Throws:
      SWTException -
      • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • getGreen

      public int getGreen()
      Returns the amount of green in the color, from 0 to 255.
      Returns:
      the green component of the color
      Throws:
      SWTException -
      • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • getRed

      public int getRed()
      Returns the amount of red in the color, from 0 to 255.
      Returns:
      the red component of the color
      Throws:
      SWTException -
      • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • getRGB

      public RGB getRGB()
      Returns an RGB representing the receiver.
      Returns:
      the RGB for the color
      Throws:
      SWTException -
      • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
    • getRGBA

      public RGBA getRGBA()
      Returns an RGBA representing the receiver.
      Returns:
      the RGBA for the color
      Throws:
      SWTException -
      • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
      Since:
      3.104
    • hashCode

      public int hashCode()
      Returns an integer hash code for the receiver. Any two objects that return true when passed to equals must return the same value for this method.
      Overrides:
      hashCode in class Object
      Returns:
      the receiver's hash
      See Also:
    • isDisposed

      public boolean isDisposed()
      Returns true if the color has been disposed, and false otherwise.

      This method gets the dispose state for the color. When a color has been disposed, it is an error to invoke any other method (except dispose()) using the color.

      Specified by:
      isDisposed in class Resource
      Returns:
      true when the color is disposed and false otherwise
    • toString

      public String toString()
      Returns a string containing a concise, human-readable description of the receiver.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the receiver
    • win32_new

      public static Color win32_new(Device device, int handle)
      Invokes platform specific functionality to allocate a new color.

      IMPORTANT: This method is not part of the public API for Color. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms, and should never be called from application code.

      Parameters:
      device - the device on which to allocate the color
      handle - the handle for the color
      Returns:
      a new color object containing the specified device and handle
      Restriction:
      This method is not intended to be referenced by clients.
    • win32_new

      public static Color win32_new(Device device, int handle, int alpha)
      Invokes platform specific functionality to allocate a new color.

      IMPORTANT: This method is not part of the public API for Color. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms, and should never be called from application code.

      Parameters:
      device - the device on which to allocate the color
      handle - the handle for the color
      alpha - the int for the alpha content in the color
      Returns:
      a new color object containing the specified device and handle
      Restriction:
      This method is not intended to be referenced by clients.