Showing posts with label java color rgb. Show all posts
Showing posts with label java color rgb. Show all posts

Monday, 1 July 2019

Java Color - Online Help

Java color:

Java provides the color class contained in java.awt.package. The Java color class is used to control the color of the graphics object. This class contains methods and constants for manipulating color in applet program. The color class is the part of java Abstract Windowing Toolkit (AWT).The color class creates color by using RGBA or HSB value components.

RGB stands for “Red, Green, and Blue”. Every color is made up of red, green and blue components, each represents by an unsigned byte value that describes its intensity. The value for the individual components RGB ranges from 0 to 255 where 0 represent the “darkest shade” and 255 represent the “lightest shade”. This is known as the RGB model.

If we specify the value beyond the above range then the “IllegalArgumentException” exception will be raised.

For example,
   
                                                Color c=newColor(255,200,0)

            Creates a color object which represents the color object, the first argument (i.e.255) represents the amount of red, the second argument (i.e.200)  represents the amount of green and the third argument (i.e.0) represents the amount of blue.


Also, color components can be represented by floating point value in the range from 0.0 to 0.1.

The following table shows the color constants used in color class.

Color constants
RGB Values
Red
255,0,0
Green
0,255,0
Blue
0,0,255
Orange
255,200,0
Pink
235,175,175
Magenta
255,0,255
Cyan
0,255,255
Black
0,0,0
White
255,255,255
Yellow
255,255,0
Grey
128,128,128
Light grey
192,192,192
Dark grey
64,64,64

Constructors in java color:

The java color class defines the following constructors:

1.Color(int r, int g, int b)
Creates an opaque color with specified RGB components (values are in range 0 – 255)

2.Color(int r, int g, int b, int a)
Creates a color with specified RGBA components (values are in range 0 – 255)

3.Color(float r, float g, float b)
Creates an opaque color with specified RGB components (values are in range 0.0 – 0.1)

4.Color(float r, float g, float b, float a)
Creates a color with specified RGBA components(values are in range 0.0 – 0.1)

5.Color(int rgb)
Creates an opaque RGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8 – 15, and the blue component in bits 0-7.

6.Color(int rgba, boolean hasalpha)
Creates an RGB 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.

7.Color(ColorSpace cspace, float[] components, float alpha)
Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.



Methods available in Java Color:

The most commonly used color() methods are as follows:
  
1.Color brighter()
Creates a new Color that is a brighter version of this Color.
           
2.PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints)
Creates and returns a PaintContext used to generate a solid color pattern.
           
3.Color darker()
Creates a new Color that is a darker the version of this Color.
           
4.static Color decode(String nm)
Converts a String to an integer and returns the specified opaque Color.
           
5.boolean equals(Object obj)
Determines whether another Color object is equal to this Color.      

6.int getAlpha()
Returns the alpha component in the range 0-255.
           
7.int getBlue()
Returns the blue component in the range 0-255 in the default sRGB space.

8.static Color getColor(String nm)
Finds a color in the system properties.
           
9.static Color getColor(String nm, Color v)
Finds a color in the system properties.
           
10.static Color getColor(String nm, int v)
Finds a color in the system properties.
           
11.float[] getColorComponents(ColorSpace cspace, float[] compArray)
Returns a float array containing only the color components of the Color in the ColorSpace specified by the cspace parameter.
           
12.float[] getColorComponents(float[] compArray)
Returns a float array containing only the color components of the Color, in the ColorSpace of the Color.
           
13.ColorSpace getColorSpace()
Returns the ColorSpace of this Color.
           
14.float[] getComponents(ColorSpace cspace, float[] compArray)
Returns a float array containing the color and alpha components of the Color, in the ColorSpace specified by the cspace parameter.
           
15.float[] getComponents(float[] compArray)
Returns a float array containing the color and alpha components of the Color, in the ColorSpace of the Color.
           
16.int getGreen()
Returns the green component in the range 0-255 in the default sRGB space.
           
17.static Color getHSBColor(float h, float s, float b)
Creates a Color object based on the specified values for the HSB color model.
           


18.int getRed()
Returns the red component in the range 0-255 in the default sRGB space.
           
19.int getRGB()
Returns the RGB value representing the color in the default sRGB ColorModel.
           
20.float[] getRGBColorComponents(float[] compArray)
Returns a float array containing only the color components of the Color, in the default sRGB color space.
           
21.float[] getRGBComponents(float[] compArray)
Returns a float array containing the color and alpha components of the Color, as represented in the default sRGB color space.
           
22.int getTransparency()
Returns the transparency mode for this Color.
           
23.int hashCode()
Computes the hash code for this Color.
           
24.static int HSBtoRGB(float hue, float saturation, float brightness)
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.
           
25.static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals)
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.
           
26.String toString()
Returns a string representation of this Color.

Sample program for Java Color:

import java.awt.*;
import java.applet.*;
/*
<applet code="codedemo" width=250 height=200>
</applet>
*/
public class colordemo extends applet
{
public void paint(Graphics g)
{
Color c=new Color(0,255,0);
g.setColor(c);
g.drawString("online help",25,25);
}
}