RGB to HSL color

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 %


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







© 2021, kalkicode.com, All rights reserved