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 the color as a combination for Red, Green, and Blue in the range of 0-255. It can also represent the color as a floating-point value between 0.0 to 1.0.
When colors offer us the chance to make outwardly satisfying applications, they likewise let us do things that are unbelievably monstrous. It’s simple to make something ugly when we are attempting to utilize each shading in the palette. Likewise, understand that Color is generally platform-specific in an exceptionally untidy way.
Java lets us utilize similar Color questions on any stage, and platform yet it can’t ensure that each show will treat the shading a similar way. The outcome relies upon everything from our product to the age of your screen. What looks pink on one screen might be red on another.
Besides, when running in a situation with a restricted palette, AWT picks the accessible shading that is nearest to what we mentioned. In the event that we truly care about appearance, there is not a viable replacement for testing.
The Color.getHSBColor() creates a color using hue,saturation and brightness color model.
brighter() and darker() are two useful methods to create a shading effect.
The getColor() methods look up a color name in the properties database and convert the integer into a Color Object.
We generally use color in setBackGround() and setForeGround() methods.
Color class in java defines several constants as colors. The Color class represents a specific color to the system.
constants are:
- black
- blue
- cyan
- dark gray
- gray
- green
- light gray
- magenta
- orange
- pink
- red
- yellow
- white
RGB and HSB values in Color
Color | Red | Green | Blue | Hue | Saturation | Brightness |
black | ||||||
blue | 255 | .666667 | 1 | 1 | ||
cyan | 255 | 255 | .5 | 1 | 1 | |
darkGray | 64 | 64 | 64 | .25098 | ||
gray | 128 | 128 | 128 | .501961 | ||
green | 255 | .333333 | 1 | 1 | ||
lightGray | 192 | 192 | 192 | .752941 | ||
magenta | 255 | 255 | .833333 | 1 | 1 | |
orange | 255 | 200 | .130719 | 1 | 1 | |
pink | 255 | 175 | 175 | .313726 | 1 | |
red | 255 | 1 | 1 | |||
white | 255 | 255 | 255 | 1 | ||
yellow | 255 | 255 | .166667 | 1 | 1 |
These constants are used like any other class variable: for example, Color.red is a constant Color object representing the color red.
When we are not using a predefined constant, we create Color objects by specifying the color’s red, green, and blue components. Depending on which constructor we use, we can specify the components as integers between 0 and 255 (most intense) or as floating-point intensities between 0.0 and 1.0 (most intense).
The result is a 24-bit quantity that represents a color. The remaining 8 bits are used to represent transparency The Color class doesn’t let us work with the transparency bits; all Color objects are opaque. However, we can use transparency when working with images;
Color properties are similar to Font properties. we can use system properties (or resource files) to allow users to select colors for our programs. The settings have the form 0xRRGGBB, where RR is the red component of the color, GG represents the green component, and BB represents the blue component.
0x indicates that the number is in hexadecimal. If we (or our user) are comfortable using decimal values for colors (0x112233 is 1122867 in decimal), we can, but then it is harder to see the values of the different
components.
Hue, saturation, and brightness
There are many other ways to represent colors. This group of methods allows us to work in terms of the HSB (hue, saturation, brightness) model.
Hue represents the base color to work with: working through the colors of the rainbow, red is represented by numbers immediately above 0; magenta is represented by numbers below 1; white is 0, and black is 1.
Saturation represents the color’s purity, ranging from completely unsaturated (either white or black depending upon brightness) to totally saturated ( just the base color present).
Brightness is the desired level of luminance, ranging from black (0) to the maximum amount determined by the saturation level.
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. This constructor allows us to provide floating-point values between 0.0 and 1.0 for each of red, green, and blue. Values outside of this range yield unpredictable results.
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 represent blue,8-15 represents green,16-23 represents red. This constructor allows us to combine all three variables in one parameter, rgb. Bits 16-23 represent the red component, and bits 8-15 represent the green component. Bits 0-7 represent the blue component. Bits 24-31 are ignored.
(((red & 0xFF) << 16 ) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0))
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). It is the most commonly used. we need to provide the specific red, green, and blue values for the color. Valid values for red, green, and blue are between 0 and 255. The constructor examines only the low-order byte of the integer and ignores anything outside the range, including the sign bit.
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.
The details of the class structure are given as follows:
public static final Color black;
public static final Color black represents the color black.
public static final Color blue;
public static final Color blue represents the color blue.
public static final Color cyan;
public static final Color cyan represents the color cyan.
public static final Color darkGray;
public static final Color darkGray represents the color dark gray.
public static final Color gray;
public static final Color gray represents the color gray.
public static final Color green;
public static final Color green represents the color green.
public static final Color lightGray;
public static final Color lightGray represents the color light gray.
public static final Color magenta;
public static final Color magenta represents the color magenta.
public static final Color orange;
public static final Color orange represents the color orange.
public static final Color pink;
public static final Color pink represents the color pink.
public static final Color red;
public static final Color red represents the color red.
public static final Color white;
public static final Color white represents the color white.
public static final Color yellow;
public static final Color yellow represents the color yellow.
public Color (int rgb);
public Color (int rgb) constructs a Color object with the given rgb value.
Parameter
rgb – Composite color value
public Color(int red, int green, int blue);
public Color (int red, int green, int blue) constructs a Color object with the given red, green, and blue values.
Parameter
red – Red component of color in the range[0, 255]
green – Green component of color in the range[0, 255]
blue – Blue component of color in the range[0, 255]
public Color (float red, float green, float blue);
public Color (float red, float green, float blue) constructs a Color object with the given red, green, and blue values.
Parameter
red – Red component of color in the range[0.0, 1.0]
green – Green component of color in the range[0.0, 1.0]
blue – Blue component of color in the range[0.0, 1.0]
Color APIs-methods:
public Color brighter();
This method creates a new Color that is a brighter version of this Color. It creates a new Color that is somewhat brighter than the current color. This method is useful if we want to highlight something on the screen.
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. It returns a new Color that is somewhat darker than the current color. This method is useful if we are trying to de-emphasize an object on the screen. If we are creating our own Component, we can use a darker() Color to mark it inactive.
public static Color decode(String nm);
This method converts a string to an integer and returns the specified Color. It gets color specified by the given string. It provides an explicit means to decipher color property settings, regardless of where the setting comes from.
(The getColor() method can decipher settings but only if they’re in the system properties file.) In particular, we can use decode() to look up color settings in a resource file. The format of the name is the same as that used by getColor().
If the contents of the name do not translate to a 24-bit integer, the NumberFormatException run-time exception is thrown.
This method returns the color requested.
Parameter
nm – A String representing a color as a 24-bit integer.
public boolean equals(Object obj);
This method checks the equality with another object to this Color. Two Color objects are equivalent if their red, green, and blue values are equal.
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. It gets color specified by the system property name.
The second variation gets the color specified by the system property name. This version of the getColor() method returns defaultColor if the name is not a valid system property or the
property’s value does not convert to an integer.
The third variation gets the color specified by the system property name. This version of the
getColor() method returns defaultColor if the name is not a valid system property or the
property’s value does not convert to an integer. The default color is specified as an integer in which bits 16-23 represent the red component, 8-15 represent the green component, and 0-7 represent the blue component. Bits 24-31 are ignored. If the property value does not convert to an integer, defaultColor is returned.
These methods return Color instances of name requested, or null if the name is invalid.
Parameter
name – The name of a system property indicating which color to fetch.
public String toString();
This method returns a string representation of this Color. This method of Color returns a string showing the color’s red, green, and blue settings.
For example
System.out.println (Color.orange);
The output will be:
java.awt.Color[r=255,g=200,b=0]
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. The hash code is used whenever a color is used as a key in a Hashtable.
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.
It converts a specific hue, saturation, and brightness to a Color and returns the red, green, and blue values as an integer. As with the constructor, bits 16-23 represent the red component, 8-15 represent the green component, and 0-7 represent the blue component. Bits 24-31 are ignored.
This method returns Color instance for values provided.
Parameters
hue – Hue component of Color to create, in the range[0.0, 1.0].
saturation – Saturation component of Color to create, in the range[0.0, 1.0].
brightness – Brightness component of Color to create, in the range[0.0, 1.0].
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. This method allows us to convert a specific red, green, blue value to the hue, saturation, and brightness equivalent. RGBtoHSB() returns the results in two different ways: the parameter hsbvals uses and the method’s return value.
The values of these are the same. If we do not want to pass an hsbvals array parameter, pass null. In both the parameter and the return value, the three components are placed in the array as follows:
- hsbvalues[0] contains hue
- hsbvalues[1] contains saturation
- hsbvalues[2] contains brightness
This are the details about the structure of Color class.
color class program in java
Since these are part of color class we can refer them using color name directly while coloring the container or a component.This will show the usage of APIs of Color class
like-
setBackGround(Color.blue)
We can also create specific color using a single integer to represent the RGB values or by using griup of three byte values.
setForeGround(new Color(0xff00dd)); setBackGround(new Color(255,0,0xdd));
These cases are applicable for color class in java applet also.
Color using Graphics2D
public void myComponent(Graphics g){ Graphics2D g2=(Graphics2D)g; g2.setColor(Color.blue) g2.drawString("Hello World",10,50); }