Pages

Java simple rgb to hex converter

Complete source code below is a small java program that convert rgb value to hex value. Now i will share with you how to get hex value from rgb value. Before i go far, let we see a description about hex value structure.


VALUE THAT YOU GET AFTER DIVIDE OR MULTIPLY

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
HEX VALUE0123456789ABCDEF

TABLE A

For example :
FFFFFF is a hex value for white and it's rgb value is 255 255 255.First FF is value for red, second FF is value for green and last FF is for blue.So it mean, first 255 in rgb is equal to first FF in hex value.Now i will show you, how to get FF from 255.For this tutorial i show you only for red part. You can try other last two part for green and blue. Now take 255 and devide it with 16. You should get 15.9375. Take value before floating point. In this case we get 15. Look at TABLE A at above. 15 is equal to F. Now you get first F.For second F, take value after floating point and multiply it with 16. In this case you should multiply 0.9375 with 16. It's result is equal to 15 and it also hold F.Now you get hex value for red part.I hope you can understand what i tried to say.

********************************************************************
COMPLETE SOURCE CODE FOR : RGBtoHEX.java
********************************************************************


import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridLayout;
import java.awt.Color;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RGBtoHEX implements ActionListener
{
JTextField r=new JTextField(10);
JTextField g=new JTextField(10);
JTextField b=new JTextField(10);

JLabel rLabel=new JLabel("R",JLabel.CENTER);
JLabel gLabel=new JLabel("G",JLabel.CENTER);
JLabel bLabel=new JLabel("B",JLabel.CENTER);

JPanel panelTextField=new JPanel();
JPanel panelLabel=new JPanel();

JButton button=new JButton("Calculate Now !");

JTextField result=new JTextField(30);

public RGBtoHEX()
{
button.addActionListener(this);

r.setHorizontalAlignment(JTextField.CENTER);
g.setHorizontalAlignment(JTextField.CENTER);
b.setHorizontalAlignment(JTextField.CENTER);

result.setHorizontalAlignment(JTextField.CENTER);
result.setBackground(new Color(255,255,255));
result.setEditable(false);

panelLabel.setLayout(new GridLayout(1,3));
panelLabel.add(rLabel);
panelLabel.add(gLabel);
panelLabel.add(bLabel);

panelTextField.setLayout(new GridLayout(1,3));
panelTextField.add(r);
panelTextField.add(g);
panelTextField.add(b);

JFrame myMainWindow=new JFrame("RGB to HEX");
myMainWindow.getContentPane().setLayout(new GridLayout(4,1));
myMainWindow.getContentPane().add(panelLabel);
myMainWindow.getContentPane().add(panelTextField);
myMainWindow.getContentPane().add(button);
myMainWindow.getContentPane().add(result);

myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setSize(300,130);
myMainWindow.setLocationRelativeTo(null);
myMainWindow.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
try
{
String hexValue="#";
r.setEditable(false);
g.setEditable(false);
b.setEditable(false);

double red=Double.parseDouble(r.getText());
double green=Double.parseDouble(g.getText());
double blue=Double.parseDouble(b.getText());

for(int i=1;i<=3;i++)
{
double temp=0;

if(i==1)
{
temp=red;
}
else if(i==2)
{
temp=green;
}
else if(i==3)
{
temp=blue;
}

double devideResult=temp/16;

String stringDevideResult=Double.toString(devideResult);

int pointIndexInString=stringDevideResult.indexOf(".");

String firstValue=stringDevideResult.substring(0,pointIndexInString);

double multiplySixteen=(devideResult-(Double.parseDouble(firstValue)))*16;

String stringMultiplySixteen=Double.toString(multiplySixteen);

pointIndexInString=stringMultiplySixteen.indexOf(".");

String secondValue=stringMultiplySixteen.substring(0,pointIndexInString);

if(firstValue.equalsIgnoreCase("10"))
{
firstValue="A";
}
if(firstValue.equalsIgnoreCase("11"))
{
firstValue="B";
}
if(firstValue.equalsIgnoreCase("12"))
{
firstValue="C";
}
if(firstValue.equalsIgnoreCase("13"))
{
firstValue="D";
}
if(firstValue.equalsIgnoreCase("14"))
{
firstValue="E";
}
if(firstValue.equalsIgnoreCase("15"))
{
firstValue="F";
}
if(secondValue.equalsIgnoreCase("10"))
{
secondValue="A";
}
if(secondValue.equalsIgnoreCase("11"))
{
secondValue="B";
}
if(secondValue.equalsIgnoreCase("12"))
{
secondValue="C";
}
if(secondValue.equalsIgnoreCase("13"))
{
secondValue="D";
}
if(secondValue.equalsIgnoreCase("14"))
{
secondValue="E";
}
if(secondValue.equalsIgnoreCase("15"))
{
secondValue="F";
}

hexValue=hexValue+firstValue+secondValue;
}


result.setText(hexValue);
r.setEditable(true);
g.setEditable(true);
b.setEditable(true);
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(null,"Program error and it will terminate","ERROR",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}

public static void main(String[]args)
{
RGBtoHEX converter=new RGBtoHEX();
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************
Related Posts Plugin for WordPress, Blogger...