Working with Colours in Java

by | Dec 20, 2020 | Java

Home » Java » Working with Colours in Java

Working with Colours in Java

To assist users in working with colours, JAVA has a dedicated class called the Color class (note the spelling of ‘Color’). As a reference, the American spelling of the word is used for all cases that deal with colour in JAVA. The RGBA values (Red, Green, Blue, Alpha) and HSB values (Hue, Saturation, BRIcomponents) are mainly used to create the colour effect on the output terminal and in Java applications. The range for RGBA values normally starts from 0 and goes upto 255. On a different scale, it is also measured between 0.0 and 0.1. The ‘Alpha’ component of RGBA controls the opacity of the colour. 0 signifies fully transparent and 255 signifies completely opaque. Values in-between can be fed as well.

The recognizable colors in JAVA are as follows: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, and yellow. If you want to use any other colour, you will need to mix and match the RBG values to produce it.

Color Class Constructors

  • Color (ColorSpace c, float [] co, float a): This creates a color in the given ColorSpace using the elements given in the float array and the alpha.
  • Color(float r, float g, float b): This creates an opaque sRBG color using the values of r, g, and b fed to it (Range: 0.0 – 1.0).
  • Color(float r, float g, float b, float a): This creates a sRBG color using the values of r, g, alpha, and b fed to it (Range: 0.0 – 1.0).
  • Color(int rbg): This takes in the entire rbg value into one variable and breaks it up in the following format – Red component in bits 16-23, green in bits 8-15, and blue in bits 0-7.
  • Color(int rbga, boolean hasalpha): This makes use of the format mentioned earlier (Red component in bits 16-23, green in bits 8-15, and blue in bits 0-7) along with the alpha component in buts 24-31.
  • Color(int r, int g, int b): Takes in r, b, and g values on a scale of 0 to 255 and gives you the desired colour.
  • Color(int r, int g, int b, int a): Takes in r, b, g, and alpha values on a scale of 0 to 255 and gives you the desired colour.

Some example where the Color class is put to use

Example 1: This is a simple code to color the background of the panel
import java.awt.*;
import javax.swing.*;
class colour extends JFrame
{
colour()
{
super("colour");
Color c = Color.yellow;
JPanel p = new JPanel();
p.setBackground(c);
setSize(200, 200);
add(p);
show();
}
 // Main Method
public static void main(String args[])
{
colour c = new colour();
}
}


Example 2: Using RBG values to colour the background panel
import java.awt.*;
import javax.swing.*;
class colour extends JFrame
{
// constructor
colour()
{
super("colour");
// create a new Color
// RGB value of Yellow is 225, 255, 0
Color c = new Color(255, 255, 0);
JPanel p = new JPanel();
p.setBackground(c);
setSize(200, 200);
add(p);
show();
}
// Main Method
public static void main(String args[])
{
colour c = new colour();
}
}


Example 3: We will take into consideration the alpha value along with the RBG values
import java.awt.*;
import javax.swing.*;
class colour extends JFrame
{

colour()
{
super("colour");
// create a new Color
// RGB value of red is 225, 0, 0
// and keep the alpha value as 150 such that the colour is actually a light shade of red
Color c = new Color(255, 0, 0, 150);
JPanel p = new JPanel();
p.setBackground(c);
setSize(200, 200);
add(p);
show();
}
public static void main(String args[])
{
colour c = new colour();
}
}

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author