Hex to RGB color

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


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







© 2021, kalkicode.com, All rights reserved