Hex to RGB color
In computer programming and web development, colors are often represented using hexadecimal (hex) codes. However, sometimes it's necessary to convert these hex color codes to their corresponding RGB (Red, Green, Blue) values for various purposes. In this article, we will discuss the problem of converting hex color codes to RGB values and provide a solution in the form of an algorithm and pseudocode.
Problem Statement
The problem is to write a program that takes a hex color code as input and converts it to its RGB representation. The hex color code is a six-digit alphanumeric value that represents the intensity of the three primary colors (red, green, and blue) used to create a specific color. Each digit in the hex code represents four bits, allowing for 16 possible values (0-9, A-F).
For example, the hex code "FA8072" represents a reddish color, "03FFA2" represents a bluish-green color, and "FFFFFF" represents white.
Example:
Input: "FA8072"
Output: R: 250, G: 128, B: 114
Algorithm and Pseudocode
To convert a hex color code to RGB, we can follow these steps:
- Define a function
color_code
that takes a character as input and returns the decimal value of the corresponding hex digit. - Define a function
hex_to_rgb
that takes a hex color code as input. - Inside the
hex_to_rgb
function, calculate the RGB values using thecolor_code
function and the given hex color code. - Display the calculated RGB values.
- Define the
main
function and test thehex_to_rgb
function with sample hex color codes.
Here is the pseudocode for the algorithm:
function color_code(color):
if color is a number:
return (decimal value of the digit)
else if color is a lowercase letter:
return (decimal value of the letter)
else if color is an uppercase letter:
return (decimal value of the letter)
else:
return -1
function hex_to_rgb(hex):
if length of hex is not equal to 6:
print "Invalid Hex color code"
return
else:
r = (color_code(hex[0]) * 16) + color_code(hex[1])
g = (color_code(hex[2]) * 16) + color_code(hex[3])
b = (color_code(hex[4]) * 16) + color_code(hex[5])
print "Hex:", hex
print "R:", r, "G:", g, "B:", b
function main():
hex_to_rgb("FA8072")
hex_to_rgb("03FFA2")
hex_to_rgb("FFFFFF")
Code Solution
Here given code implementation process.
// C program
// Hex to RGB color
#include <stdio.h>
#include <string.h>
// Returns the decimal code of given hex
int color_code(char color)
{
if (color >= '0' && color <= '9')
{
//When hex digit, is a number
return (color - 48);
}
else if (color >= 'a' && color <= 'f')
{
//when of hex digit is an lowercase letter
return (color - 87);
}
else if (color >= 'A' && color <= 'F')
{
//when of hex digit is an uppercase letter
return (color - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
void hex_to_rgb(char *hex)
{
// Get the length
int digits = strlen(hex);
if (digits != 6)
{
printf("\n Invalid Hex color code [%s]\n", hex);
return;
}
// Calculate color code
int r = (color_code(hex[0]) *16) + (color_code(hex[1]));
int g = (color_code(hex[2]) *16) + (color_code(hex[3]));
int b = (color_code(hex[4]) *16) + (color_code(hex[5]));
// Display calculated result
printf(" Hex : %s \n R : %d G : %d B : %d \n", hex, r, g, b);
}
int main()
{
// Test Case
hex_to_rgb("fa8072");
hex_to_rgb("03ffa2");
hex_to_rgb("ffffff");
return 0;
}
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
/*
Java Program
RGB to HSL color
*/
public class ColorConversion
{
// Returns the decimal code of given hex
public int color_code(char color)
{
if (color >= '0' && color <= '9')
{
//When hex digit, is a number
return (color - 48);
}
else if (color >= 'a' && color <= 'f')
{
//when of hex digit is an lowercase letter
return (color - 87);
}
else if (color >= 'A' && color <= 'F')
{
//when of hex digit is an uppercase letter
return (color - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
public void hex_to_rgb(String hex)
{
// Get the length
int digits = hex.length();
if (digits != 6)
{
System.out.print("\n Invalid Hex color code [" + hex + "]\n");
return;
}
// Calculate color code
int r = (color_code(hex.charAt(0)) * 16) + (color_code(hex.charAt(1)));
int g = (color_code(hex.charAt(2)) * 16) + (color_code(hex.charAt(3)));
int b = (color_code(hex.charAt(4)) * 16) + (color_code(hex.charAt(5)));
// Display calculated result
System.out.print(" Hex : " + hex + " \n R : " + r + " G : " + g + " B : " + b + " \n");
}
public static void main(String[] args)
{
ColorConversion color = new ColorConversion();
// Test Case
color.hex_to_rgb("fa8072");
color.hex_to_rgb("03ffa2");
color.hex_to_rgb("ffffff");
}
}
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
// Include header file
#include <iostream>
#include<string.h>
using namespace std;
/*
C++ Program
RGB to HSL color
*/
class ColorConversion
{
public:
// Returns the decimal code of given hex
int color_code(char color)
{
if (color >= '0' && color <= '9')
{
//When hex digit, is a number
return (color - 48);
}
else if (color >= 'a' && color <= 'f')
{
//when of hex digit is an lowercase letter
return (color - 87);
}
else if (color >= 'A' && color <= 'F')
{
//when of hex digit is an uppercase letter
return (color - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
void hex_to_rgb(string hex)
{
// Get the length
int digits = hex.size();
if (digits != 6)
{
cout << "\n Invalid Hex color code [" << hex << "]\n";
return;
}
// Calculate color code
int r = (this->color_code(hex[0]) *16) + (this->color_code(hex[1]));
int g = (this->color_code(hex[2]) *16) + (this->color_code(hex[3]));
int b = (this->color_code(hex[4]) *16) + (this->color_code(hex[5]));
// Display calculated result
cout << " Hex : " << hex << " \n R : " << r << " G : " << g << " B : " << b << " \n";
}
};
int main()
{
ColorConversion color = ColorConversion();
// Test Case
color.hex_to_rgb("fa8072");
color.hex_to_rgb("03ffa2");
color.hex_to_rgb("ffffff");
return 0;
}
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
// Include namespace system
using System;
/*
C# Program
RGB to HSL color
*/
public class ColorConversion
{
// Returns the decimal code of given hex
public int color_code(char color)
{
if (color >= '0' && color <= '9')
{
//When hex digit, is a number
return (color - 48);
}
else if (color >= 'a' && color <= 'f')
{
//when of hex digit is an lowercase letter
return (color - 87);
}
else if (color >= 'A' && color <= 'F')
{
//when of hex digit is an uppercase letter
return (color - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
public void hex_to_rgb(String hex)
{
// Get the length
int digits = hex.Length;
if (digits != 6)
{
Console.Write("\n Invalid Hex color code [" + hex + "]\n");
return;
}
// Calculate color code
int r = (color_code(hex[0]) * 16) + (color_code(hex[1]));
int g = (color_code(hex[2]) * 16) + (color_code(hex[3]));
int b = (color_code(hex[4]) * 16) + (color_code(hex[5]));
// Display calculated result
Console.Write(" Hex : " + hex + " \n R : " + r + " G : " + g + " B : " + b + " \n");
}
public static void Main(String[] args)
{
ColorConversion color = new ColorConversion();
// Test Case
color.hex_to_rgb("fa8072");
color.hex_to_rgb("03ffa2");
color.hex_to_rgb("ffffff");
}
}
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
<?php
/*
Php Program
RGB to HSL color
*/
class ColorConversion
{
// Returns the decimal code of given hex
public function color_code($color)
{
if (ord($color) >= ord('0') && ord($color) <= ord('9'))
{
//When hex digit, is a number
return (ord($color) - 48);
}
else if (ord($color) >= ord('a') && ord($color) <= ord('f'))
{
//when of hex digit is an lowercase letter
return (ord($color) - 87);
}
else if (ord($color) >= ord('A') && ord($color) <= ord('F'))
{
//when of hex digit is an uppercase letter
return (ord($color) - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
public function hex_to_rgb($hex)
{
// Get the length
$digits = strlen($hex);
if ($digits != 6)
{
echo "\n Invalid Hex color code [". $hex ."]\n";
return;
}
// Calculate color code
$r = ($this->color_code($hex[0]) * 16) + ($this->color_code($hex[1]));
$g = ($this->color_code($hex[2]) * 16) + ($this->color_code($hex[3]));
$b = ($this->color_code($hex[4]) * 16) + ($this->color_code($hex[5]));
// Display calculated result
echo " Hex : ". $hex ." \n R : ". $r ." G : ". $g ." B : ". $b ." \n";
}
}
function main()
{
$color = new ColorConversion();
// Test Case
$color->hex_to_rgb("fa8072");
$color->hex_to_rgb("03ffa2");
$color->hex_to_rgb("ffffff");
}
main();
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
/*
Node Js Program
RGB to HSL color
*/
class ColorConversion
{
// Returns the decimal code of given hex
color_code(color)
{
if ((color).charCodeAt(0) >= ('0').charCodeAt(0) && (color).charCodeAt(0) <= ('9').charCodeAt(0))
{
//When hex digit, is a number
return ((color).charCodeAt(0) - 48);
}
else if ((color).charCodeAt(0) >= ('a').charCodeAt(0) && (color).charCodeAt(0) <= ('f').charCodeAt(0))
{
//when of hex digit is an lowercase letter
return ((color).charCodeAt(0) - 87);
}
else if ((color).charCodeAt(0) >= ('A').charCodeAt(0) && (color).charCodeAt(0) <= ('F').charCodeAt(0))
{
//when of hex digit is an uppercase letter
return ((color).charCodeAt(0) - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
hex_to_rgb(hex)
{
// Get the length
var digits = hex.length;
if (digits != 6)
{
process.stdout.write("\n Invalid Hex color code [" + hex + "]\n");
return;
}
// Calculate color code
var r = (this.color_code(hex[0]) * 16) + (this.color_code(hex[1]));
var g = (this.color_code(hex[2]) * 16) + (this.color_code(hex[3]));
var b = (this.color_code(hex[4]) * 16) + (this.color_code(hex[5]));
// Display calculated result
process.stdout.write(" Hex : " + hex + " \n R : " + r + " G : " + g + " B : " + b + " \n");
}
}
function main()
{
var color = new ColorConversion();
// Test Case
color.hex_to_rgb("fa8072");
color.hex_to_rgb("03ffa2");
color.hex_to_rgb("ffffff");
}
main();
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
# Python 3 Program
# RGB to HSL color
class ColorConversion :
# Returns the decimal code of given hex
def color_code(self, color) :
if (ord(color) >= ord('0') and ord(color) <= ord('9')) :
# When hex digit, is a number
return (ord(color) - 48)
elif(ord(color) >= ord('a') and ord(color) <= ord('f')) :
# when of hex digit is an lowercase letter
return (ord(color) - 87)
elif(ord(color) >= ord('A') and ord(color) <= ord('F')) :
# when of hex digit is an uppercase letter
return (ord(color) - 55)
# When not a valid code
return -1
# Converting hex color to RGB color
def hex_to_rgb(self, hex) :
# Get the length
digits = len(hex)
if (digits != 6) :
print("\n Invalid Hex color code [", hex ,"]")
return
# Calculate color code
r = (self.color_code(hex[0]) * 16) + (self.color_code(hex[1]))
g = (self.color_code(hex[2]) * 16) + (self.color_code(hex[3]))
b = (self.color_code(hex[4]) * 16) + (self.color_code(hex[5]))
# Display calculated result
print(" Hex : ", hex ," \n R : ", r ," G : ", g ," B : ", b ," ")
def main() :
color = ColorConversion()
# Test Case
color.hex_to_rgb("fa8072")
color.hex_to_rgb("03ffa2")
color.hex_to_rgb("ffffff")
if __name__ == "__main__": main()
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
# Ruby Program
# RGB to HSL color
class ColorConversion
# Returns the decimal code of given hex
def color_code(color)
if ((color).ord >= ('0').ord && (color).ord <= ('9').ord)
# When hex digit, is a number
return ((color).ord - 48)
elsif((color).ord >= ('a').ord && (color).ord <= ('f').ord)
# when of hex digit is an lowercase letter
return ((color).ord - 87)
elsif((color).ord >= ('A').ord && (color).ord <= ('F').ord)
# when of hex digit is an uppercase letter
return ((color).ord - 55)
end
# When not a valid code
return -1
end
# Converting hex color to RGB color
def hex_to_rgb(hex)
# Get the length
digits = hex.length()
if (digits != 6)
print("\n Invalid Hex color code [", hex ,"]\n")
return
end
# Calculate color code
r = (self.color_code(hex[0]) * 16) + (self.color_code(hex[1]))
g = (self.color_code(hex[2]) * 16) + (self.color_code(hex[3]))
b = (self.color_code(hex[4]) * 16) + (self.color_code(hex[5]))
# Display calculated result
print(" Hex : ", hex ," \n R : ", r ," G : ", g ," B : ", b ," \n")
end
end
def main()
color = ColorConversion.new()
# Test Case
color.hex_to_rgb("fa8072")
color.hex_to_rgb("03ffa2")
color.hex_to_rgb("ffffff")
end
main()
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
/*
Scala Program
RGB to HSL color
*/
class ColorConversion
{
// Returns the decimal code of given hex
def color_code(color: Char): Int = {
if (color >= '0' && color <= '9')
{
//When hex digit, is a number
return (color - 48);
}
else if (color >= 'a' && color <= 'f')
{
//when of hex digit is an lowercase letter
return (color - 87);
}
else if (color >= 'A' && color <= 'F')
{
//when of hex digit is an uppercase letter
return (color - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
def hex_to_rgb(hex: String): Unit = {
// Get the length
var digits: Int = hex.length();
if (digits != 6)
{
print("\n Invalid Hex color code [" + hex + "]\n");
return;
}
// Calculate color code
var r: Int = (this.color_code(hex(0)) * 16) + (this.color_code(hex(1)));
var g: Int = (this.color_code(hex(2)) * 16) + (this.color_code(hex(3)));
var b: Int = (this.color_code(hex(4)) * 16) + (this.color_code(hex(5)));
// Display calculated result
print(" Hex : " + hex + " \n R : " + r + " G : " + g + " B : " + b + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var color: ColorConversion = new ColorConversion();
// Test Case
color.hex_to_rgb("fa8072");
color.hex_to_rgb("03ffa2");
color.hex_to_rgb("ffffff");
}
}
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
/*
Swift 4 Program
RGB to HSL color
*/
class ColorConversion
{
// Returns the decimal code of given hex
func color_code(_ color: Character)->Int
{
if (color >= "0" && color <= "9")
{
//When hex digit, is a number
return Int(UnicodeScalar(String(color))!.value) - 48;
}
else if (color >= "a" && color <= "f")
{
//when of hex digit is an lowercase letter
return Int(UnicodeScalar(String(color))!.value) - 87;
}
else if (color >= "A" && color <= "F")
{
//when of hex digit is an uppercase letter
return Int(UnicodeScalar(String(color))!.value) - 55;
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
func hex_to_rgb(_ hex_code: String)
{
// Get the length
let digits: Int = hex_code.count;
if (digits != 6)
{
print("\n Invalid Hex color code [", hex_code ,"]");
return;
}
let hex = Array(hex_code);
// Calculate color code
let r: Int = (self.color_code(hex[0]) * 16) + (self.color_code(hex[1]));
let g: Int = (self.color_code(hex[2]) * 16) + (self.color_code(hex[3]));
let b: Int = (self.color_code(hex[4]) * 16) + (self.color_code(hex[5]));
// Display calculated result
print(" Hex : ", hex_code ," \n R : ", r ," G : ", g ," B : ", b ," ");
}
}
func main()
{
let color: ColorConversion = ColorConversion();
// Test Case
color.hex_to_rgb("fa8072");
color.hex_to_rgb("03ffa2");
color.hex_to_rgb("ffffff");
}
main();
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
/*
Kotlin Program
RGB to HSL color
*/
class ColorConversion
{
// Returns the decimal code of given hex
fun color_code(color: Char): Int
{
if (color >= '0' && color <= '9')
{
//When hex digit, is a number
return (color.toInt() - 48);
}
else
if (color >= 'a' && color <= 'f')
{
//when of hex digit is an lowercase letter
return (color.toInt() - 87);
}
else
if (color >= 'A' && color <= 'F')
{
//when of hex digit is an uppercase letter
return (color.toInt() - 55);
}
// When not a valid code
return -1;
}
// Converting hex color to RGB color
fun hex_to_rgb(hex: String): Unit
{
// Get the length
var digits: Int = hex.count();
if (digits != 6)
{
print("\n Invalid Hex color code [" + hex + "]\n");
return;
}
// Calculate color code
var r: Int = (this.color_code(hex[0]) * 16) + (this.color_code(hex[1]));
var g: Int = (this.color_code(hex[2]) * 16) + (this.color_code(hex[3]));
var b: Int = (this.color_code(hex[4]) * 16) + (this.color_code(hex[5]));
// Display calculated result
print(" Hex : " + hex + " \n R : " + r + " G : " + g + " B : " + b + " \n");
}
}
fun main(args: Array<String>): Unit
{
var color: ColorConversion = ColorConversion();
// Test Case
color.hex_to_rgb("fa8072");
color.hex_to_rgb("03ffa2");
color.hex_to_rgb("ffffff");
}
Output
Hex : fa8072
R : 250 G : 128 B : 114
Hex : 03ffa2
R : 3 G : 255 B : 162
Hex : ffffff
R : 255 G : 255 B : 255
Output Explanation
The given program converts the hex color codes to their respective RGB values. The provided test cases demonstrate this conversion:
- Hex code "FA8072" corresponds to the RGB values R: 250, G: 128, B: 114.
- Hex code "03FFA2" corresponds to the RGB values R: 3, G: 255, B: 162.
- Hex code "FFFFFF" corresponds to the RGB values R: 255, G: 255, B: 255, representing the color white.
Time Complexity Analysis
The time complexity of the provided code is primarily determined by the length of the hex color code, which is constant (6 characters). Therefore, the time complexity of the hex_to_rgb
function is O(1).
Finally
In this article, we discussed the problem of converting hex color codes to RGB values. We provided an algorithm and pseudocode to solve the problem, along with an explanation of the program's output. The provided code demonstrates a simple and efficient way to convert hex color codes to their corresponding RGB values, allowing developers to work with colors more flexibly in their programs or web applications.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment