Posted on by Kalkicode
Code Conversion

HSV to RGB color

HSV (Hue, Saturation, Value) is a color model widely used in computer graphics and image processing. It represents colors based on three parameters: hue, which determines the type of color (e.g., red, green, blue); saturation, which measures the intensity or purity of the color; and value, which represents the brightness of the color. On the other hand, RGB (Red, Green, Blue) is a color model that defines colors by mixing different intensities of red, green, and blue light.

Problem Statement

The problem at hand is to convert a given HSV color to its equivalent RGB representation. This conversion is necessary when working with different color models or when displaying HSV-based colors on devices that use RGB, such as computer screens or printers. The goal is to develop an algorithm that takes the hue, saturation, and value as inputs and produces the corresponding RGB values as output.

For example, let's consider the HSV color with hue = 14°, saturation = 86.7%, and value = 100%. We want to find the corresponding RGB values for this color. Similarly, we'll explore two more test cases: hue = 0°, saturation = 0%, and value = 100%, and hue = 36°, saturation = 96%, and value = 79.2%.

Algorithm and Pseudocode

To convert HSV to RGB, we follow these steps:

  1. Normalize the saturation and value values to be in the range [0, 1] by dividing them by 100.
  2. Calculate the chroma value by multiplying the normalized saturation and value.
  3. Normalize the hue value to be in the range [0, 360] by dividing it by 60.
  4. Determine the intermediate values for red, green, and blue based on the hue:
    • If the hue is between 0 and 1, green is maximum, and red varies.
    • If the hue is between 1 and 2, red is maximum, and green varies.
    • If the hue is between 2 and 3, green is maximum, and blue varies.
    • If the hue is between 3 and 4, blue is maximum, and green varies.
    • If the hue is between 4 and 5, red is maximum, and blue varies.
    • If the hue is between 5 and 6, blue is maximum, and red varies.
  5. Calculate the match value by subtracting chroma from the normalized value.
  6. Calculate the final red, green, and blue values by adding the match value to each intermediate value.
  7. Multiply each RGB value by 255 and round to the nearest integer.

Here is the pseudocode representation of the algorithm:

function hsv_to_rgb(h, s, v):
    if h < 0 or s < 0 or v < 0:
        return

    s = s / 100.0
    v = v / 100.0

    chroma = v * s
    hh = h / 60.0
    m = v - chroma
    x = chroma * (1.0 - abs(hh % 2 - 1.0))

    if 0.0 ≤ hh ≤ 1.0:
        green = x
        red = chroma
    else if 1.0 ≤ hh ≤ 2.0:
        red = x
        green = chroma
    else if 2.0 ≤ hh < 3.0:
        blue = x
        green = chroma
    else if 3.0 ≤ hh < 4.0:
        green = x
        blue = chroma
    else if 4.0 ≤ hh < 5.0:
        red = x
        blue = chroma
    else:
        blue = x
        red = chroma

    red = (red + m) * 255
    green = (green + m) * 255
    blue = (blue + m) * 255

    r = round_value(red)
    g = round_value(green)
    b = round_value(blue)

    return (r, g, b)

Code Solution

Here given code implementation process.

// C program 
// HSV to RGB color
#include <stdio.h>
#include <math.h>

// Returns absolute value
double abs_value(double value)
{
	if (value < 0.0)
	{
		return -value;
	}
	else
	{
		return value;
	}
}
int round_value(double num)
{
	if (num < 0)
	{
		return num - 0.5;
	}
	else
	{
		return num + 0.5;
	}
}
// Convert HSV to RGB
void hsv_to_rgb(double h, double s, double v)
{
	if (h < 0 || s < 0 || v < 0)
	{
		return;
	}
	// Display Given data
	printf(" Given HSV ");
	// hue
	printf("\n H = %lf °", h);
	// saturation
	printf("\n S = %lf %% ", s);
	// value
	printf("\n V = %lf %% ", v);
	// Define some variables
	double red = 0;
	double green = 0;
	double blue = 0;
	if (s != 0)
	{
		s /= 100.0;
	}
	if (v != 0)
	{
		v /= 100.0;
	}
	// First, we find chroma
	double chroma = v *s;
	double hh = h / 60.0;
	// find match value
	double m = v - chroma;
	//intermediate value X for the second largest component of color
	double x = chroma *(1.0 - abs_value(fmod(hh, 2) - 1.0));
	if (hh >= 0.0 && hh <= 1.0)
	{
		green = x;
		red = chroma;
	}
	else if (hh >= 1.0 && hh <= 1.0)
	{
		red = x;
		green = chroma;
	}
	else if (hh >= 2.0 && hh < 3.0)
	{
		blue = x;
		green = chroma;
	}
	else if (hh >= 3.0 && hh < 4.0)
	{
		green = x;
		blue = chroma;
	}
	else if (hh >= 4.0 && hh < 5.0)
	{
		red = x;
		blue = chroma;
	}
	else
	{
		blue = x;
		red = chroma;
	}
	// Adding the same amount to each component (m)
	red += m;
	green += m;
	blue += m;
	// Multiply by 255
	red *= 255.0;
	green *= 255.0;
	blue *= 255.0;
	// Get the actual result values
	int r = round_value(red);
	int g = round_value(green);
	int b = round_value(blue);
	// Display Calculate results
	printf("\n Calculate  RGB");
	printf("\n R = %d ", r);
	printf("\n G = %d ", g);
	printf("\n B = %d \n\n", b);
}
int main()
{
	// Test Cases
	// Hex #FFFFFF
	hsv_to_rgb(0, 0, 100);
	// Hex #FF5622
	hsv_to_rgb(14, 86.7, 100.0);
	// Hex CA7D08
	hsv_to_rgb(36, 96.0, 79.2);
	return 0;
}

Output

 Given HSV
 H = 0.000000 °
 S = 0.000000 %
 V = 100.000000 %
 Calculate  RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14.000000 °
 S = 86.700000 %
 V = 100.000000 %
 Calculate  RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36.000000 °
 S = 96.000000 %
 V = 79.200000 %
 Calculate  RGB
 R = 202
 G = 124
 B = 8
/*
  Java Program
  HSV to RGB color
*/
public class ColorConversion
{
    // Returns absolute value
    public double abs_value(double value)
    {
        if (value < 0.0)
        {
            return - value;
        }
        else
        {
            return value;
        }
    }
    public int round_value(double num)
    {
        if (num < 0)
        {
            return  (int)(num - 0.5);
        }
        else
        {
            return (int)(num + 0.5);
        }
    }
    // Convert HSV to RGB
    public void hsv_to_rgb(double h, double s, double v)
    {
        // Display Given data
        System.out.print(" Given HSV ");
        // hue
        System.out.print("\n H = " + h + "°");
        // saturation
        System.out.print("\n S = " + s + "% ");
        // value
        System.out.print("\n V = " + v + "% ");
        // Define some variables
        double red = 0;
        double green = 0;
        double blue = 0;
        if (s != 0)
        {
            s /= 100.0;
        }
        if (v != 0)
        {
            v /= 100.0;
        }
        // First, we find chroma
        double chroma = v * s;
        double hh = h / 60.0;
        // find match value
        double m = v - chroma;
        //intermediate value X for the second largest component of color
        double x = chroma * (1.0 - abs_value((hh % 2) - 1.0));
        if (hh >= 0.0 && hh <= 1.0)
        {
            green = x;
            red = chroma;
        }
        else if (hh >= 1.0 && hh <= 1.0)
        {
            red = x;
            green = chroma;
        }
        else if (hh >= 2.0 && hh < 3.0)
        {
            blue = x;
            green = chroma;
        }
        else if (hh >= 3.0 && hh < 4.0)
        {
            green = x;
            blue = chroma;
        }
        else if (hh >= 4.0 && hh < 5.0)
        {
            red = x;
            blue = chroma;
        }
        else
        {
            blue = x;
            red = chroma;
        }
        // Adding the same amount to each component (m)
        red += m;
        green += m;
        blue += m;
        // Multiply by 255
        red *= 255.0;
        green *= 255.0;
        blue *= 255.0;
        // Get the actual result values
        int r = round_value(red);
        int g = round_value(green);
        int b = round_value(blue);
        // Display Calculate results
        System.out.print("\n Calculate RGB");
        System.out.print("\n R = " + r);
        System.out.print("\n G = " + g);
        System.out.print("\n B = " + b + " \n\n");
    }
    public static void main(String[] args)
    {
        ColorConversion color = new ColorConversion();
        // Test Cases
        // Hex #FFFFFF
        color.hsv_to_rgb(0, 0, 100);
        // Hex #FF5622
        color.hsv_to_rgb(14, 86.7, 100.0);
        // Hex CA7D08
        color.hsv_to_rgb(36, 96.0, 79.2);
    }
}

Output

 Given HSV
 H = 0.0°
 S = 0.0%
 V = 100.0%
 Calculate RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14.0°
 S = 86.7%
 V = 100.0%
 Calculate RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36.0°
 S = 96.0%
 V = 79.2%
 Calculate RGB
 R = 202
 G = 124
 B = 8
// Include header file
#include <iostream>
#include <math.h>
using namespace std;
/*
  C++ Program
  HSV to RGB color
*/
class ColorConversion
{
	public:
		// Returns absolute value
		double abs_value(double value)
		{
			if (value < 0.0)
			{
				return -value;
			}
			else
			{
				return value;
			}
		}
	int round_value(double num)
	{
		if (num < 0)
		{
			return (int)(num - 0.5);
		}
		else
		{
			return (int)(num + 0.5);
		}
	}
	// Convert HSV to RGB
	void hsv_to_rgb(double h, double s, double v)
	{
		// Display Given data
		cout << " Given HSV ";
		// hue
		cout << "\n H = " << h << "°";
		// saturation
		cout << "\n S = " << s << "% ";
		// value
		cout << "\n V = " << v << "% ";
		// Define some variables
		double red = 0;
		double green = 0;
		double blue = 0;
		if (s != 0)
		{
			s /= 100.0;
		}
		if (v != 0)
		{
			v /= 100.0;
		}
		// First, we find chroma
		double chroma = v *s;
		double hh = h / 60.0;
		// find match value
		double m = v - chroma;
		//intermediate value X for the second largest component of color
		double x = chroma *(1.0 - this->abs_value(fmod(hh , 2) - 1.0));
		if (hh >= 0.0 && hh <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (hh >= 1.0 && hh <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (hh >= 2.0 && hh < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (hh >= 3.0 && hh < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (hh >= 4.0 && hh < 5.0)
		{
			red = x;
			blue = chroma;
		}
		else
		{
			blue = x;
			red = chroma;
		}
		// Adding the same amount to each component (m)
		red += m;
		green += m;
		blue += m;
		// Multiply by 255
		red *= 255.0;
		green *= 255.0;
		blue *= 255.0;
		// Get the actual result values
		int r = this->round_value(red);
		int g = this->round_value(green);
		int b = this->round_value(blue);
		// Display Calculate results
		cout << "\n Calculate RGB";
		cout << "\n R = " << r;
		cout << "\n G = " << g;
		cout << "\n B = " << b << " \n\n";
	}
};
int main()
{
	ColorConversion color = ColorConversion();
	// Test Cases// Hex #FFFFFF
	color.hsv_to_rgb(0, 0, 100);
	// Hex #FF5622
	color.hsv_to_rgb(14, 86.7, 100.0);
	// Hex CA7D08
	color.hsv_to_rgb(36, 96.0, 79.2);
	return 0;
}

Output

 Given HSV
 H = 0°
 S = 0%
 V = 100%
 Calculate RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14°
 S = 86.7%
 V = 100%
 Calculate RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36°
 S = 96%
 V = 79.2%
 Calculate RGB
 R = 202
 G = 124
 B = 8
// Include namespace system
using System;
/*
  C# Program
  HSV to RGB color
*/
public class ColorConversion
{
	// Returns absolute value
	public double abs_value(double value)
	{
		if (value < 0.0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	public int round_value(double num)
	{
		if (num < 0)
		{
			return (int)(num - 0.5);
		}
		else
		{
			return (int)(num + 0.5);
		}
	}
	// Convert HSV to RGB
	public void hsv_to_rgb(double h, double s, double v)
	{
		// Display Given data
		Console.Write(" Given HSV ");
		// hue
		Console.Write("\n H = " + h + "°");
		// saturation
		Console.Write("\n S = " + s + "% ");
		// value
		Console.Write("\n V = " + v + "% ");
		// Define some variables
		double red = 0;
		double green = 0;
		double blue = 0;
		if (s != 0)
		{
			s /= 100.0;
		}
		if (v != 0)
		{
			v /= 100.0;
		}
		// First, we find chroma
		double chroma = v * s;
		double hh = h / 60.0;
		// find match value
		double m = v - chroma;
		//intermediate value X for the second largest component of color
		double x = chroma * (1.0 - abs_value((hh % 2) - 1.0));
		if (hh >= 0.0 && hh <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (hh >= 1.0 && hh <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (hh >= 2.0 && hh < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (hh >= 3.0 && hh < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (hh >= 4.0 && hh < 5.0)
		{
			red = x;
			blue = chroma;
		}
		else
		{
			blue = x;
			red = chroma;
		}
		// Adding the same amount to each component (m)
		red += m;
		green += m;
		blue += m;
		// Multiply by 255
		red *= 255.0;
		green *= 255.0;
		blue *= 255.0;
		// Get the actual result values
		int r = round_value(red);
		int g = round_value(green);
		int b = round_value(blue);
		// Display Calculate results
		Console.Write("\n Calculate RGB");
		Console.Write("\n R = " + r);
		Console.Write("\n G = " + g);
		Console.Write("\n B = " + b + " \n\n");
	}
	public static void Main(String[] args)
	{
		ColorConversion color = new ColorConversion();
		// Test Cases// Hex #FFFFFF
		color.hsv_to_rgb(0, 0, 100);
		// Hex #FF5622
		color.hsv_to_rgb(14, 86.7, 100.0);
		// Hex CA7D08
		color.hsv_to_rgb(36, 96.0, 79.2);
	}
}

Output

 Given HSV
 H = 0°
 S = 0%
 V = 100%
 Calculate RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14°
 S = 86.7%
 V = 100%
 Calculate RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36°
 S = 96%
 V = 79.2%
 Calculate RGB
 R = 202
 G = 124
 B = 8
<?php
/*
  Php Program
  HSV to RGB color
*/
class ColorConversion
{
	// Returns absolute value
	public	function abs_value($value)
	{
		if ($value < 0.0)
		{
			return -$value;
		}
		else
		{
			return $value;
		}
	}
	public	function round_value($num)
	{
		if ($num < 0)
		{
			return (int)($num - 0.5);
		}
		else
		{
			return (int)($num + 0.5);
		}
	}
	// Convert HSV to RGB
	public	function hsv_to_rgb($h, $s, $v)
	{
		// Display Given data
		echo " Given HSV ";
		// hue
		echo "\n H = ". $h ."°";
		// saturation
		echo "\n S = ". $s ."% ";
		// value
		echo "\n V = ". $v ."% ";
		// Define some variables
		$red = 0;
		$green = 0;
		$blue = 0;
		if ($s != 0)
		{
			$s = ($s / 100.0);
		}
		if ($v != 0)
		{
			$v = ($v / 100.0);
		}
		// First, we find chroma
		$chroma = $v * $s;
		$hh = ($h / 60.0);
		// find match value
		$m = $v - $chroma;
		//intermediate value X for the second largest component of color
		$x = $chroma * (1.0 - $this->abs_value(fmod($hh , 2) - 1.0));
		if ($hh >= 0.0 && $hh <= 1.0)
		{
			$green = $x;
			$red = $chroma;
		}
		else if ($hh >= 1.0 && $hh <= 1.0)
		{
			$red = $x;
			$green = $chroma;
		}
		else if ($hh >= 2.0 && $hh < 3.0)
		{
			$blue = $x;
			$green = $chroma;
		}
		else if ($hh >= 3.0 && $hh < 4.0)
		{
			$green = $x;
			$blue = $chroma;
		}
		else if ($hh >= 4.0 && $hh < 5.0)
		{
			$red = $x;
			$blue = $chroma;
		}
		else
		{
			$blue = $x;
			$red = $chroma;
		}
		// Adding the same amount to each component (m)
		$red += $m;
		$green += $m;
		$blue += $m;
		// Multiply by 255
		$red *= 255.0;
		$green *= 255.0;
		$blue *= 255.0;
		// Get the actual result values
		$r = $this->round_value($red);
		$g = $this->round_value($green);
		$b = $this->round_value($blue);
		// Display Calculate results
		echo "\n Calculate RGB";
		echo "\n R = ". $r;
		echo "\n G = ". $g;
		echo "\n B = ". $b ." \n\n";
	}
}

function main()
{
	$color = new ColorConversion();
	// Test Cases// Hex #FFFFFF
	$color->hsv_to_rgb(0, 0, 100);
	// Hex #FF5622
	$color->hsv_to_rgb(14, 86.7, 100.0);
	// Hex CA7D08
	$color->hsv_to_rgb(36, 96.0, 79.2);
}
main();

Output

 Given HSV
 H = 0°
 S = 0%
 V = 100%
 Calculate RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14°
 S = 86.7%
 V = 100%
 Calculate RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36°
 S = 96%
 V = 79.2%
 Calculate RGB
 R = 202
 G = 124
 B = 8
/*
  Node Js Program
  HSV to RGB color
*/
class ColorConversion
{
	// Returns absolute value
	abs_value(value)
	{
		if (value < 0.0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	round_value(num)
	{
		if (num < 0)
		{
			return parseInt((num - 0.5));
		}
		else
		{
			return parseInt((num + 0.5));
		}
	}
	// Convert HSV to RGB
	hsv_to_rgb(h, s, v)
	{
		// Display Given data
		process.stdout.write(" Given HSV ");
		// hue
		process.stdout.write("\n H = " + h + "°");
		// saturation
		process.stdout.write("\n S = " + s + "% ");
		// value
		process.stdout.write("\n V = " + v + "% ");
		// Define some variables
		var red = 0;
		var green = 0;
		var blue = 0;
		if (s != 0)
		{
			s = (s / 100.0);
		}
		if (v != 0)
		{
			v = (v / 100.0);
		}
		// First, we find chroma
		var chroma = v * s;
		var hh = (h / 60.0);
		// find match value
		var m = v - chroma;
		//intermediate value X for the second largest component of color
		var x = chroma * (1.0 - this.abs_value((hh % 2) - 1.0));
		if (hh >= 0.0 && hh <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (hh >= 1.0 && hh <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (hh >= 2.0 && hh < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (hh >= 3.0 && hh < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (hh >= 4.0 && hh < 5.0)
		{
			red = x;
			blue = chroma;
		}
		else
		{
			blue = x;
			red = chroma;
		}
		// Adding the same amount to each component (m)
		red += m;
		green += m;
		blue += m;
		// Multiply by 255
		red *= 255.0;
		green *= 255.0;
		blue *= 255.0;
		// Get the actual result values
		var r = this.round_value(red);
		var g = this.round_value(green);
		var b = this.round_value(blue);
		// Display Calculate results
		process.stdout.write("\n Calculate RGB");
		process.stdout.write("\n R = " + r);
		process.stdout.write("\n G = " + g);
		process.stdout.write("\n B = " + b + " \n\n");
	}
}

function main()
{
	var color = new ColorConversion();
	// Test Cases// Hex #FFFFFF
	color.hsv_to_rgb(0, 0, 100);
	// Hex #FF5622
	color.hsv_to_rgb(14, 86.7, 100.0);
	// Hex CA7D08
	color.hsv_to_rgb(36, 96.0, 79.2);
}
main();

Output

 Given HSV
 H = 0°
 S = 0%
 V = 100%
 Calculate RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14°
 S = 86.7%
 V = 100%
 Calculate RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36°
 S = 96%
 V = 79.2%
 Calculate RGB
 R = 202
 G = 124
 B = 8
#  Python 3 Program
#  HSV to RGB color

class ColorConversion :
	#  Returns absolute value
	def abs_value(self, value) :
		if (value < 0.0) :
			return -value
		else :
			return value
		
	
	def round_value(self, num) :
		if (num < 0) :
			return int((num - 0.5))
		else :
			return int((num + 0.5))
		
	
	#  Convert HSV to RGB
	def hsv_to_rgb(self, h, s, v) :
		if (h < 0 or s < 0 or v < 0) :
			return 0
		
		#  Display Given data
		print(" Given HSV ", end = "")
		#  hue
		print("\n H = ", h ,"°", end = "")
		#  saturation
		print("\n S = ", s ,"% ", end = "")
		#  value
		print("\n V = ", v ,"% ", end = "")
		#  Define some variables
		red = 0
		green = 0
		blue = 0
		if (s != 0) :
			s = (s / 100.0)
		
		if (v != 0) :
			v = (v / 100.0)
		
		#  First, we find chroma
		chroma = v * s
		hh = (h / 60.0)
		#  find match value
		m = v - chroma
		# intermediate value X for the second largest component of color
		x = chroma * (1.0 - self.abs_value((hh % 2) - 1.0))
		if (hh >= 0.0 and hh <= 1.0) :
			green = x
			red = chroma
		
		elif(hh >= 1.0 and hh <= 1.0) :
			red = x
			green = chroma
		
		elif(hh >= 2.0 and hh < 3.0) :
			blue = x
			green = chroma
		
		elif(hh >= 3.0 and hh < 4.0) :
			green = x
			blue = chroma
		
		elif(hh >= 4.0 and hh < 5.0) :
			red = x
			blue = chroma
		else :
			blue = x
			red = chroma
		
		#  Adding the same amount to each component (m)
		red += m
		green += m
		blue += m
		#  Multiply by 255
		red *= 255.0
		green *= 255.0
		blue *= 255.0
		#  Get the actual result values
		r = self.round_value(red)
		g = self.round_value(green)
		b = self.round_value(blue)
		#  Display Calculate results
		print("\n Calculate RGB", end = "")
		print("\n R = ", r, end = "")
		print("\n G = ", g, end = "")
		print("\n B = ", b ," \n")
	

def main() :
	color = ColorConversion()
	#  Test Cases
	#  Hex #FFFFFF
	color.hsv_to_rgb(0, 0, 100)
	#  Hex #FF5622
	color.hsv_to_rgb(14, 86.7, 100.0)
	#  Hex CA7D08
	color.hsv_to_rgb(36, 96.0, 79.2)

if __name__ == "__main__": main()

Output

 Given HSV
 H =  0 °
 S =  0 %
 V =  100 %
 Calculate RGB
 R =  255
 G =  255
 B =  255

 Given HSV
 H =  14 °
 S =  86.7 %
 V =  100.0 %
 Calculate RGB
 R =  255
 G =  86
 B =  34

 Given HSV
 H =  36 °
 S =  96.0 %
 V =  79.2 %
 Calculate RGB
 R =  202
 G =  124
 B =  8
# Ruby Program
# HSV to RGB color

class ColorConversion 
	#  Returns absolute value
	def abs_value(value) 
		if (value < 0.0) 
			return -value
		else 
			return value
		end

	end

	def round_value(num) 
		if (num < 0) 
			return ((num - 0.5)).to_i
		else 
			return ((num + 0.5)).to_i
		end

	end

	#  Convert HSV to RGB
	def hsv_to_rgb(h, s, v) 
		if (h < 0 || s < 0 || v < 0) 
			return 0
		end

		#  Display Given data
		print(" Given HSV ")
		#  hue
		print("\n H = ", h ,"°")
		#  saturation
		print("\n S = ", s ,"% ")
		#  value
		print("\n V = ", v ,"% ")
		#  Define some variables
		red = 0
		green = 0
		blue = 0
		if (s != 0) 
			s /= 100.0
		end

		if (v != 0) 
			v /= 100.0
		end

		#  First, we find chroma
		chroma = v * s
		hh = h / 60.0
		#  find match value
		m = v - chroma
		# intermediate value X for the second largest component of color
		x = chroma * (1.0 - self.abs_value((hh % 2) - 1.0))
		if (hh >= 0.0 && hh <= 1.0) 
			green = x
			red = chroma
		elsif(hh >= 1.0 && hh <= 1.0) 
			red = x
			green = chroma
		elsif(hh >= 2.0 && hh < 3.0) 
			blue = x
			green = chroma
		elsif(hh >= 3.0 && hh < 4.0) 
			green = x
			blue = chroma
		elsif(hh >= 4.0 && hh < 5.0) 
			red = x
			blue = chroma
		else 
			blue = x
			red = chroma
		end

		#  Adding the same amount to each component (m)
		red += m
		green += m
		blue += m
		#  Multiply by 255
		red *= 255.0
		green *= 255.0
		blue *= 255.0
		#  Get the actual result values
		r = self.round_value(red)
		g = self.round_value(green)
		b = self.round_value(blue)
		#  Display Calculate results
		print("\n Calculate RGB")
		print("\n R = ", r)
		print("\n G = ", g)
		print("\n B = ", b ," \n\n")
	end

end

def main() 
	color = ColorConversion.new()
	#  Test Cases
	#  Hex #FFFFFF
	color.hsv_to_rgb(0, 0, 100)
	#  Hex #FF5622
	color.hsv_to_rgb(14, 86.7, 100.0)
	#  Hex CA7D08
	color.hsv_to_rgb(36, 96.0, 79.2)
end

main()

Output

 Given HSV 
 H = 0°
 S = 0% 
 V = 100% 
 Calculate RGB
 R = 255
 G = 255
 B = 255 

 Given HSV 
 H = 14°
 S = 86.7% 
 V = 100.0% 
 Calculate RGB
 R = 255
 G = 86
 B = 34 

 Given HSV 
 H = 36°
 S = 96.0% 
 V = 79.2% 
 Calculate RGB
 R = 202
 G = 124
 B = 8 

/*
  Swift 4 Program
  HSV to RGB color
*/
class ColorConversion
{
	// Returns absolute value
	func abs_value(_ value: Double)->Double
	{
		if (value < 0.0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	func round_value(_ num: Double)->Int
	{
		if (num < 0)
		{
			return Int(num - 0.5);
		}
		else
		{
			return Int(num + 0.5);
		}
	}
	// Convert HSV to RGB
	func hsv_to_rgb(_ h: Double, _ saturation: Double, _ value: Double)
	{
		if (h < 0 || saturation < 0 || value < 0)
		{
			return;
		}
		// Display Given data
		print(" Given HSV ", terminator: "");
		// hue
		print("\n H = ", h, "°", terminator: "");
		// saturation
		print("\n S = ", saturation, "% ", terminator: "");
		// value
		print("\n V = ", value, "% ", terminator: "");
		// Define some variables
		var red: Double = 0.0;
		var green: Double = 0.0;
		var blue: Double = 0.0;
		var s: Double = saturation;
		var v: Double = value;
		if (s != 0.0)
		{
			s /= 100.0;
		}
		if (v != 0)
		{
			v /= 100.0;
		}
		// First, we find chroma
		let chroma: Double = v * s;
		let hh: Double = h / 60.0;
		// find match value
		let m: Double = v - chroma;
		//intermediate value X for the second largest component of color
		let x: Double = chroma * (1.0 - self.abs_value(hh.truncatingRemainder(dividingBy: 2) - 1.0));
		if (hh >= 0.0 && hh <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (hh >= 1.0 && hh <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (hh >= 2.0 && hh < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (hh >= 3.0 && hh < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (hh >= 4.0 && hh < 5.0)
		{
			red = x;
			blue = chroma;
		}
		else
		{
			blue = x;
			red = chroma;
		}
		// Adding the same amount to each component (m)
		red += m;
		green += m;
		blue += m;
		// Multiply by 255
		red *= 255.0;
		green *= 255.0;
		blue *= 255.0;
		// Get the actual result values
		let r: Int = self.round_value(red);
		let g: Int = self.round_value(green);
		let b: Int = self.round_value(blue);
		// Display Calculate results
		print("\n Calculate RGB", terminator: "");
		print("\n R = ", r, terminator: "");
		print("\n G = ", g, terminator: "");
		print("\n B = ", b, " \n");
	}
}
func main()
{
	let color: ColorConversion = ColorConversion();
	// Test Cases// Hex #FFFFFF
	color.hsv_to_rgb(0, 0, 100);
	// Hex #FF5622
	color.hsv_to_rgb(14, 86.7, 100.0);
	// Hex CA7D08
	color.hsv_to_rgb(36, 96.0, 79.2);
}
main();

Output

 Given HSV
 H =  0.0 °
 S =  0.0 %
 V =  100.0 %
 Calculate RGB
 R =  255
 G =  255
 B =  255

 Given HSV
 H =  14.0 °
 S =  86.7 %
 V =  100.0 %
 Calculate RGB
 R =  255
 G =  86
 B =  34

 Given HSV
 H =  36.0 °
 S =  96.0 %
 V =  79.2 %
 Calculate RGB
 R =  202
 G =  124
 B =  8
/*
  Kotlin Program
  HSV to RGB color
*/
class ColorConversion
{
	// Returns absolute value
	fun abs_value(value: Double): Double
	{
		if (value < 0.0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	fun round_value(num: Double): Int
	{
		if (num < 0.0)
		{
			return (num - 0.5).toInt();
		}
		else
		{
			return (num + 0.5).toInt();
		}
	}
	// Convert HSV to RGB
	fun hsv_to_rgb(hue: Any, saturation: Any, value: Any): Unit
	{
      	var s: Double = saturation.toString().toDouble();
      	var v: Double = value.toString().toDouble();
      	var h: Double = hue.toString().toDouble();
		if (h < 0.0 || s < 0.0 || v < 0.0)
		{
			return;
		}
		// Display Given data
		print(" Given HSV ");
		// hue
		print("\n H = " + h + "°");
		// saturation
		print("\n S = " + s + "% ");
		// value
		print("\n V = " + v + "% ");
      
      
		// Define some variables
		var red: Double = 0.0;
		var green: Double = 0.0;
		var blue: Double = 0.0;
      
		if (s != 0.0)
		{
			s /= 100.0;
		}
		if (v != 0.0)
		{
			v /= 100.0;
		}
		// First, we find chroma
		var chroma: Double = v * s;
		var hh: Double = h / 60.0;
		// find match value
		var m: Double = v - chroma;
		//intermediate value X for the second largest component of color
		var x: Double = chroma * (1.0 - this.abs_value((hh % 2) - 1.0));
		if (hh>= 0.0 && hh <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else
		if (hh>= 1.0 && hh <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else
		if (hh>= 2.0 && hh<3.0)
		{
			blue = x;
			green = chroma;
		}
		else
		if (hh>= 3.0 && hh<4.0)
		{
			green = x;
			blue = chroma;
		}
		else
		if (hh>= 4.0 && hh<5.0)
		{
			red = x;
			blue = chroma;
		}
		else
		{
			blue = x;
			red = chroma;
		}
		// Adding the same amount to each component (m)
		red += m;
		green += m;
		blue += m;
		// Multiply by 255
		red *= 255.0;
		green *= 255.0;
		blue *= 255.0;
		// Get the actual result values
		var r: Int = this.round_value(red);
		var g: Int = this.round_value(green);
		var b: Int = this.round_value(blue);
		// Display Calculate results
		print("\n Calculate RGB");
		print("\n R = " + r);
		print("\n G = " + g);
		print("\n B = " + b + " \n\n");
	}
}
fun main(args: Array<String>): Unit
{
	var color: ColorConversion = ColorConversion();
	// Test Cases// Hex #FFFFFF
	color.hsv_to_rgb(0, 0, 100);
	// Hex #FF5622
	color.hsv_to_rgb(14, 86.7, 100.0);
	// Hex CA7D08
	color.hsv_to_rgb(36, 96.0, 79.2);
}

Output

 Given HSV
 H = 0.0°
 S = 0.0%
 V = 100.0%
 Calculate RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14.0°
 S = 86.7%
 V = 100.0%
 Calculate RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36.0°
 S = 96.0%
 V = 79.2%
 Calculate RGB
 R = 202
 G = 124
 B = 8
/*
  Scala Program
  HSV to RGB color
*/
class ColorConversion
{
	// Returns absolute value
	def abs_value(value: Double): Double = {
		if (value < 0.0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	def round_value(num: Double): Int = {
		if (num < 0)
		{
			return ((num - 0.5)).toInt;
		}
		else
		{
			return ((num + 0.5)).toInt;
		}
	}
	// Convert HSV to RGB
	def hsv_to_rgb(h: Double, saturation: Double, value: Double): Unit = {
		
        if (h < 0 || saturation < 0 || value < 0)
		{
			return;
		}
		// Display Given data
		print(" Given HSV ");
		// hue
		print("\n H = " + h + "°");
		// saturation
		print("\n S = " + saturation + "% ");
		// value
		print("\n V = " + value + "% ");
		// Define some variables
		var red: Double = 0;
		var green: Double = 0;
		var blue: Double = 0;
		var s: Double = saturation;
		var v: Double = value;
		s = s / 100.0;
		v = v / 100.0;
		
		// First, we find chroma
		var chroma: Double = v * s;
		var hh: Double = (h / 60.0);
		// find match value
		var m: Double = v - chroma;
		//intermediate value X for the second largest component of color
		var x: Double = chroma * (1.0 - this.abs_value((hh % 2) - 1.0));
		if (hh >= 0.0 && hh <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (hh >= 1.0 && hh <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (hh >= 2.0 && hh < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (hh >= 3.0 && hh < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (hh >= 4.0 && hh < 5.0)
		{
			red = x;
			blue = chroma;
		}
		else
		{
			blue = x;
			red = chroma;
		}
		// Adding the same amount to each component (m)
		red += m;
		green += m;
		blue += m;
		// Multiply by 255
		red *= 255.0;
		green *= 255.0;
		blue *= 255.0;
		// Get the actual result values
		var r: Int = this.round_value(red);
		var g: Int = this.round_value(green);
		var b: Int = this.round_value(blue);
		// Display Calculate results
		print("\n Calculate RGB");
		print("\n R = " + r);
		print("\n G = " + g);
		print("\n B = " + b + " \n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var color: ColorConversion = new ColorConversion();
		// Test Cases// Hex #FFFFFF
		color.hsv_to_rgb(0, 0, 100);
		// Hex #FF5622
		color.hsv_to_rgb(14, 86.7, 100.0);
		// Hex CA7D08
		color.hsv_to_rgb(36, 96.0, 79.2);
	}
}

Output

 Given HSV
 H = 0.0°
 S = 0.0%
 V = 100.0%
 Calculate RGB
 R = 255
 G = 255
 B = 255

 Given HSV
 H = 14.0°
 S = 86.7%
 V = 100.0%
 Calculate RGB
 R = 255
 G = 86
 B = 34

 Given HSV
 H = 36.0°
 S = 96.0%
 V = 79.2%
 Calculate RGB
 R = 202
 G = 124
 B = 8

Output Explanation

Let's analyze the output for the three test cases provided.

  1. For the HSV color with hue = 0°, saturation = 0%, and value = 100%, the corresponding RGB values are R = 255, G = 255, and B = 255. This represents white.
  2. For the HSV color with hue = 14°, saturation = 86.7%, and value = 100%, the corresponding RGB values are R = 255, G = 86, and B = 34. This represents a shade of orange.
  3. For the HSV color with hue = 36°, saturation = 96%, and value = 79.2%, the corresponding RGB values are R = 202, G = 124, and B = 8. This represents a shade of brown.

The algorithm successfully converts the given HSV colors to their respective RGB representations.

Time Complexity

The time complexity of the given algorithm is O(1) because it performs a fixed number of calculations and operations regardless of the input values. The algorithm does not involve any loops or recursive calls that depend on the input size, so the time complexity remains constant.

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