Skip to main content

Convert RGB to hex color

Converting RGB values to hexadecimal (hex) color codes is a common task in web development, graphic design, and digital media. Hex color codes are widely used to represent colors in HTML, CSS, and other digital platforms. This conversion involves translating the Red, Green, and Blue color components of an RGB color into a single hex code.

Problem Statement

Given the RGB values (Red, Green, and Blue) of a color, the task is to convert them into their corresponding hexadecimal color code. This conversion allows us to represent the color in a concise format that's easily interpretable by computers and web browsers.

Example

Let's consider the first test case from your code:

  • R = 65
  • G = 32
  • B = 126

We want to convert these RGB values to a hex color code.

Idea to Solve the Problem

The conversion from RGB to a hex color code involves dividing each RGB component by 16 and then mapping the resulting quotient and remainder to their corresponding hexadecimal characters.

Pseudocode

get_value(num):
    if num >= 0 and num <= 9:
        return (char)(num + '0')
    else:
        return (char)(num - 10 + 'A')

hexadecimal(number):
    decimal = number
    result = ""
    while decimal > 0:
        result = get_value(decimal % 16) + result
        decimal /= 16
    return result

rgb_to_hex(red, green, blue):
    hex = hexadecimal(red) + hexadecimal(green) + hexadecimal(blue)
    return "#" + hex

Algorithm Explanation

  1. The get_value function maps a numerical value to its corresponding hexadecimal character. Digits from 0 to 9 are mapped to '0' to '9', while values from 10 to 15 are mapped to 'A' to 'F'.
  2. The hexadecimal function converts a decimal number to its hexadecimal representation. It repeatedly divides the decimal number by 16 and appends the remainder as a hexadecimal digit to the result.
  3. The rgb_to_hex function calls the hexadecimal function for each RGB component and concatenates their hexadecimal representations to form the final hex color code. It then adds a '#' at the beginning of the code.

Code Solution

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

Resultant Output Explanation

For the first test case (65, 32, 126):

  • Hexadecimal representation of 65: 41
  • Hexadecimal representation of 32: 20
  • Hexadecimal representation of 126: 7E
  • The concatenated hex code: #41207E

For the second test case (54, 155, 229):

  • Hexadecimal representation of 54: 36
  • Hexadecimal representation of 155: 9B
  • Hexadecimal representation of 229: E5
  • The concatenated hex code: #369BE5

For the third test case (33, 150, 243):

  • Hexadecimal representation of 33: 21
  • Hexadecimal representation of 150: 96
  • Hexadecimal representation of 243: F3
  • The concatenated hex code: #2196F3

Time Complexity

The time complexity of this algorithm is O(logN), where N is the largest RGB component value. This is because the hexadecimal function iterates through the decimal value, dividing it by 16 in each iteration.

Finally

This article explained the process of converting RGB color values to hexadecimal color codes. It introduced the concept, provided suitable examples, presented the algorithmic approach, and discussed the time complexity. The goal is to simplify the understanding of the RGB to hex color conversion process and provide readers with a clear explanation of how the conversion is performed.





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