Skip to main content

HSL to RGB color

In the world of computer graphics and web development, colors play a vital role in creating visually appealing designs. One popular color model is the HSL (Hue, Saturation, Lightness) model, which provides a way to represent colors using intuitive parameters.

Problem Statement

The problem at hand is to convert an HSL color representation into its RGB (Red, Green, Blue) counterpart. HSL represents colors based on their hue, saturation, and lightness values, while RGB represents colors using intensities of the primary color channels: red, green, and blue.

Given an HSL color with values of hue (H), saturation (S), and lightness (L), we need to develop a program to convert it into its corresponding RGB color values.

Let's understand the problem with an example:

Consider an HSL color with the values:

  • Hue (H) = 34
  • Saturation (S) = 64%
  • Lightness (L) = 13%

To convert this HSL color to RGB, we need to follow a specific algorithm.

Algorithm

  1. Normalize the HSL values: If any of the HSL values are outside their valid range (H: 0-359, S/L: 0-100), adjust them accordingly.
  2. Normalize the saturation and lightness values: Convert S and L to the range of 0-1 by dividing them by 100.
  3. Calculate the chroma value: Chroma is the difference between the maximum and minimum values among R, G, and B.
  4. Calculate the hue prime value: Divide the hue (H) by 60.
  5. Calculate the intermediate value X: Multiply the chroma by (1 - |(H' mod 2) - 1|).
  6. Based on the hue prime value, assign the appropriate values to R, G, and B.
  7. Add the same amount to each component (m): Add the difference between L and chroma divided by 2 to R, G, and B.
  8. Multiply R, G, and B by 255.
  9. Round the resulting values of R, G, and B to the nearest integers.
  10. The resulting values represent the RGB color.

Pseudocode


abs_value(value)
    if value < 0.0
        return -value
    else
        return value

round_value(num)
    if num < 0
        return num - 0.5
    else
        return num + 0.5

hsl_to_rgb(h, s, l)
    if h < 0 or s < 0 or l < 0
        return

    if s > 100
        s = 100

    if l > 100
        l = 100

    if h > 359
        h = 100

    red = 0
    green = 0
    blue = 0

    if s != 0
        s = s / 100.0

    if l != 0
        l = l / 100.0

    chroma = s * (1 - abs_value(2 * l - 1))
    h1 = h / 60.0
    m = l - (chroma / 2)
    x = chroma * (1.0 - abs_value(h1 mod 2 - 1.0))

    if h1 >= 0.0 and h1 <= 1.0
        green = x
        red = chroma

    else if h1 >= 1.0 and h1 <= 1.0
        red = x
        green = chroma

    else if h1 >= 2.0 and h1 < 3.0
        blue = x
        green = chroma

    else if h1 >= 3.0 and h1 < 4.0
        green = x
        blue = chroma

    else if h1 >= 4.0 and h1 < 5.0
        red = x
        blue = chroma

    else
        blue = x
        red = chroma

    red = red + m
    green = green + m
    blue = blue + m

    red = red * 255.0
    green = green * 255.0
    blue = blue * 255.0

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

    return (r, g, b)

main()
    hsv_to_rgb(34, 64, 13)
    hsv_to_rgb(56, 64.64, 43.24)

Code Solution

Here given code implementation process.

// C program 
// HSL 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 HSL to RGB
void hsv_to_rgb(double h, double s, double l)
{
	if (h < 0 || s < 0 || l < 0)
	{
		return;
	}
	// Display Given data
	printf(" Given HSL ");
	// hue
	printf("\n H = %lf °", h);
	// saturation
	printf("\n S = %lf %% ", s);
	// lightness
	printf("\n L = %lf %% ", l);
	if (s > 100)
	{
		s = 100;
	}
	if (l > 100)
	{
		l = 100;
	}
	if (h > 359)
	{
		h = 100;
	}
	// Define some variables
	double red = 0;
	double green = 0;
	double blue = 0;
	if (s != 0)
	{
		s /= 100.0;
	}
	if (l != 0)
	{
		l /= 100.0;
	}
	// First, we find chroma
	double chroma = s *(1 - abs_value(2 *l - 1));
	double h1 = h / 60.0;
	// find match value
	double m = l - (chroma / 2);
	//intermediate value X for the second largest component of color
	double x = chroma *(1.0 - abs_value(fmod(h1, 2) - 1.0));
	if (h1 >= 0.0 && h1 <= 1.0)
	{
		green = x;
		red = chroma;
	}
	else if (h1 >= 1.0 && h1 <= 1.0)
	{
		red = x;
		green = chroma;
	}
	else if (h1 >= 2.0 && h1 < 3.0)
	{
		blue = x;
		green = chroma;
	}
	else if (h1 >= 3.0 && h1 < 4.0)
	{
		green = x;
		blue = chroma;
	}
	else if (h1 >= 4.0 && h1 < 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
	hsv_to_rgb(34, 64, 13);
	hsv_to_rgb(56, 64.64, 43.24);
	return 0;
}

Output

 Given HSL
 H = 34.000000 °
 S = 64.000000 %
 L = 13.000000 %
 Calculate  RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56.000000 °
 S = 64.640000 %
 L = 43.240000 %
 Calculate  RGB
 R = 182
 G = 172
 B = 39
/*
  Java Program
  CMYK 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 HSL to RGB
	public void hsv_to_rgb(double h, double s, double l)
	{
		if (h < 0 || s < 0 || l < 0)
		{
			return;
		}
		// Display Given data
		System.out.print(" Given HSL ");
		// hue
		System.out.print("\n H = " + h + " °");
		// saturation
		System.out.print("\n S = " + s + " % ");
		// lightness
		System.out.print("\n L = " + l + " % ");
		if (s > 100)
		{
			s = 100;
		}
		if (l > 100)
		{
			l = 100;
		}
		if (h > 359)
		{
			h = 100;
		}
		// Define some variables
		double red = 0;
		double green = 0;
		double blue = 0;
		if (s != 0)
		{
			s /= 100.0;
		}
		if (l != 0)
		{
			l /= 100.0;
		}
		// First, we find chroma
		double chroma = s * (1 - abs_value(2 * l - 1));
		double h1 = h / 60.0;
		// find match value
		double m = l - (chroma / 2);
		//intermediate value X for the second largest component of color
		double x = chroma * (1.0 - abs_value((h1 % 2) - 1.0));
		if (h1 >= 0.0 && h1 <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (h1 >= 1.0 && h1 <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (h1 >= 2.0 && h1 < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (h1 >= 3.0 && h1 < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (h1 >= 4.0 && h1 < 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 Case
		color.hsv_to_rgb(34, 64, 13);
		color.hsv_to_rgb(56, 64.64, 43.24);
	}
}

Output

 Given HSL
 H = 34.0 °
 S = 64.0 %
 L = 13.0 %
 Calculate RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56.0 °
 S = 64.64 %
 L = 43.24 %
 Calculate RGB
 R = 182
 G = 172
 B = 39
// Include header file
#include <iostream>
#include <math.h>
using namespace std;

/*
  C++ Program
  CMYK 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 HSL to RGB
	void hsv_to_rgb(double h, double s, double l)
	{
		if (h < 0 || s < 0 || l < 0)
		{
			return;
		}
		// Display Given data
		cout << " Given HSL ";
		// hue
		cout << "\n H = " << h << " °";
		// saturation
		cout << "\n S = " << s << " % ";
		// lightness
		cout << "\n L = " << l << " % ";
		if (s > 100)
		{
			s = 100;
		}
		if (l > 100)
		{
			l = 100;
		}
		if (h > 359)
		{
			h = 100;
		}
		// Define some variables
		double red = 0;
		double green = 0;
		double blue = 0;
		if (s != 0)
		{
			s /= 100.0;
		}
		if (l != 0)
		{
			l /= 100.0;
		}
		// First, we find chroma
		double chroma = s *(1 - this->abs_value(2 *l - 1));
		double h1 = h / 60.0;
		// find match value
		double m = l - (chroma / 2);
		//intermediate value X for the second largest component of color
		double x = chroma *(1.0 - this->abs_value(fmod(h1 , 2) - 1.0));
		if (h1 >= 0.0 && h1 <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (h1 >= 1.0 && h1 <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (h1 >= 2.0 && h1 < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (h1 >= 3.0 && h1 < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (h1 >= 4.0 && h1 < 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 Case
	color.hsv_to_rgb(34, 64, 13);
	color.hsv_to_rgb(56, 64.64, 43.24);
	return 0;
}

Output

 Given HSL
 H = 34 °
 S = 64 %
 L = 13 %
 Calculate RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56 °
 S = 64.64 %
 L = 43.24 %
 Calculate RGB
 R = 182
 G = 172
 B = 39
// Include namespace system
using System;
/*
  C# Program
  CMYK 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 HSL to RGB
	public void hsv_to_rgb(double h, double s, double l)
	{
		if (h < 0 || s < 0 || l < 0)
		{
			return;
		}
		// Display Given data
		Console.Write(" Given HSL ");
		// hue
		Console.Write("\n H = " + h + " °");
		// saturation
		Console.Write("\n S = " + s + " % ");
		// lightness
		Console.Write("\n L = " + l + " % ");
		if (s > 100)
		{
			s = 100;
		}
		if (l > 100)
		{
			l = 100;
		}
		if (h > 359)
		{
			h = 100;
		}
		// Define some variables
		double red = 0;
		double green = 0;
		double blue = 0;
		if (s != 0)
		{
			s /= 100.0;
		}
		if (l != 0)
		{
			l /= 100.0;
		}
		// First, we find chroma
		double chroma = s * (1 - abs_value(2 * l - 1));
		double h1 = h / 60.0;
		// find match value
		double m = l - (chroma / 2);
		//intermediate value X for the second largest component of color
		double x = chroma * (1.0 - abs_value((h1 % 2) - 1.0));
		if (h1 >= 0.0 && h1 <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (h1 >= 1.0 && h1 <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (h1 >= 2.0 && h1 < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (h1 >= 3.0 && h1 < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (h1 >= 4.0 && h1 < 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 Case
		color.hsv_to_rgb(34, 64, 13);
		color.hsv_to_rgb(56, 64.64, 43.24);
	}
}

Output

 Given HSL
 H = 34 °
 S = 64 %
 L = 13 %
 Calculate RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56 °
 S = 64.64 %
 L = 43.24 %
 Calculate RGB
 R = 182
 G = 172
 B = 39
<?php
/*
  Php Program
  CMYK 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 HSL to RGB
	public	function hsv_to_rgb($h, $s, $l)
	{
		if ($h < 0 || $s < 0 || $l < 0)
		{
			return;
		}
		// Display Given data
		echo " Given HSL ";
		// hue
		echo "\n H = ". $h ." °";
		// saturation
		echo "\n S = ". $s ." % ";
		// lightness
		echo "\n L = ". $l ." % ";
		if ($s > 100)
		{
			$s = 100;
		}
		if ($l > 100)
		{
			$l = 100;
		}
		if ($h > 359)
		{
			$h = 100;
		}
		// Define some variables
		$red = 0;
		$green = 0;
		$blue = 0;
		if ($s != 0)
		{
			$s = ($s / 100.0);
		}
		if ($l != 0)
		{
			$l = ($l / 100.0);
		}
		// First, we find chroma
		$chroma = $s * (1 - $this->abs_value(2 * $l - 1));
		$h1 = ($h / 60.0);
		// find match value
		$m = $l - (($chroma / 2));
		//intermediate value X for the second largest component of color
		$x = $chroma * (1.0 - $this->abs_value(fmod($h1 , 2) - 1.0));
		if ($h1 >= 0.0 && $h1 <= 1.0)
		{
			$green = $x;
			$red = $chroma;
		}
		else if ($h1 >= 1.0 && $h1 <= 1.0)
		{
			$red = $x;
			$green = $chroma;
		}
		else if ($h1 >= 2.0 && $h1 < 3.0)
		{
			$blue = $x;
			$green = $chroma;
		}
		else if ($h1 >= 3.0 && $h1 < 4.0)
		{
			$green = $x;
			$blue = $chroma;
		}
		else if ($h1 >= 4.0 && $h1 < 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 Case
	$color->hsv_to_rgb(34, 64, 13);
	$color->hsv_to_rgb(56, 64.64, 43.24);
}
main();

Output

 Given HSL
 H = 34 °
 S = 64 %
 L = 13 %
 Calculate RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56 °
 S = 64.64 %
 L = 43.24 %
 Calculate RGB
 R = 182
 G = 172
 B = 39
/*
  Node Js Program
  CMYK 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 HSL to RGB
	hsv_to_rgb(h, s, l)
	{
		if (h < 0 || s < 0 || l < 0)
		{
			return;
		}
		// Display Given data
		process.stdout.write(" Given HSL ");
		// hue
		process.stdout.write("\n H = " + h + " °");
		// saturation
		process.stdout.write("\n S = " + s + " % ");
		// lightness
		process.stdout.write("\n L = " + l + " % ");
		if (s > 100)
		{
			s = 100;
		}
		if (l > 100)
		{
			l = 100;
		}
		if (h > 359)
		{
			h = 100;
		}
		// Define some variables
		var red = 0;
		var green = 0;
		var blue = 0;
		if (s != 0)
		{
			s = (s / 100.0);
		}
		if (l != 0)
		{
			l = (l / 100.0);
		}
		// First, we find chroma
		var chroma = s * (1 - this.abs_value(2 * l - 1));
		var h1 = (h / 60.0);
		// find match value
		var m = l - ((chroma / 2));
		//intermediate value X for the second largest component of color
		var x = chroma * (1.0 - this.abs_value((h1 % 2) - 1.0));
		if (h1 >= 0.0 && h1 <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (h1 >= 1.0 && h1 <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (h1 >= 2.0 && h1 < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (h1 >= 3.0 && h1 < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (h1 >= 4.0 && h1 < 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 Case
	color.hsv_to_rgb(34, 64, 13);
	color.hsv_to_rgb(56, 64.64, 43.24);
}
main();

Output

 Given HSL
 H = 34 °
 S = 64 %
 L = 13 %
 Calculate RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56 °
 S = 64.64 %
 L = 43.24 %
 Calculate RGB
 R = 182
 G = 172
 B = 39
#   Python 3 Program
#   CMYK 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 HSL to RGB
	def hsv_to_rgb(self, h, s, l) :
		if (h < 0 or s < 0 or l < 0) :
			return
		
		#  Display Given data
		print(" Given HSL ", end = "")
		#  hue
		print("\n H = ", h ," °", end = "")
		#  saturation
		print("\n S = ", s ," % ", end = "")
		#  lightness
		print("\n L = ", l ," % ", end = "")
		if (s > 100) :
			s = 100
		
		if (l > 100) :
			l = 100
		
		if (h > 359) :
			h = 100
		
		#  Define some variables
		red = 0
		green = 0
		blue = 0
		if (s != 0) :
			s = (s / 100.0)
		
		if (l != 0) :
			l = (l / 100.0)
		
		#  First, we find chroma
		chroma = s * (1 - self.abs_value(2 * l - 1))
		h1 = (h / 60.0)
		#  find match value
		m = l - ((chroma / 2))
		# intermediate value X for the second largest component of color
		x = chroma * (1.0 - self.abs_value((h1 % 2) - 1.0))
		if (h1 >= 0.0 and h1 <= 1.0) :
			green = x
			red = chroma
		
		elif(h1 >= 1.0 and h1 <= 1.0) :
			red = x
			green = chroma
		
		elif(h1 >= 2.0 and h1 < 3.0) :
			blue = x
			green = chroma
		
		elif(h1 >= 3.0 and h1 < 4.0) :
			green = x
			blue = chroma
		
		elif(h1 >= 4.0 and h1 < 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 Case
	color.hsv_to_rgb(34, 64, 13)
	color.hsv_to_rgb(56, 64.64, 43.24)

if __name__ == "__main__": main()

Output

 Given HSL
 H =  34  °
 S =  64  %
 L =  13  %
 Calculate RGB
 R =  54
 G =  36
 B =  12

 Given HSL
 H =  56  °
 S =  64.64  %
 L =  43.24  %
 Calculate RGB
 R =  182
 G =  172
 B =  39
#   Ruby Program
#   CMYK 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 HSL to RGB
	def hsv_to_rgb(h, s, l) 
		if (h < 0 || s < 0 || l < 0) 
			return
		end

		#  Display Given data
		print(" Given HSL ")
		#  hue
		print("\n H = ", h ," °")
		#  saturation
		print("\n S = ", s ," % ")
		#  lightness
		print("\n L = ", l ," % ")
		if (s > 100) 
			s = 100
		end

		if (l > 100) 
			l = 100
		end

		if (h > 359) 
			h = 100
		end

		#  Define some variables
		red = 0
		green = 0
		blue = 0
		if (s != 0) 
			s /= 100.0
		end

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

		#  First, we find chroma
		chroma = s * (1 - self.abs_value(2 * l - 1))
		h1 = h / 60.0
		#  find match value
		m = l - (chroma / 2)
		# intermediate value X for the second largest component of color
		x = chroma * (1.0 - self.abs_value((h1 % 2) - 1.0))
		if (h1 >= 0.0 && h1 <= 1.0) 
			green = x
			red = chroma
		elsif(h1 >= 1.0 && h1 <= 1.0) 
			red = x
			green = chroma
		elsif(h1 >= 2.0 && h1 < 3.0) 
			blue = x
			green = chroma
		elsif(h1 >= 3.0 && h1 < 4.0) 
			green = x
			blue = chroma
		elsif(h1 >= 4.0 && h1 < 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 Case
	color.hsv_to_rgb(34, 64, 13)
	color.hsv_to_rgb(56, 64.64, 43.24)
end

main()

Output

 Given HSL 
 H = 34 °
 S = 64 % 
 L = 13 % 
 Calculate RGB
 R = 54
 G = 36
 B = 12 

 Given HSL 
 H = 56 °
 S = 64.64 % 
 L = 43.24 % 
 Calculate RGB
 R = 182
 G = 172
 B = 39 

/*
  Scala Program
  CMYK 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 HSL to RGB
	def hsv_to_rgb(hue: Double, saturation: Double, lightness: Double): Unit = {
		if (hue < 0 || saturation < 0 || lightness < 0)
		{
			return;
		}
		// Display Given data
		print(" Given HSL ");
		// hue
		print("\n H = " + hue + " °");
		// saturation
		print("\n S = " + saturation + " % ");
		// lightness
		print("\n L = " + lightness + " % ");
		var s = saturation;
		var l = lightness;
		var h = hue;
		if (s > 100.0)
		{
			s = 100.0;
		}
		if (l > 100)
		{
			l = 100.0;
		}
		if (h > 359)
		{
			h = 100.0;
		}
		// Define some variables
		var red: Double = 0;
		var green: Double = 0;
		var blue: Double = 0;
		if (s != 0)
		{
			s = (s / 100.0);
		}
		if (l != 0)
		{
			l = (l / 100.0);
		}
		// First, we find chroma
		var chroma: Double = s * (1 - this.abs_value(2 * l - 1));
		var h1: Double = (h / 60.0);
		// find match value
		var m: Double = l - (chroma / 2);
		//intermediate value X for the second largest component of color
		var x: Double = chroma * (1.0 - this.abs_value((h1 % 2) - 1.0));
		if (h1 >= 0.0 && h1 <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (h1 >= 1.0 && h1 <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (h1 >= 2.0 && h1 < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (h1 >= 3.0 && h1 < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (h1 >= 4.0 && h1 < 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 Case
		color.hsv_to_rgb(34, 64, 13);
		color.hsv_to_rgb(56, 64.64, 43.24);
	}
}

Output

 Given HSL
 H = 34.0 °
 S = 64.0 %
 L = 13.0 %
 Calculate RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56.0 °
 S = 64.64 %
 L = 43.24 %
 Calculate RGB
 R = 182
 G = 172
 B = 39
/*
  Swift 4 Program
  CMYK 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 HSL to RGB
	func hsv_to_rgb(_ hue: Double, _ saturation: Double, _ lightness: Double)
	{
		var s = saturation;
		var l = lightness;
		var h = hue;
		if (h < 0 || s < 0 || l < 0)
		{
			return;
		}
		// Display Given data
		print(" Given HSL ", terminator: "");
		// hue
		print("\n H = ", h, " °", terminator: "");
		// saturation
		print("\n S = ", s, " % ", terminator: "");
		// lightness
		print("\n L = ", l, " % ", terminator: "");
		if (s > 100)
		{
			s = 100;
		}
		if (l > 100)
		{
			l = 100;
		}
		if (h > 359)
		{
			h = 100;
		}
		// Define some variables
		var red: Double = 0;
		var green: Double = 0;
		var blue: Double = 0;
		if (s != 0)
		{
			s /= 100.0;
		}
		if (l != 0)
		{
			l /= 100.0;
		}
		// First, we find chroma
		let chroma: Double = s * (1 - self.abs_value(2 * l - 1));
		let h1: Double = h / 60.0;
		// find match value
		let m: Double = l - (chroma / 2);
		//intermediate value X for the second largest component of color
		let x: Double = chroma * (1.0 - self.abs_value((h1.truncatingRemainder(dividingBy: 2)) - 1.0));
		if (h1 >= 0.0 && h1 <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else if (h1 >= 1.0 && h1 <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else if (h1 >= 2.0 && h1 < 3.0)
		{
			blue = x;
			green = chroma;
		}
		else if (h1 >= 3.0 && h1 < 4.0)
		{
			green = x;
			blue = chroma;
		}
		else if (h1 >= 4.0 && h1 < 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 Case
	color.hsv_to_rgb(34, 64, 13);
	color.hsv_to_rgb(56, 64.64, 43.24);
}
main();

Output

 Given HSL
 H =  34.0  °
 S =  64.0  %
 L =  13.0  %
 Calculate RGB
 R =  54
 G =  36
 B =  12

 Given HSL
 H =  56.0  °
 S =  64.64  %
 L =  43.24  %
 Calculate RGB
 R =  182
 G =  172
 B =  39
/*
  Kotlin Program
  CMYK 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 HSL to RGB
	fun hsv_to_rgb(hue: Any, saturation: Any, lightness: Any): Unit
	{
      	var s: Double = saturation.toString().toDouble();
      	var l: Double = lightness.toString().toDouble();
      	var h: Double = hue.toString().toDouble();
      	if (h<0.0 || s<0.0 || l<0.0)
		{
			return;
		}
		// Display Given data
		print(" Given HSL ");
		// hue
		print("\n H = " + h + " °");
		// saturation
		print("\n S = " + s + " % ");
		// lightness
		print("\n L = " + l + " % ");
		if (s>100.0)
		{
			s = 100.0;
		}
		if (l>100.0)
		{
			l = 100.0;
		}
		if (h > 359.0)
		{
			h = 100.0;
		}
		// 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 (l != 0.0)
		{
			l /= 100.0;
		}
		// First, we find chroma
		var chroma: Double = s * (1 - this.abs_value(2 * l - 1));
		var h1: Double = h / 60.0;
		// find match value
		var m: Double = l - (chroma / 2);
		//intermediate value X for the second largest component of color
		var x: Double = chroma * (1.0 - this.abs_value((h1 % 2) - 1.0));
		if (h1>= 0.0 && h1 <= 1.0)
		{
			green = x;
			red = chroma;
		}
		else
		if (h1>= 1.0 && h1 <= 1.0)
		{
			red = x;
			green = chroma;
		}
		else
		if (h1>= 2.0 && h1<3.0)
		{
			blue = x;
			green = chroma;
		}
		else
		if (h1>= 3.0 && h1<4.0)
		{
			green = x;
			blue = chroma;
		}
		else
		if (h1>= 4.0 && h1<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 Case
	color.hsv_to_rgb(34, 64, 13);
	color.hsv_to_rgb(56, 64.64, 43.24);
}

Output

 Given HSL
 H = 34.0 °
 S = 64.0 %
 L = 13.0 %
 Calculate RGB
 R = 54
 G = 36
 B = 12

 Given HSL
 H = 56.0 °
 S = 64.64 %
 L = 43.24 %
 Calculate RGB
 R = 182
 G = 172
 B = 39

Explanation of Result

For the given HSL values:

  • Hue (H) = 34
  • Saturation (S) = 64%
  • Lightness (L) = 13%

The calculated RGB values are:

  • R = 54
  • G = 36
  • B = 12

This means that the corresponding RGB color is (54, 36, 12).

Similarly, for the second set of HSL values:

  • Hue (H) = 56
  • Saturation (S) = 64.64%
  • Lightness (L) = 43.24%

The calculated RGB values are:

  • R = 182
  • G = 172
  • B = 39

So, the corresponding RGB color is (182, 172, 39).

Time Complexity

The time complexity of the provided code is relatively low. Since it involves basic mathematical operations and a few conditional statements, the time complexity can be considered to be O(1) or constant time.





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