Color class in Java With Example

0
33
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

"<yoastmark

This post talks about Color class in java,structure of Color class,APIs of Color class,color class in java applet,color class program in java.

All About Color in java:

The Color object represents a color. The color can be specified as RGB hexadecimal numbers or a named constant.The constructor color() describes a color as a combination for Red,Green and Blue in the range of 0-255. It can also represent color as floating point value between 0.0 to 1.0.

The Color.getHSBColor() creates a color using hue,saturation and brightness color model.

brighter() and darker() are two useful methods to create shading effect.

The getColor() methods look up a color name in the properties database and converts the integer into a Color Object.

We generally use color in setBackGround() and setForeGround() methods.

Color class in java defines several constants as colors

constants are:

  • black
  • blue
  • cyan
  • dark gray
  • gray
  • green
  • light gray
  • magenta
  • orange
  • pink
  • red
  • yellow
  • white

The structure of Color class and APIs of Color class

public class java.awt.Color extends java.lang.Object implements Serializable {
// Public Constructors
public Color(int r, int g, int b);
public Color(int rgb);
public Color(float r, float g, float b);
public Color(float r, float g, float b, float a);
public Color(int r, int g, int b, int a);
public Color(int rgba, boolean hasalpha);
public Color(ColorSpace colorspace, float[] components, float alpha);
// Constants
public static final Color black;
public static final Color blue;
public static final Color cyan;
public static final Color darkGray;
public static final Color gray;
public static final Color green;
public static final Color lightGray;
public static final Color magenta;
public static final Color orange;
public static final Color pink;
public static final Color red;
public static final Color white;
public static final Color yellow;
// Class Methods
public static int HSBtoRGB(float hue, float saturation, float
brightness);
public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals);
public static Color decode(String nm) throws NumberFormatException;
public static Color getColor(String nm);
public static Color getColor(String nm, Color v);
public static Color getColor(String nm, int v);
public static Color getHSBColor(float h, float s, float b);
// Public Instance Methods
public Color brighter();
public Color darker();
public boolean equals(Object obj); // Overrides Object
public static Color decode(String nm);
public int getBlue();
public int getGreen();
public int getRGB();
public int getRed();
public int getAlpha();
public static Color getColor(String nm);
public static Color getColor(String nm, Color v);
public static Color getColor(String nm, int v);
public float[] getColorComponents(ColorSpace cspace, float[] compArray);
public float[] getColorComponents(float[] compArray);
public ColorSpace getColorSpace();
public float[] getComponents(float[] compArray);
public float[] getComponents(ColorSpace cspace, float[] compArray);
public int hashCode(); // Overrides Object
public String toString(); // Overrides Object
public int getTransparency();
public static int HSBtoRGB(float hue, float saturation, float brightness);
public float[] getRGBComponents(float[] compArray);
public float[] getRGBColorComponents(float[] compArray);
public static Color getHSBColor(float h, float s, float b);
public PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints);
public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals);
}

Color APIs-Constructors:

public Color(ColorSpace colorspace, float[] components, float alpha)

This method creates a Color from the specified colorspec with components and alpha.

public Color(float r, float g, float b);

This method creates color with RGB with the range varying from 0.0 to 1.0

public Color(float r, float g, float b, float a);

This method creates color with RGB along with alpha with the range varying from 0.0 to 1.0

public Color(int rgb);

This method creates color with RGB where 0-7 bits represents blue,8-15 represents green,16-23 represents red.

public Color(int r, int g, int b);

This method creates a color with the specified red, green, and blue values in the range (0 – 255).

public Color(int r, int g, int b, int a);

This method creates a color with the specified red, green, blue and alpha values in the range (0 – 255).

public Color(int rgba, boolean hasalpha)

This method creates a color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

Color APIs-methods:

public Color brighter();

This method creates a new Color that is a brighter version of this Color.

public PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints);

This method creates and returns a PaintContext used to generate a solid color field pattern.

public Color darker();

This method creates a new Color that is a darker version of this Color.

public static Color decode(String nm);

This method converts a String to an integer and returns the specified Color.

public boolean equals(Object obj);

This method checks the equality with other object to this Color.

public int getAlpha();

This method returns the alpha component in the range 0-255.

public int getBlue();

This method returns the blue component in the range 0-255 .

public int getGreen();

This method returns the green component in the range 0-255.

public int getRed();

This method returns the red component in the range 0-255.

public int getRGB();

This method returns the RGB value representing the color.

public static Color getColor(String nm);
public static Color getColor(String nm, Color v);
public static Color getColor(String nm, int v);

These methods find a color in the system properties.

public String toString();

This method returns a string representation of this Color.

public float[] getColorComponents(ColorSpace cspace, float[] compArray);

This method returns a float array that contains the color components of the Color in the specified ColorSpace.

public float[] getColorComponents(float[] compArray);

This method returns a float array containing only the color components of the Color.

public ColorSpace getColorSpace();

This method returns the ColorSpace of this Color.

public float[] getComponents(ColorSpace cspace, float[] compArray);

This method returns a float array containing the color and alpha components of the Color for the specified ColorSpace.

public float[] getComponents(float[] compArray);

This method returns a float array containing the color and alpha components of the Color.

public int hashCode();

This method computes the hash code for this Color.

public static Color getHSBColor(float h, float s, float b);

This method creates a Color object based on the specified values for the HSB color model.

public float[] getRGBColorComponents(float[] compArray);

This method returns a float array containing only the color components of the Color.

public int getTransparency();

This method returns the transparency mode for this Color.

public float[] getRGBComponents(float[] compArray);

This method returns a float array containing the color and alpha components of the Color.

public static int HSBtoRGB(float hue, float saturation, float brightness);

This method converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.

public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals);

This method converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue, saturation, and brightness that are the three components of the HSB model.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

LEAVE A REPLY

Please enter your comment!
Please enter your name here