Posted on by Kalkicode
Code Conversion

RGB to HSL color

The RGB to HSL color conversion is a common problem in computer graphics and image processing. RGB (Red, Green, Blue) and HSL (Hue, Saturation, Luminosity) are two different color models used to represent colors in digital systems. While RGB represents colors as combinations of red, green, and blue intensities, HSL represents colors based on their hue, saturation, and luminosity values.

In this problem, we are given an RGB color and we need to convert it into the corresponding HSL representation. The HSL values provide a more intuitive and human-friendly way to describe colors.

Let's consider an example to understand the problem better. Suppose we have an RGB color with the values:

R G B
255 152 0

To convert this RGB color to HSL, we need to perform the following steps:

  1. Normalize the RGB values by dividing them by 255 to obtain the corresponding values in the range [0, 1].
  2. Find the maximum and minimum values among the normalized RGB values.
  3. Calculate the difference between the maximum and minimum values (delta).
  4. Compute the hue value based on the maximum RGB component:
    • If the maximum component is red, calculate the hue as ((G - B) / delta).
    • If the maximum component is green, calculate the hue as ((B - R) / delta) + 2.
    • If the maximum component is blue, calculate the hue as ((R - G) / delta) + 4.
    • Normalize the hue value by multiplying it by 60.
  5. Calculate the luminosity as the average of the maximum and minimum RGB values.
  6. If delta is not zero, compute the saturation using the formula: delta / (1 - |2 * luminosity - 1|).
  7. Convert the saturation and luminosity values to percentages by multiplying them by 100.

Applying the above steps to the example RGB color (255, 152, 0), we get the following HSL values:

Hue (H) Saturation (S) Luminosity (L)
35.76° 100% 50%

The resulting HSL values provide a more intuitive representation of the color. The hue value represents the color's position on the color wheel, saturation indicates the color's intensity or purity, and luminosity represents the perceived brightness of the color.

Algorithm and Pseudocode

Here is the algorithm and pseudocode for converting RGB to HSL color:

function max_value(a, b)
    if a > b
        return a
    else
        return b

function min_value(a, b)
    if a < b
        return a
    else
        return b

function abs_value(value)
    if value < 0
        return -value
    else
        return value

function rgb_to_hsl(red, green, blue)
    r = red / 255.0
    g = green / 255.0
    b = blue / 255.0
    max = max_value(max_value(r, g), b)
    min = min_value(min_value(r, g), b)
    delta = max - min
    hue = 0.0
    saturation = 0.0
    luminosity = 0.0

    if delta > 0
        if max = r
            hue = ((g - b) / delta)
            if hue < 0
                hue = hue + 6
        else if max = g
            hue = ((b - r) / delta) + 2
        else if max = b
            hue = ((r - g) / delta) + 4
        hue = hue * 60

    luminosity = (max + min) / 2.0

    if delta != 0
        saturation = delta / (1 - abs_value(2 * luminosity - 1))

    saturation = saturation * 100
    luminosity = luminosity * 100

    return hue, saturation, luminosity

// Example usage
h, s, l = rgb_to_hsl(255, 152, 0)
print("Hue (H): ", h, "°")
print("Saturation (S): ", s, "%")
print("Luminosity (L): ", l, "%")

The above pseudocode outlines the steps to convert RGB to HSL. It defines helper functions for finding the maximum and minimum values and absolute value. The main function, rgb_to_hsl, takes the input RGB values, performs the necessary calculations, and returns the resulting HSL values.

Code Solution

Here given code implementation process.

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

// Returns the maximum value of given two numbers
double max_value(double a, double b)
{
    if (a > b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
// Returns the minimum value of given two numbers
double min_value(double a, double b)
{
    if (a < b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
// Returns absolute value
double abs_value(double value)
{
    if (value < 0)
    {
        return -value;
    }
    else
    {
        return value;
    }
}
void rgb_to_hsl(int red, int green, int blue)
{
    double r = red / 255.0;
    double g = green / 255.0;
    double b = blue / 255.0;
    // Find maximum of r,g,b
    double max = max_value(max_value(r, g), b);
    // Find Minimum of r,g,b
    double min = min_value(min_value(r, g), b);
    // Calculate difference
    double dalet = max - min;
    // Define useful resultant variables
    double hue = 0.0;
    double saturation = 0.0;
    double luminosity = 0.0;
    if (dalet > 0)
    {
        // Calculate hue
        if (max == r)
        {
            hue = ((g - b) / dalet);
            if (hue < 0)
            {
                hue = hue + 6;
            }
        }
        else if (max == g)
        {
            hue = ((b - r) / dalet) + 2;
        }
        else if (max == b)
        {
            hue = ((r - g) / dalet) + 4;
        }
        hue = hue *60;
    }
    // Calculate Luminosity
    // (Max(RGB) + Min(RGB)) / 2
    luminosity = (max + min) / 2.0;
    if (dalet != 0)
    {
        // Calculate Saturation
        // (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
        saturation = dalet / (1 - abs_value(2 *luminosity - 1));
    }
    // Convert into %
    saturation = saturation *100;
    luminosity = luminosity *100;

    // Display Given results
    printf(" Given RGB");
    printf("\n R = %d ", red);
    printf("\n G = %d ", green);
    printf("\n B = %d ", blue);

    // Display Calculate results
    printf("\n Calculate HSL ");
    printf("\n H = %lf °", hue);
    printf("\n S = %lf %% ", saturation);
    printf("\n L = %lf %% \n\n", luminosity);
}
int main()
{
    // Test Case
    rgb_to_hsl(255, 152, 0);
    rgb_to_hsl(54, 155, 229);
    rgb_to_hsl(33, 150, 243);
    return 0;
}

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.764706 °
 S = 100.000000 %
 L = 50.000000 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.371429 °
 S = 77.092511 %
 L = 55.490196 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.571429 °
 S = 89.743590 %
 L = 54.117647 %
/*
  Java Program
  RGB to HSL color
*/
public class ColorConversion
{
	// Returns the maximum value of given two numbers
	public double max_value(double a, double b)
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns the minimum value of given two numbers
	public double min_value(double a, double b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns absolute value
	public double abs_value(double value)
	{
		if (value < 0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	// Find HSL to given RGB
	public void rgb_to_hsl(int red, int green, int blue)
	{
		double r = red / 255.0;
		double g = green / 255.0;
		double b = blue / 255.0;
		// Find maximum of r,g,b
		double max = max_value(max_value(r, g), b);
		// Find Minimum of r,g,b
		double min = min_value(min_value(r, g), b);
		// Calculate difference
		double dalet = max - min;
		// Define useful resultant variables
		double hue = 0.0;
		double saturation = 0.0;
		double luminosity = 0.0;
		if (dalet > 0)
		{
			// Calculate hue
			if (max == r)
			{
				hue = ((g - b) / dalet);
				if (hue < 0)
				{
					hue = hue + 6;
				}
			}
			else if (max == g)
			{
				hue = ((b - r) / dalet) + 2;
			}
			else if (max == b)
			{
				hue = ((r - g) / dalet) + 4;
			}
			hue = hue * 60;
		}
		// Calculate Luminosity
		// (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0;
		if (dalet != 0)
		{
			// Calculate Saturation
			// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - abs_value(2 * luminosity - 1));
		}
		// Convert into %
		saturation = saturation * 100;
		luminosity = luminosity * 100;
		// 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);
		// Display Calculate results
		System.out.print("\n Calculate HSL ");
		System.out.print("\n H = " + hue + " °");
		System.out.print("\n S = " + saturation + " % ");
		System.out.print("\n L = " + luminosity + " % \n\n");
	}
	public static void main(String[] args)
	{
		ColorConversion color = new ColorConversion();
		// Test Case
		color.rgb_to_hsl(255, 152, 0);
		color.rgb_to_hsl(54, 155, 229);
		color.rgb_to_hsl(33, 150, 243);
	}
}

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.76470588235294 °
 S = 100.0 %
 L = 50.0 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.3714285714286 °
 S = 77.09251101321587 %
 L = 55.490196078431374 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.57142857142858 °
 S = 89.74358974358974 %
 L = 54.11764705882353 %
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  RGB to HSL color
*/
class ColorConversion
{
	public:
		// Returns the maximum value of given two numbers
		double max_value(double a, double b)
		{
			if (a > b)
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	// Returns the minimum value of given two numbers
	double min_value(double a, double b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns absolute value
	double abs_value(double value)
	{
		if (value < 0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	// Find HSL to given RGB
	void rgb_to_hsl(int red, int green, int blue)
	{
		double r = red / 255.0;
		double g = green / 255.0;
		double b = blue / 255.0;
		// Find maximum of r,g,b
		double max = this->max_value(this->max_value(r, g), b);
		// Find Minimum of r,g,b
		double min = this->min_value(this->min_value(r, g), b);
		// Calculate difference
		double dalet = max - min;
		// Define useful resultant variables
		double hue = 0.0;
		double saturation = 0.0;
		double luminosity = 0.0;
		if (dalet > 0)
		{
			// Calculate hue
			if (max == r)
			{
				hue = ((g - b) / dalet);
				if (hue < 0)
				{
					hue = hue + 6;
				}
			}
			else if (max == g)
			{
				hue = ((b - r) / dalet) + 2;
			}
			else if (max == b)
			{
				hue = ((r - g) / dalet) + 4;
			}
			hue = hue *60;
		}
		// Calculate Luminosity// (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0;
		if (dalet != 0)
		{
			// Calculate Saturation// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - this->abs_value(2 *luminosity - 1));
		}
		// Convert into %
		saturation = saturation *100;
		luminosity = luminosity *100;
		// Display Given results
		cout << " Given RGB";
		cout << "\n R = " << red;
		cout << "\n G = " << green;
		cout << "\n B = " << blue;
		// Display Calculate results
		cout << "\n Calculate HSL ";
		cout << "\n H = " << hue << " °";
		cout << "\n S = " << saturation << " % ";
		cout << "\n L = " << luminosity << " % \n\n";
	}
};
int main()
{
	ColorConversion color = ColorConversion();
	// Test Case
	color.rgb_to_hsl(255, 152, 0);
	color.rgb_to_hsl(54, 155, 229);
	color.rgb_to_hsl(33, 150, 243);
	return 0;
}

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.7647 °
 S = 100 %
 L = 50 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.371 °
 S = 77.0925 %
 L = 55.4902 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.571 °
 S = 89.7436 %
 L = 54.1176 %
// Include namespace system
using System;
/*
  C# Program
  RGB to HSL color
*/
public class ColorConversion
{
	// Returns the maximum value of given two numbers
	public double max_value(double a, double b)
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns the minimum value of given two numbers
	public double min_value(double a, double b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns absolute value
	public double abs_value(double value)
	{
		if (value < 0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	// Find HSL to given RGB
	public void rgb_to_hsl(int red, int green, int blue)
	{
		double r = red / 255.0;
		double g = green / 255.0;
		double b = blue / 255.0;
		// Find maximum of r,g,b
		double max = max_value(max_value(r, g), b);
		// Find Minimum of r,g,b
		double min = min_value(min_value(r, g), b);
		// Calculate difference
		double dalet = max - min;
		// Define useful resultant variables
		double hue = 0.0;
		double saturation = 0.0;
		double luminosity = 0.0;
		if (dalet > 0)
		{
			// Calculate hue
			if (max == r)
			{
				hue = ((g - b) / dalet);
				if (hue < 0)
				{
					hue = hue + 6;
				}
			}
			else if (max == g)
			{
				hue = ((b - r) / dalet) + 2;
			}
			else if (max == b)
			{
				hue = ((r - g) / dalet) + 4;
			}
			hue = hue * 60;
		}
		// Calculate Luminosity// (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0;
		if (dalet != 0)
		{
			// Calculate Saturation// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - abs_value(2 * luminosity - 1));
		}
		// Convert into %
		saturation = saturation * 100;
		luminosity = luminosity * 100;
		// Display Given results
		Console.Write(" Given RGB");
		Console.Write("\n R = " + red);
		Console.Write("\n G = " + green);
		Console.Write("\n B = " + blue);
		// Display Calculate results
		Console.Write("\n Calculate HSL ");
		Console.Write("\n H = " + hue + " °");
		Console.Write("\n S = " + saturation + " % ");
		Console.Write("\n L = " + luminosity + " % \n\n");
	}
	public static void Main(String[] args)
	{
		ColorConversion color = new ColorConversion();
		// Test Case
		color.rgb_to_hsl(255, 152, 0);
		color.rgb_to_hsl(54, 155, 229);
		color.rgb_to_hsl(33, 150, 243);
	}
}

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.7647058823529 °
 S = 100 %
 L = 50 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.371428571429 °
 S = 77.0925110132159 %
 L = 55.4901960784314 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.571428571429 °
 S = 89.7435897435897 %
 L = 54.1176470588235 %
<?php
/*
  Php Program
  RGB to HSL color
*/
class ColorConversion
{
	// Returns the maximum value of given two numbers
	public	function max_value($a, $b)
	{
		if ($a > $b)
		{
			return $a;
		}
		else
		{
			return $b;
		}
	}
	// Returns the minimum value of given two numbers
	public	function min_value($a, $b)
	{
		if ($a < $b)
		{
			return $a;
		}
		else
		{
			return $b;
		}
	}
	// Returns absolute value
	public	function abs_value($value)
	{
		if ($value < 0)
		{
			return -$value;
		}
		else
		{
			return $value;
		}
	}
	// Find HSL to given RGB
	public	function rgb_to_hsl($red, $green, $blue)
	{
		$r = ($red / 255.0);
		$g = ($green / 255.0);
		$b = ($blue / 255.0);
		// Find maximum of r,g,b
		$max = $this->max_value($this->max_value($r, $g), $b);
		// Find Minimum of r,g,b
		$min = $this->min_value($this->min_value($r, $g), $b);
		// Calculate difference
		$dalet = $max - $min;
		// Define useful resultant variables
		$hue = 0.0;
		$saturation = 0.0;
		$luminosity = 0.0;
		if ($dalet > 0)
		{
			// Calculate hue
			if ($max == $r)
			{
				$hue = (($g - $b) / $dalet);
				if ($hue < 0)
				{
					$hue = $hue + 6;
				}
			}
			else if ($max == $g)
			{
				$hue = (($b - $r) / $dalet) + 2;
			}
			else if ($max == $b)
			{
				$hue = (($r - $g) / $dalet) + 4;
			}
			$hue = $hue * 60;
		}
		// Calculate Luminosity// (Max(RGB) + Min(RGB)) / 2
		$luminosity = ($max + $min) / 2.0;
		if ($dalet != 0)
		{
			// Calculate Saturation// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			$saturation = $dalet / (1 - $this->abs_value(2 * $luminosity - 1));
		}
		// Convert into %
		$saturation = $saturation * 100;
		$luminosity = $luminosity * 100;
		// Display Given results
		echo " Given RGB";
		echo "\n R = ". $red;
		echo "\n G = ". $green;
		echo "\n B = ". $blue;
		// Display Calculate results
		echo "\n Calculate HSL ";
		echo "\n H = ". $hue ." °";
		echo "\n S = ". $saturation ." % ";
		echo "\n L = ". $luminosity ." % \n\n";
	}
}

function main()
{
	$color = new ColorConversion();
	// Test Case
	$color->rgb_to_hsl(255, 152, 0);
	$color->rgb_to_hsl(54, 155, 229);
	$color->rgb_to_hsl(33, 150, 243);
}
main();

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.764705882353 °
 S = 100 %
 L = 50 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.37142857143 °
 S = 77.092511013216 %
 L = 55.490196078431 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.57142857143 °
 S = 89.74358974359 %
 L = 54.117647058824 %
/*
  Node Js Program
  RGB to HSL color
*/
class ColorConversion
{
	// Returns the maximum value of given two numbers
	max_value(a, b)
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns the minimum value of given two numbers
	min_value(a, b)
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns absolute value
	abs_value(value)
	{
		if (value < 0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	// Find HSL to given RGB
	rgb_to_hsl(red, green, blue)
	{
		var r = (red / 255.0);
		var g = (green / 255.0);
		var b = (blue / 255.0);
		// Find maximum of r,g,b
		var max = this.max_value(this.max_value(r, g), b);
		// Find Minimum of r,g,b
		var min = this.min_value(this.min_value(r, g), b);
		// Calculate difference
		var dalet = max - min;
		// Define useful resultant variables
		var hue = 0.0;
		var saturation = 0.0;
		var luminosity = 0.0;
		if (dalet > 0)
		{
			// Calculate hue
			if (max == r)
			{
				hue = ((g - b) / dalet);
				if (hue < 0)
				{
					hue = hue + 6;
				}
			}
			else if (max == g)
			{
				hue = ((b - r) / dalet) + 2;
			}
			else if (max == b)
			{
				hue = ((r - g) / dalet) + 4;
			}
			hue = hue * 60;
		}
		// Calculate Luminosity// (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0;
		if (dalet != 0)
		{
			// Calculate Saturation// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - this.abs_value(2 * luminosity - 1));
		}
		// Convert into %
		saturation = saturation * 100;
		luminosity = luminosity * 100;
		// 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);
		// Display Calculate results
		process.stdout.write("\n Calculate HSL ");
		process.stdout.write("\n H = " + hue + " °");
		process.stdout.write("\n S = " + saturation + " % ");
		process.stdout.write("\n L = " + luminosity + " % \n\n");
	}
}

function main()
{
	var color = new ColorConversion();
	// Test Case
	color.rgb_to_hsl(255, 152, 0);
	color.rgb_to_hsl(54, 155, 229);
	color.rgb_to_hsl(33, 150, 243);
}
main();

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.76470588235294 °
 S = 100 %
 L = 50 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.3714285714286 °
 S = 77.09251101321587 %
 L = 55.490196078431374 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.57142857142858 °
 S = 89.74358974358974 %
 L = 54.11764705882353 %
#   Python 3 Program
#   RGB to HSL color

class ColorConversion :
	#  Returns the maximum value of given two numbers
	def max_value(self, a, b) :
		if (a > b) :
			return a
		else :
			return b
		
	
	#  Returns the minimum value of given two numbers
	def min_value(self, a, b) :
		if (a < b) :
			return a
		else :
			return b
		
	
	#  Returns absolute value
	def abs_value(self, value) :
		if (value < 0) :
			return -value
		else :
			return value
		
	
	#  Find HSL to given RGB
	def rgb_to_hsl(self, red, green, blue) :
		r = (red / 255.0)
		g = (green / 255.0)
		b = (blue / 255.0)
		#  Find maximum of r,g,b
		max = self.max_value(self.max_value(r, g), b)
		#  Find Minimum of r,g,b
		min = self.min_value(self.min_value(r, g), b)
		#  Calculate difference
		dalet = max - min
		#  Define useful resultant variables
		hue = 0.0
		saturation = 0.0
		luminosity = 0.0
		if (dalet > 0) :
			#  Calculate hue
			if (max == r) :
				hue = ((g - b) / dalet)
				if (hue < 0) :
					hue = hue + 6
				
			
			elif(max == g) :
				hue = ((b - r) / dalet) + 2
			
			elif(max == b) :
				hue = ((r - g) / dalet) + 4
			
			hue = hue * 60
		
		#  Calculate Luminosity
		#  (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0
		if (dalet != 0) :
			#  Calculate Saturation
			#  (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - self.abs_value(2 * luminosity - 1))
		
		#  Convert into %
		saturation = saturation * 100
		luminosity = luminosity * 100
		#  Display Given results
		print(" Given RGB")
		print(" R = ", red)
		print(" G = ", green)
		print(" B = ", blue)
		#  Display Calculate results
		print(" Calculate HSL ")
		print(" H = ", hue ,"°")
		print(" S = ", saturation ,"% ")
		print(" L = ", luminosity ,"% \n")
	

def main() :
	color = ColorConversion()
	#  Test Case
	color.rgb_to_hsl(255, 152, 0)
	color.rgb_to_hsl(54, 155, 229)
	color.rgb_to_hsl(33, 150, 243)

if __name__ == "__main__": main()

Output

 Given RGB
 R =  255
 G =  152
 B =  0
 Calculate HSL
 H =  35.76470588235294 °
 S =  100.0 %
 L =  50.0 %

 Given RGB
 R =  54
 G =  155
 B =  229
 Calculate HSL
 H =  205.3714285714286 °
 S =  77.09251101321587 %
 L =  55.490196078431374 %

 Given RGB
 R =  33
 G =  150
 B =  243
 Calculate HSL
 H =  206.57142857142858 °
 S =  89.74358974358974 %
 L =  54.11764705882353 %
#   Ruby Program
#   RGB to HSL color

class ColorConversion 
	#  Returns the maximum value of given two numbers
	def max_value(a, b) 
		if (a > b) 
			return a
		else 
			return b
		end

	end

	#  Returns the minimum value of given two numbers
	def min_value(a, b) 
		if (a < b) 
			return a
		else 
			return b
		end

	end

	#  Returns absolute value
	def abs_value(value) 
		if (value < 0) 
			return -value
		else 
			return value
		end

	end

	#  Find HSL to given RGB
	def rgb_to_hsl(red, green, blue) 
		r = red / 255.0
		g = green / 255.0
		b = blue / 255.0
		#  Find maximum of r,g,b
		max = self.max_value(self.max_value(r, g), b)
		#  Find Minimum of r,g,b
		min = self.min_value(self.min_value(r, g), b)
		#  Calculate difference
		dalet = max - min
		#  Define useful resultant variables
		hue = 0.0
		saturation = 0.0
		luminosity = 0.0
		if (dalet > 0) 
			#  Calculate hue
			if (max == r) 
				hue = ((g - b) / dalet)
				if (hue < 0) 
					hue = hue + 6
				end

			elsif(max == g) 
				hue = ((b - r) / dalet) + 2
			elsif(max == b) 
				hue = ((r - g) / dalet) + 4
			end

			hue = hue * 60
		end

		#  Calculate Luminosity
		#  (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0
		if (dalet != 0) 
			#  Calculate Saturation
			#  (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - self.abs_value(2 * luminosity - 1))
		end

		#  Convert into %
		saturation = saturation * 100
		luminosity = luminosity * 100
		#  Display Given results
		print(" Given RGB")
		print("\n R = ", red)
		print("\n G = ", green)
		print("\n B = ", blue)
		#  Display Calculate results
		print("\n Calculate HSL ")
		print("\n H = ", hue ," °")
		print("\n S = ", saturation ," % ")
		print("\n L = ", luminosity ," % \n\n")
	end

end

def main() 
	color = ColorConversion.new()
	#  Test Case
	color.rgb_to_hsl(255, 152, 0)
	color.rgb_to_hsl(54, 155, 229)
	color.rgb_to_hsl(33, 150, 243)
end

main()

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL 
 H = 35.76470588235294 °
 S = 100.0 % 
 L = 50.0 % 

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL 
 H = 205.3714285714286 °
 S = 77.09251101321587 % 
 L = 55.490196078431374 % 

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL 
 H = 206.57142857142858 °
 S = 89.74358974358974 % 
 L = 54.11764705882353 % 

/*
  Scala Program
  RGB to HSL color
*/
class ColorConversion
{
	// Returns the maximum value of given two numbers
	def max_value(a: Double, b: Double): Double = {
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns the minimum value of given two numbers
	def min_value(a: Double, b: Double): Double = {
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns absolute value
	def abs_value(value: Double): Double = {
		if (value < 0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	// Find HSL to given RGB
	def rgb_to_hsl(red: Int, green: Int, blue: Int): Unit = {
		var r: Double = (red / 255.0);
		var g: Double = (green / 255.0);
		var b: Double = (blue / 255.0);
		// Find maximum of r,g,b
		var max: Double = this.max_value(this.max_value(r, g), b);
		// Find Minimum of r,g,b
		var min: Double = this.min_value(this.min_value(r, g), b);
		// Calculate difference
		var dalet: Double = max - min;
		// Define useful resultant variables
		var hue: Double = 0.0;
		var saturation: Double = 0.0;
		var luminosity: Double = 0.0;
		if (dalet > 0)
		{
			// Calculate hue
			if (max == r)
			{
				hue = ((g - b) / dalet);
				if (hue < 0)
				{
					hue = hue + 6;
				}
			}
			else if (max == g)
			{
				hue = ((b - r) / dalet) + 2;
			}
			else if (max == b)
			{
				hue = ((r - g) / dalet) + 4;
			}
			hue = hue * 60;
		}
		// Calculate Luminosity// (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0;
		if (dalet != 0)
		{
			// Calculate Saturation// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - this.abs_value(2 * luminosity - 1));
		}
		// Convert into %
		saturation = saturation * 100;
		luminosity = luminosity * 100;
		// Display Given results
		print(" Given RGB");
		print("\n R = " + red);
		print("\n G = " + green);
		print("\n B = " + blue);
		// Display Calculate results
		print("\n Calculate HSL ");
		print("\n H = " + hue + " °");
		print("\n S = " + saturation + " % ");
		print("\n L = " + luminosity + " % \n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var color: ColorConversion = new ColorConversion();
		// Test Case
		color.rgb_to_hsl(255, 152, 0);
		color.rgb_to_hsl(54, 155, 229);
		color.rgb_to_hsl(33, 150, 243);
	}
}

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.76470588235294 °
 S = 100.0 %
 L = 50.0 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.3714285714286 °
 S = 77.09251101321587 %
 L = 55.490196078431374 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.57142857142858 °
 S = 89.74358974358974 %
 L = 54.11764705882353 %
/*
  Swift 4 Program
  RGB to HSL color
*/
class ColorConversion
{
	// Returns the maximum value of given two numbers
	func max_value(_ a: Double, _ b: Double)->Double
	{
		if (a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns the minimum value of given two numbers
	func min_value(_ a: Double, _ b: Double)->Double
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns absolute value
	func abs_value(_ value: Double)->Double
	{
		if (value < 0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	// Find HSL to given RGB
	func rgb_to_hsl(_ red: Int, _ green: Int, _ blue: Int)
	{
		let r: Double = Double(red) / 255.0;
		let g: Double = Double(green) / 255.0;
		let b: Double = Double(blue) / 255.0;
		// Find maximum of r,g,b
		let max: Double = self.max_value(self.max_value(r, g), b);
		// Find Minimum of r,g,b
		let min: Double = self.min_value(self.min_value(r, g), b);
		// Calculate difference
		let dalet: Double = max - min;
		// Define useful resultant variables
		var hue: Double = 0.0;
		var saturation: Double = 0.0;
		var luminosity: Double = 0.0;
		if (dalet > 0)
		{
			// Calculate hue
			if (max == r)
			{
				hue = ((g - b) / dalet);
				if (hue < 0)
				{
					hue = hue + 6;
				}
			}
			else if (max == g)
			{
				hue = ((b - r) / dalet) + 2;
			}
			else if (max == b)
			{
				hue = ((r - g) / dalet) + 4;
			}
			hue = hue * 60;
		}
		// Calculate Luminosity// (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0;
		if (dalet != 0)
		{
			// Calculate Saturation// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - self.abs_value(2 * luminosity - 1));
		}
		// Convert into %
		saturation = saturation * 100;
		luminosity = luminosity * 100;
		// Display Given results
		print(" Given RGB", terminator: "");
		print("\n R = ", red, terminator: "");
		print("\n G = ", green, terminator: "");
		print("\n B = ", blue, terminator: "");
		// Display Calculate results
		print("\n Calculate HSL ", terminator: "");
		print("\n H = ", hue ," °", terminator: "");
		print("\n S = ", saturation ," % ", terminator: "");
		print("\n L = ", luminosity ," % \n");
	}
}
func main()
{
	let color: ColorConversion = ColorConversion();
	// Test Case
	color.rgb_to_hsl(255, 152, 0);
	color.rgb_to_hsl(54, 155, 229);
	color.rgb_to_hsl(33, 150, 243);
}
main();

Output

 Given RGB
 R =  255
 G =  152
 B =  0
 Calculate HSL
 H =  35.7647058823529  °
 S =  100.0  %
 L =  50.0  %

 Given RGB
 R =  54
 G =  155
 B =  229
 Calculate HSL
 H =  205.371428571429  °
 S =  77.0925110132159  %
 L =  55.4901960784314  %

 Given RGB
 R =  33
 G =  150
 B =  243
 Calculate HSL
 H =  206.571428571429  °
 S =  89.7435897435897  %
 L =  54.1176470588235  %
/*
  Kotlin Program
  RGB to HSL color
*/
class ColorConversion
{
	// Returns the maximum value of given two numbers
	fun max_value(a: Double  , b : Double  ): Double 
	{
		if (a>b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns the minimum value of given two numbers
	fun min_value(a: Double  , b : Double  ): Double 
	{
		if (a<b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	// Returns absolute value
	fun abs_value(value: Double  ): Double 
	{
		if (value<0)
		{
			return -value;
		}
		else
		{
			return value;
		}
	}
	// Find HSL to given RGB
	fun rgb_to_hsl(red: Int, green: Int, blue: Int): Unit
	{
		var r: Double  = red / 255.0;
		var g: Double  = green / 255.0;
		var b: Double  = blue / 255.0;
		// Find maximum of r,g,b
		var max: Double  = this.max_value(this.max_value(r, g), b);
		// Find Minimum of r,g,b
		var min: Double  = this.min_value(this.min_value(r, g), b);
		// Calculate difference
		var dalet: Double  = max - min;
		// Define useful resultant variables
		var hue: Double  = 0.0;
		var saturation: Double  = 0.0;
		var luminosity: Double  ;
		if (dalet>0)
		{
			// Calculate hue
			if (max == r)
			{
				hue = ((g - b) / dalet);
				if (hue<0)
				{
					hue = hue + 6;
				}
			}
			else
			if (max == g)
			{
				hue = ((b - r) / dalet) + 2;
			}
			else
			if (max == b)
			{
				hue = ((r - g) / dalet) + 4;
			}
			hue = hue * 60;
		}
		// Calculate Luminosity// (Max(RGB) + Min(RGB)) / 2
		luminosity = (max + min) / 2.0;
		if (dalet != 0.0)
		{
			// Calculate Saturation// (Max(RGB) — Min(RGB)) / (1 — |2L - 1|)
			saturation = dalet / (1 - this.abs_value(2 * luminosity - 1));
		}
		// Convert into %
		saturation = saturation * 100;
		luminosity = luminosity * 100;
		// Display Given results
		print(" Given RGB");
		print("\n R = " + red);
		print("\n G = " + green);
		print("\n B = " + blue);
		// Display Calculate results
		print("\n Calculate HSL ");
		print("\n H = " + hue + " °");
		print("\n S = " + saturation + " % ");
		print("\n L = " + luminosity + " % \n\n");
	}
}
fun main(args: Array<String>): Unit
{
	var color: ColorConversion = ColorConversion();
	// Test Case
	color.rgb_to_hsl(255, 152, 0);
	color.rgb_to_hsl(54, 155, 229);
	color.rgb_to_hsl(33, 150, 243);
}

Output

 Given RGB
 R = 255
 G = 152
 B = 0
 Calculate HSL
 H = 35.76470588235294 °
 S = 100.0 %
 L = 50.0 %

 Given RGB
 R = 54
 G = 155
 B = 229
 Calculate HSL
 H = 205.3714285714286 °
 S = 77.09251101321587 %
 L = 55.490196078431374 %

 Given RGB
 R = 33
 G = 150
 B = 243
 Calculate HSL
 H = 206.57142857142858 °
 S = 89.74358974358974 %
 L = 54.11764705882353 %

Resultant Output Explanation

Using the provided example inputs:

  1. RGB values: (255, 152, 0)
  2. Normalized RGB values: (1.0, 0.596, 0.0)
  3. Maximum value: 1.0
  4. Minimum value: 0.0
  5. Delta: 1.0
  6. Hue: ((0.596 - 0.0) / 1.0) * 60 = 35.76°
  7. Luminosity: (1.0 + 0.0) / 2.0 = 0.5
  8. Saturation: 1.0 / (1 - |2 * 0.5 - 1|) = 1.0 / (1 - 0) = 1.0
  9. HSL values: (35.76°, 100%, 50%)

For the given RGB color (255, 152, 0), the resulting HSL values are H = 35.76°, S = 100%, and L = 50%. These values indicate that the color is a vibrant shade of orange with maximum saturation and medium luminosity.

Time Complexity Analysis

The time complexity of the provided code is relatively low as it performs a fixed number of calculations based on the RGB values. The dominant operations involve finding the maximum and minimum values, which require comparing three numbers. Therefore, the time complexity can be considered as constant or O(1) in terms of the input size.

Finally

The RGB to HSL color conversion is a common problem in computer graphics and image processing. Converting colors from the RGB model to the HSL model provides a more intuitive representation, making it easier to understand and work with colors.

In this article, we discussed the problem statement, explained the algorithm for converting RGB to HSL color, and provided pseudocode for the conversion process. We also walked through an example, demonstrating how the algorithm is applied to a specific RGB color to obtain the corresponding HSL values.

Additionally, we analyzed the time complexity of the code and concluded that it has a constant time complexity of O(1). This means that the code performs its calculations efficiently and is not affected by the input size.

Understanding the RGB to HSL color conversion is valuable knowledge for anyone working with colors in various applications, including graphic design, image processing, and web development. By leveraging the HSL color model, developers and designers can manipulate and describe colors in a more intuitive and visually meaningful way.

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