Skip to main content

Convert RGB to hex color

Here given code implementation process.

/*
  Java Program
  RGB to Hex color
*/
public class ColorConversion
{
	public char get_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	public String hexadecimal(int number)
	{
		int decimal = number;
		//This is used to store result
		String result = "";
		while (decimal > 0)
		{
			result = (get_value(decimal % 16)) + result;
			decimal /= 16;
		}
		return result;
	}
	// Converts the given RGB color to Hex color
	public void rgb_to_hex(int red, int green, int blue)
	{
		// Display Given results
		System.out.print(" Given RGB");
		System.out.print("\n R = " + red);
		System.out.print("\n G = " + green);
		System.out.print("\n B = " + blue);
		String hex = hexadecimal(red) + hexadecimal(green) + hexadecimal(blue);
		System.out.print("\n Hex = #" + hex + "\n\n");
	}
	public static void main(String[] args)
	{
		ColorConversion color = new ColorConversion();
		// Test Case
		color.rgb_to_hex(65, 32, 126);
		color.rgb_to_hex(54, 155, 229);
		color.rgb_to_hex(33, 150, 243);
	}
}

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #41207E

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369BE5

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #2196F3
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  RGB to Hex color
*/
class ColorConversion
{
	public: char get_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	string hexadecimal(int number)
	{
		int decimal = number;
		//This is used to store result
		string result = "";
		while (decimal > 0)
		{
			result = (this->get_value(decimal % 16)) + result;
			decimal /= 16;
		}
		return result;
	}
	// Converts the given RGB color to Hex color
	void rgb_to_hex(int red, int green, int blue)
	{
		// Display Given results
		cout << " Given RGB";
		cout << "\n R = " << red;
		cout << "\n G = " << green;
		cout << "\n B = " << blue;
		string hex = this->hexadecimal(red) + this->hexadecimal(green) + this->hexadecimal(blue);
		cout << "\n Hex = #" << hex << "\n\n";
	}
};
int main()
{
	ColorConversion color = ColorConversion();
	// Test Case
	color.rgb_to_hex(65, 32, 126);
	color.rgb_to_hex(54, 155, 229);
	color.rgb_to_hex(33, 150, 243);
	return 0;
}

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #41207E

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369BE5

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #2196F3
// Include namespace system
using System;
/*
  C# Program
  RGB to Hex color
*/
public class ColorConversion
{
	public char get_value(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	public String hexadecimal(int number)
	{
		int decimal_number = number;
		//This is used to store result
		String result = "";
		while (decimal_number > 0)
		{
			result = (get_value(decimal_number % 16)) + result;
			decimal_number /= 16;
		}
		return result;
	}
	// Converts the given RGB color to Hex color
	public void rgb_to_hex(int red, int green, int blue)
	{
		// Display Given results
		Console.Write(" Given RGB");
		Console.Write("\n R = " + red);
		Console.Write("\n G = " + green);
		Console.Write("\n B = " + blue);
		String hex = hexadecimal(red) + hexadecimal(green) + hexadecimal(blue);
		Console.Write("\n Hex = #" + hex + "\n\n");
	}
	public static void Main(String[] args)
	{
		ColorConversion color = new ColorConversion();
		// Test Case
		color.rgb_to_hex(65, 32, 126);
		color.rgb_to_hex(54, 155, 229);
		color.rgb_to_hex(33, 150, 243);
	}
}

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #41207E

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369BE5

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #2196F3
<?php
/*
  Php Program
  RGB to Hex color
*/
class ColorConversion
{
	public	function get_value($num)
	{
		if ($num >= 0 && $num <= 9)
		{
			return (string)($num + '0');
		}
		else
		{
			return (string)($num - 10 + 'A');
		}
	}
	public	function hexadecimal($number)
	{
		$decimal = $number;
		//This is used to store result
		$result = "";
		while ($decimal > 0)
		{
			$result = ($this->get_value($decimal % 16)) . $result;
			$decimal = intval($decimal / 16);
		}
		return $result;
	}
	// Converts the given RGB color to Hex color
	public	function rgb_to_hex($red, $green, $blue)
	{
		// Display Given results
		echo " Given RGB";
		echo "\n R = ". $red;
		echo "\n G = ". $green;
		echo "\n B = ". $blue;
		$hex = $this->hexadecimal($red) . $this->hexadecimal($green) . $this->hexadecimal($blue);
		echo "\n Hex = #". $hex ."\n\n";
	}
}

function main()
{
	$color = new ColorConversion();
	// Test Case
	$color->rgb_to_hex(65, 32, 126);
	$color->rgb_to_hex(54, 155, 229);
	$color->rgb_to_hex(33, 150, 243);
}
main();

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #412074

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369145

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #219653
/*
  Node Js Program
  RGB to Hex color
*/
class ColorConversion
{
	get_value(num)
	{
		if (num >= 0 && num <= 9)
		{
			return String.fromCharCode((num + '0'.charCodeAt(0)));
		}
		else
		{
			return String.fromCharCode((num - 10 + 'A'.charCodeAt(0)));
		}
	}
	hexadecimal(number)
	{
		var decimal = number;
		//This is used to store result
		var result = "";
		while (decimal > 0)
		{
			result = (this.get_value(decimal % 16)) + result;
			decimal = parseInt(decimal / 16);
		}
		return result;
	}
	// Converts the given RGB color to Hex color
	rgb_to_hex(red, green, blue)
	{
		// Display Given results
		process.stdout.write(" Given RGB");
		process.stdout.write("\n R = " + red);
		process.stdout.write("\n G = " + green);
		process.stdout.write("\n B = " + blue);
		var hex = this.hexadecimal(red) + this.hexadecimal(green) + this.hexadecimal(blue);
		process.stdout.write("\n Hex = #" + hex + "\n\n");
	}
}

function main()
{
	var color = new ColorConversion();
	// Test Case
	color.rgb_to_hex(65, 32, 126);
	color.rgb_to_hex(54, 155, 229);
	color.rgb_to_hex(33, 150, 243);
}
main();

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #41207E

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369BE5

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #2196F3
# Python 3 Program
# RGB to Hex color

class ColorConversion :
	def get_value(self, num) :
		if (num >= 0 and num <= 9) :
			return chr((num + ord('0')))
		else :
			return chr((num - 10 + ord('A')))
		
	
	def hexadecimal(self, number) :
		decimal = number
		# This is used to store result
		result = ""
		while (decimal > 0) :
			result = (self.get_value(decimal % 16)) + result
			decimal = int(decimal / 16)
		
		return result
	
	#  Converts the given RGB color to Hex color
	def rgb_to_hex(self, red, green, blue) :
		#  Display Given results
		print(" Given RGB", end = "")
		print("\n R = ", red, end = "")
		print("\n G = ", green, end = "")
		print("\n B = ", blue, end = "")
		hex = self.hexadecimal(red) + self.hexadecimal(green) + self.hexadecimal(blue)
		print("\n Hex = #{0}".format(hex) ,"\n")
	

def main() :
	color = ColorConversion()
	#  Test Case
	color.rgb_to_hex(65, 32, 126)
	color.rgb_to_hex(54, 155, 229)
	color.rgb_to_hex(33, 150, 243)

if __name__ == "__main__": main()

Output

 Given RGB
 R =  65
 G =  32
 B =  126
 Hex = #41207E

 Given RGB
 R =  54
 G =  155
 B =  229
 Hex = #369BE5

 Given RGB
 R =  33
 G =  150
 B =  243
 Hex = #2196F3
# Ruby Program
# RGB to Hex color

class ColorConversion 
	def get_value(num) 
		if (num >= 0 && num <= 9) 
			return ((num + '0'.ord )).chr
		else 
			return ((num - 10 + 'A'.ord )).chr
		end

	end

	def hexadecimal(number) 
		decimal = number
		# This is used to store result
		result = ""
		while (decimal > 0) 
			result = (self.get_value(decimal % 16)) + result
			decimal /= 16
		end

		return result
	end

	#  Converts the given RGB color to Hex color
	def rgb_to_hex(red, green, blue) 
		#  Display Given results
		print(" Given RGB")
		print("\n R = ", red)
		print("\n G = ", green)
		print("\n B = ", blue)
		hex = self.hexadecimal(red) + self.hexadecimal(green) + self.hexadecimal(blue)
		print("\n Hex = #", hex ,"\n\n")
	end

end

def main() 
	color = ColorConversion.new()
	#  Test Case
	color.rgb_to_hex(65, 32, 126)
	color.rgb_to_hex(54, 155, 229)
	color.rgb_to_hex(33, 150, 243)
end

main()

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #41207E

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369BE5

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #2196F3

/*
  Scala Program
  RGB to Hex color
*/
class ColorConversion
{
	def get_value(num: Int): Char = {
		if (num >= 0 && num <= 9)
		{
			return ((num + '0')).toChar;
		}
		else
		{
			return ((num - 10 + 'A')).toChar;
		}
	}
	def hexadecimal(number: Int): String = {
		var decimal: Int = number;
		//This is used to store result
		var result: String = "";
		while (decimal > 0)
		{
			result = ""+(this.get_value(decimal % 16)) + result;
			decimal = (decimal / 16).toInt;
		}
		return result;
	}
	// Converts the given RGB color to Hex color
	def rgb_to_hex(red: Int, green: Int, blue: Int): Unit = {
		// Display Given results
		print(" Given RGB");
		print("\n R = " + red);
		print("\n G = " + green);
		print("\n B = " + blue);
		var hex: String = this.hexadecimal(red) + this.hexadecimal(green) + this.hexadecimal(blue);
		print("\n Hex = #" + hex + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var color: ColorConversion = new ColorConversion();
		// Test Case
		color.rgb_to_hex(65, 32, 126);
		color.rgb_to_hex(54, 155, 229);
		color.rgb_to_hex(33, 150, 243);
	}
}

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #41207E

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369BE5

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #2196F3
/*
  Swift 4 Program
  RGB to Hex color
*/
class ColorConversion
{
	func get_value(_ num: Int)->String
	{
		if (num >= 0 && num <= 9)
		{
			return String(UnicodeScalar(UInt8((num + 48))));
		}
		else
		{
			return String(UnicodeScalar(UInt8((num - 10 + 65))));
		}
	}
	func hexadecimal(_ number: Int)->String
	{
		var decimal: Int = number;
		//This is used to store result
		var result: String = "";
		while (decimal > 0)
		{
			result = (self.get_value(decimal % 16)) + result;
			decimal /= 16;
		}
		return result;
	}
	// Converts the given RGB color to Hex color
	func rgb_to_hex(_ red: Int, _ green: Int, _ blue: Int)
	{
		// Display Given results
		print(" Given RGB", terminator: "");
		print("\n R = ", red, terminator: "");
		print("\n G = ", green, terminator: "");
		print("\n B = ", blue, terminator: "");
		let hex: String = self.hexadecimal(red) + self.hexadecimal(green) + self.hexadecimal(blue);
		print("\n Hex = #\(hex)" ,"\n");
	}
}
func main()
{
	let color: ColorConversion = ColorConversion();
	// Test Case
	color.rgb_to_hex(65, 32, 126);
	color.rgb_to_hex(54, 155, 229);
	color.rgb_to_hex(33, 150, 243);
}
main();

Output

 Given RGB
 R =  65
 G =  32
 B =  126
 Hex = #41207E

 Given RGB
 R =  54
 G =  155
 B =  229
 Hex = #369BE5

 Given RGB
 R =  33
 G =  150
 B =  243
 Hex = #2196F3
/*
  Kotlin Program
  RGB to Hex color
*/
class ColorConversion
{
	fun get_value(num: Int): Char
	{
		if (num>= 0 && num <= 9)
		{
			return (num + 48).toChar();
		}
		else
		{
			return (num - 10 + 65).toChar();
		}
	}
	fun hexadecimal(number: Int): String
	{
		var decimal: Int = number;
		//This is used to store result
		var result: String = "";
		while (decimal>0)
		{
			result = (this.get_value(decimal % 16)) + result;
			decimal /= 16;
		}
		return result;
	}
	// Converts the given RGB color to Hex color
	fun rgb_to_hex(red: Int, green: Int, blue: Int): Unit
	{
		// Display Given results
		print(" Given RGB");
		print("\n R = " + red);
		print("\n G = " + green);
		print("\n B = " + blue);
		var hex: String = this.hexadecimal(red) + this.hexadecimal(green) + this.hexadecimal(blue);
		print("\n Hex = #" + hex + "\n\n");
	}
}
fun main(args: Array<String>): Unit
{
	var color: ColorConversion = ColorConversion();
	// Test Case
	color.rgb_to_hex(65, 32, 126);
	color.rgb_to_hex(54, 155, 229);
	color.rgb_to_hex(33, 150, 243);
}

Output

 Given RGB
 R = 65
 G = 32
 B = 126
 Hex = #41207E

 Given RGB
 R = 54
 G = 155
 B = 229
 Hex = #369BE5

 Given RGB
 R = 33
 G = 150
 B = 243
 Hex = #2196F3




Comment

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