Convert Color Code into HSV to RGB and Hex Color Code in Java
Posted By : Rahul Singh | 05-Dec-2017
Hi guys, In this blog I discussed color code conversion which is the basic need for programmers to convert color code one to another. So create a program in java which is basically to conversion of colors codes.
Commonly we used hex code for color which more convenient to understand for any one but for more convenient way for us to specify colors in software, we used HSV and HSB color code. There is RGB color code is also a good option for machine understandablity but not for human friendly and RGB range between 0 to 255.
HSV/HSB:
In HSV, There H stand by Hue and S stand by Saturation. The range of Hue is number between 0 to 360 that represent in the color wheel. There is two color modal HSB and HSV which are same, there is only difference is B in HSB stand by Brightness and V in HSV stand by Value, only the perception of the amount power or light of the source. Both Saturation and Value/Brightness range is in number 0 to 1 or as percentage(%).
HSL:
In HSL, Hue and Saturation is exactly same as HSB/HSV but L stand by Lightness, is not same as Value/Brightness. Brightness perceived "amount of light" which can be any color code but Lightness is best option to understand the light of white.
All these HSB, HSV, HSL are just representation of RGB color model and its usefull to provide parameter in RGB coordinates. Here we can covert color code from one to another like: HSV to RGB and RGB to Hex Code by using JAVA Language.
Example of Color Code:
Hex: #FFFFFF
RGB: rgb(128,255,64)
HSV: hsv(120°,50%,100%)
HSB: hsb(120°,100%,100%)
HSL: hsl(120°,100%,60%)
Convert HSV/HSB color code to RGB:
package test;
import java.awt.Color;
import java.util.ArrayList;
public class TestColor
{
public static void main( String[] args )
{
//here we can get color from function
String colorCode = "hsv(50, 60%, 60%)";
//split and get color code numbers
String aa[] = colorCode.split( "[hsv(,%,)]" );
ArrayList< Integer > ab = new ArrayList< Integer >();
for ( String a : aa )
{
String ss = a.trim();
if ( ss != null && !ss.isEmpty() && ss != "" )
{
ab.add( Integer.parseInt( ss ) );
}
}
//get Code in numbers and pass their respective HSV/HSB values
//we can also pass the direct values into these variables
float hue = ab.get( 0 );
float saturation = ab.get( 1 );
float brightness = ab.get( 2 );
float h = Math.max( 0, Math.min( 360, hue ) );
float s = Math.max( 0, Math.min( 100, saturation ) );
float v = Math.max( 0, Math.min( 100, brightness ) );
h /= 360;
s /= 100;
v /= 100;
//get color and print RGB color code on console
Color cc = Color.getHSBColor( h, s, v );
System.out.println( "HSV Code: "+cc.getRed() + ", " + cc.getGreen() + ", " + cc.getBlue() );
//covert color code into RGB to HSV and print on console
float[] hsvArray = Color.RGBtoHSB(cc.getRed(), cc.getGreen(), cc.getBlue(), null);
float hue1 = hsvArray[0];
float saturation1 = hsvArray[1];
float brightness1 = hsvArray[2];
System.out.println("RGB Code: "+ hue1 + ", " + saturation1 + ", " + brightness1);
//also convert RGB color into Hex color Code and print on console
String hex = String.format( "#%02x%02x%02x", cc.getRed(), cc.getGreen(), cc.getBlue() );
System.out.println( hex );
}
}
-- Thanks --
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Singh
Rahul singh is a Java Developer and having experience in developing Applications. He is a quick learner.