Posted on by Kalkicode
Code Conversion

# RGB to HSV color

The RGB to HSV color conversion is a commonly used algorithm in computer graphics and image processing. RGB (Red, Green, Blue) and HSV (Hue, Saturation, Value) are different color models that represent colors in different ways. RGB represents colors as a combination of red, green, and blue components, while HSV represents colors as a combination of hue, saturation, and value components. The conversion between these two color models allows for more intuitive manipulation of colors, especially when it comes to adjusting attributes such as brightness and saturation.

## Problem Statement

The problem is to convert an RGB color value to its corresponding HSV representation. Given an RGB color with red (R), green (G), and blue (B) components ranging from 0 to 255, the task is to calculate the corresponding hue (H) value in degrees, saturation (S) value as a percentage, and value (V) also as a percentage.

For example, given an RGB color with R = 92, G = 191, and B = 178, the algorithm should output the corresponding HSV values: H = 172.121212°, S = 51.832461%, and V = 74.901961%.

## Algorithm

The algorithm for converting RGB to HSV involves several steps:

1. Normalize the RGB values by dividing each component by 255 to obtain values between 0 and 1.
2. Calculate the maximum and minimum values among the normalized R, G, and B components.
3. Calculate the difference (delta) between the maximum and minimum values.
4. Determine the hue (H) value:
• If the maximum value is equal to the red component, calculate H as ((G - B) / delta).
• If the maximum value is equal to the green component, calculate H as ((B - R) / delta) + 2.
• If the maximum value is equal to the blue component, calculate H as ((R - G) / delta) + 4.
• Multiply H by 60 to convert it to degrees.
5. If H is negative, add 360 to it.
6. Calculate the saturation (S) value as delta divided by the maximum value.
7. Calculate the value (V) as the maximum value.
8. Convert S and V to percentages by multiplying them by 100.
9. Display the RGB and HSV values.

## Pseudocode

```    ```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 rgb_to_hsv(red, green, blue)
if red > 255 or green > 255 or blue > 255
return
r = 0
g = 0
b = 0
if red != 0
r = red / 255.0
if green != 0
g = green / 255.0
if blue != 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
value = 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
if hue < 0
hue += 360
value = max
if value == 0
saturation = 0
else
saturation = delta / value
saturation = saturation * 100
value = value * 100
display("Given RGB")
display("R = " + red)
display("G = " + green)
display("B = " + blue)
display("Resultant HSV")
display("H = " + hue + "°")
display("S = " + saturation + "%")
display("V = " + value + "%")

rgb_to_hsv(92, 191, 178)
rgb_to_hsv(54, 155, 229)
rgb_to_hsv(68, 88, 204)
```
```

## Code Solution

Here given code implementation process.

``````// C program
// RGB to HSV 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;
}
}
void rgb_to_hsv(int red, int green, int blue)
{
if (red > 255 || green > 255 || blue > 255)
{
return;
}
double r = 0;
double g = 0;
double b = 0;
if (red != 0)
{
r = red / 255.0;
}
if (green != 0)
{
g = green / 255.0;
}
if (blue != 0)
{
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 value = 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;
}
if (hue < 0)
{
hue += 360;
}
// Calculate Value
value = max;
if (value == 0)
{
saturation = 0;
}
else
{
saturation = dalet / value;
}
// Convert into %
saturation = saturation *100;
value = value *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 Resultant HSV ");
printf("\n H = %lf °", hue);
printf("\n S = %lf %% ", saturation);
printf("\n V = %lf %% \n\n", value);
}
int main()
{
// Test Case
rgb_to_hsv(92, 191, 178);
rgb_to_hsv(54, 155, 229);
rgb_to_hsv(68, 88, 204);
return 0;
}``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.121212 °
S = 51.832461 %
V = 74.901961 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.371429 °
S = 76.419214 %
V = 89.803922 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.176471 °
S = 66.666667 %
V = 80.000000 %
``````
``````/*
Java Program
RGB to HSV 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;
}
}
public void rgb_to_hsv(int red, int green, int blue)
{
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
{
return;
}
double r = 0;
double g = 0;
double b = 0;
if (red != 0)
{
r = red / 255.0;
}
if (green != 0)
{
g = green / 255.0;
}
if (blue != 0)
{
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 value = 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;
}
if (hue < 0)
{
hue += 360;
}
// Calculate Value
value = max;
if (value == 0)
{
saturation = 0;
}
else
{
saturation = dalet / value;
}
// Convert into %
saturation = saturation * 100;
value = value * 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 Resultant HSV ");
System.out.print("\n H = " + hue + " °");
System.out.print("\n S = " + saturation + " % ");
System.out.print("\n V = " + value + " % \n\n");
}
public static void main(String[] args)
{
ColorConversion color = new ColorConversion();
// Test Case
color.rgb_to_hsv(92, 191, 178);
color.rgb_to_hsv(54, 155, 229);
color.rgb_to_hsv(68, 88, 204);
}
}``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.12121212121212 °
S = 51.832460732984295 %
V = 74.90196078431373 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.3714285714286 °
S = 76.41921397379913 %
V = 89.80392156862746 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.1764705882353 °
S = 66.66666666666667 %
V = 80.0 %
``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
RGB to HSV 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;
}
}
void rgb_to_hsv(int red, int green, int blue)
{
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
{
return;
}
double r = 0;
double g = 0;
double b = 0;
if (red != 0)
{
r = red / 255.0;
}
if (green != 0)
{
g = green / 255.0;
}
if (blue != 0)
{
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 value = 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;
}
if (hue < 0)
{
hue += 360;
}
// Calculate Value
value = max;
if (value == 0)
{
saturation = 0;
}
else
{
saturation = dalet / value;
}
// Convert into %
saturation = saturation *100;
value = value *100;
// Display Given results
cout << " Given RGB";
cout << "\n R = " << red;
cout << "\n G = " << green;
cout << "\n B = " << blue;
// Display Calculate results
cout << "\n Resultant HSV ";
cout << "\n H = " << hue << " °";
cout << "\n S = " << saturation << " % ";
cout << "\n V = " << value << " % \n\n";
}
};
int main()
{
ColorConversion color = ColorConversion();
// Test Case
color.rgb_to_hsv(92, 191, 178);
color.rgb_to_hsv(54, 155, 229);
color.rgb_to_hsv(68, 88, 204);
return 0;
}``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.121 °
S = 51.8325 %
V = 74.902 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.371 °
S = 76.4192 %
V = 89.8039 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.176 °
S = 66.6667 %
V = 80 %
``````
``````// Include namespace system
using System;
/*
C# Program
RGB to HSV 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;
}
}
public void rgb_to_hsv(int red, int green, int blue)
{
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
{
return;
}
double r = 0;
double g = 0;
double b = 0;
if (red != 0)
{
r = red / 255.0;
}
if (green != 0)
{
g = green / 255.0;
}
if (blue != 0)
{
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 value = 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;
}
if (hue < 0)
{
hue += 360;
}
// Calculate Value
value = max;
if (value == 0)
{
saturation = 0;
}
else
{
saturation = dalet / value;
}
// Convert into %
saturation = saturation * 100;
value = value * 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 Resultant HSV ");
Console.Write("\n H = " + hue + " °");
Console.Write("\n S = " + saturation + " % ");
Console.Write("\n V = " + value + " % \n\n");
}
public static void Main(String[] args)
{
ColorConversion color = new ColorConversion();
// Test Case
color.rgb_to_hsv(92, 191, 178);
color.rgb_to_hsv(54, 155, 229);
color.rgb_to_hsv(68, 88, 204);
}
}``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.121212121212 °
S = 51.8324607329843 %
V = 74.9019607843137 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.371428571429 °
S = 76.4192139737991 %
V = 89.8039215686275 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.176470588235 °
S = 66.6666666666667 %
V = 80 %
``````
``````<?php
/*
Php Program
RGB to HSV 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;
}
}
public	function rgb_to_hsv(\$red, \$green, \$blue)
{
if (\$red < 0 || \$green < 0 || \$blue < 0 || \$red > 255 || \$green > 255 || \$blue > 255)
{
return;
}
\$r = 0;
\$g = 0;
\$b = 0;
if (\$red != 0)
{
\$r = \$red / 255.0;
}
if (\$green != 0)
{
\$g = \$green / 255.0;
}
if (\$blue != 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;
\$value = 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;
}
if (\$hue < 0)
{
\$hue += 360;
}
// Calculate Value
\$value = \$max;
if (\$value == 0)
{
\$saturation = 0;
}
else
{
\$saturation = \$dalet / \$value;
}
// Convert into %
\$saturation = \$saturation * 100;
\$value = \$value * 100;
// Display Given results
echo " Given RGB";
echo "\n R = ". \$red;
echo "\n G = ". \$green;
echo "\n B = ". \$blue;
// Display Calculate results
echo "\n Resultant HSV ";
echo "\n H = ". \$hue ." °";
echo "\n S = ". \$saturation ." % ";
echo "\n V = ". \$value ." % \n\n";
}
}

function main()
{
\$color = new ColorConversion();
// Test Case
\$color->rgb_to_hsv(92, 191, 178);
\$color->rgb_to_hsv(54, 155, 229);
\$color->rgb_to_hsv(68, 88, 204);
}
main();``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.12121212121 °
S = 51.832460732984 %
V = 74.901960784314 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.37142857143 °
S = 76.419213973799 %
V = 89.803921568627 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.17647058824 °
S = 66.666666666667 %
V = 80 %
``````
``````/*
Node Js Program
RGB to HSV 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;
}
}
rgb_to_hsv(red, green, blue)
{
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
{
return;
}
var r = 0;
var g = 0;
var b = 0;
if (red != 0)
{
r = (red / 255.0);
}
if (green != 0)
{
g = (green / 255.0);
}
if (blue != 0)
{
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 value = 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;
}
if (hue < 0)
{
hue += 360;
}
// Calculate Value
value = max;
if (value == 0)
{
saturation = 0;
}
else
{
saturation = (dalet / value);
}
// Convert into %
saturation = saturation * 100;
value = value * 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 Resultant HSV ");
process.stdout.write("\n H = " + hue + " °");
process.stdout.write("\n S = " + saturation + " % ");
process.stdout.write("\n V = " + value + " % \n\n");
}
}

function main()
{
var color = new ColorConversion();
// Test Case
color.rgb_to_hsv(92, 191, 178);
color.rgb_to_hsv(54, 155, 229);
color.rgb_to_hsv(68, 88, 204);
}
main();``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.12121212121212 °
S = 51.832460732984295 %
V = 74.90196078431373 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.3714285714286 °
S = 76.41921397379913 %
V = 89.80392156862746 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.1764705882353 °
S = 66.66666666666667 %
V = 80 %
``````
``````#   Python 3 Program
#   RGB to HSV 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

def rgb_to_hsv(self, red, green, blue) :
if (red < 0 or green < 0 or blue < 0 or red > 255 or green > 255 or blue > 255) :
return

r = 0
g = 0
b = 0
if (red != 0) :
r = (red / 255.0)

if (green != 0) :
g = (green / 255.0)

if (blue != 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
value = 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

if (hue < 0) :
hue += 360

#  Calculate Value
value = max
if (value == 0) :
saturation = 0
else :
saturation = (dalet / value)

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

def main() :
color = ColorConversion()
#  Test Case
color.rgb_to_hsv(92, 191, 178)
color.rgb_to_hsv(54, 155, 229)
color.rgb_to_hsv(68, 88, 204)

if __name__ == "__main__": main()``````

#### Output

`````` Given RGB
R =  92
G =  191
B =  178
Resultant HSV
H =  172.12121212121212  °
S =  51.832460732984295  %
V =  74.90196078431373  %

Given RGB
R =  54
G =  155
B =  229
Resultant HSV
H =  205.3714285714286  °
S =  76.41921397379913  %
V =  89.80392156862746  %

Given RGB
R =  68
G =  88
B =  204
Resultant HSV
H =  231.1764705882353  °
S =  66.66666666666667  %
V =  80.0  %
``````
``````#   Ruby Program
#   RGB to HSV 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

def rgb_to_hsv(red, green, blue)
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
return
end

r = 0
g = 0
b = 0
if (red != 0)
r = red / 255.0
end

if (green != 0)
g = green / 255.0
end

if (blue != 0)
b = blue / 255.0
end

#  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
value = 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

if (hue < 0)
hue += 360
end

#  Calculate Value
value = max
if (value == 0)
saturation = 0
else
saturation = dalet / value
end

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

end

def main()
color = ColorConversion.new()
#  Test Case
color.rgb_to_hsv(92, 191, 178)
color.rgb_to_hsv(54, 155, 229)
color.rgb_to_hsv(68, 88, 204)
end

main()``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.12121212121212 °
S = 51.832460732984295 %
V = 74.90196078431373 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.3714285714286 °
S = 76.41921397379913 %
V = 89.80392156862746 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.1764705882353 °
S = 66.66666666666667 %
V = 80.0 %

``````
``````/*
Scala Program
RGB to HSV 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;
}
}
def rgb_to_hsv(red: Int, green: Int, blue: Int): Unit = {
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
{
return;
}
var r: Double = 0;
var g: Double = 0;
var b: Double = 0;
if (red != 0)
{
r = (red / 255.0);
}
if (green != 0)
{
g = (green / 255.0);
}
if (blue != 0)
{
b = (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 value: 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;
}
if (hue < 0)
{
hue += 360;
}
// Calculate Value
value = max;
if (value == 0)
{
saturation = 0;
}
else
{
saturation = (dalet / value);
}
// Convert into %
saturation = saturation * 100;
value = value * 100;
// Display Given results
print(" Given RGB");
print("\n R = " + red);
print("\n G = " + green);
print("\n B = " + blue);
// Display Calculate results
print("\n Resultant HSV ");
print("\n H = " + hue + " °");
print("\n S = " + saturation + " % ");
print("\n V = " + value + " % \n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var color: ColorConversion = new ColorConversion();
// Test Case
color.rgb_to_hsv(92, 191, 178);
color.rgb_to_hsv(54, 155, 229);
color.rgb_to_hsv(68, 88, 204);
}
}``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.12121212121212 °
S = 51.832460732984295 %
V = 74.90196078431373 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.3714285714286 °
S = 76.41921397379913 %
V = 89.80392156862746 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.1764705882353 °
S = 66.66666666666667 %
V = 80.0 %
``````
``````/*
Swift 4 Program
RGB to HSV 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;
}
}
func rgb_to_hsv(_ red: Int, _ green: Int, _ blue: Int)
{
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
{
return;
}
var r: Double = 0;
var g: Double = 0;
var b: Double = 0;
if (red != 0)
{
r = Double(red) / 255.0;
}
if (green != 0)
{
g = Double(green) / 255.0;
}
if (blue != 0)
{
b = 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 value: 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;
}
if (hue < 0)
{
hue += 360;
}
// Calculate Value
value = max;
if (value == 0)
{
saturation = 0;
}
else
{
saturation = dalet / value;
}
// Convert into %
saturation = saturation * 100;
value = value * 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 Resultant HSV ", terminator: "");
print("\n H = ", hue ," °", terminator: "");
print("\n S = ", saturation ," % ", terminator: "");
print("\n V = ", value ," % \n");
}
}
func main()
{
let color: ColorConversion = ColorConversion();
// Test Case
color.rgb_to_hsv(92, 191, 178);
color.rgb_to_hsv(54, 155, 229);
color.rgb_to_hsv(68, 88, 204);
}
main();``````

#### Output

`````` Given RGB
R =  92
G =  191
B =  178
Resultant HSV
H =  172.121212121212  °
S =  51.8324607329843  %
V =  74.9019607843137  %

Given RGB
R =  54
G =  155
B =  229
Resultant HSV
H =  205.371428571429  °
S =  76.4192139737991  %
V =  89.8039215686275  %

Given RGB
R =  68
G =  88
B =  204
Resultant HSV
H =  231.176470588235  °
S =  66.6666666666667  %
V =  80.0  %
``````
``````/*
Kotlin Program
RGB to HSV 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;
}
}
fun rgb_to_hsv(red: Int, green: Int, blue: Int): Unit
{
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255)
{
return;
}
var r: Double = 0.0;
var g: Double = 0.0;
var b: Double = 0.0;
if (red != 0)
{
r = red / 255.0;
}
if (green != 0)
{
g = green / 255.0;
}
if (blue != 0)
{
b = 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;
// Calculate Value
var value: Double = max;
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;
}
if (hue < 0)
{
hue += 360;
}
if (value != 0.0)
{
saturation = dalet / value;
}
// Convert into %
saturation = saturation * 100;
value = value * 100;
// Display Given results
print(" Given RGB");
print("\n R = " + red);
print("\n G = " + green);
print("\n B = " + blue);
// Display Calculate results
print("\n Resultant HSV ");
print("\n H = " + hue + " °");
print("\n S = " + saturation + " % ");
print("\n V = " + value + " % \n\n");
}
}
fun main(args: Array < String > ): Unit
{
var color: ColorConversion = ColorConversion();
// Test Case
color.rgb_to_hsv(92, 191, 178);
color.rgb_to_hsv(54, 155, 229);
color.rgb_to_hsv(68, 88, 204);
}``````

#### Output

`````` Given RGB
R = 92
G = 191
B = 178
Resultant HSV
H = 172.12121212121212 °
S = 51.832460732984295 %
V = 74.90196078431373 %

Given RGB
R = 54
G = 155
B = 229
Resultant HSV
H = 205.3714285714286 °
S = 76.41921397379913 %
V = 89.80392156862746 %

Given RGB
R = 68
G = 88
B = 204
Resultant HSV
H = 231.1764705882353 °
S = 66.66666666666667 %
V = 80.0 %
``````

## Resultant Output Explanation

For the given RGB color (92, 191, 178), the calculated HSV values are as follows: Hue (H) = 172.121212 degrees, Saturation (S) = 51.832461%, Value (V) = 74.901961%. These values represent the corresponding HSV color for the given RGB color.

Similarly, the HSV values for the other two RGB colors are calculated and displayed in the output.

## Time Complexity

The time complexity of the given code is constant, O(1), as it performs a fixed set of operations regardless of the input size. The code involves basic arithmetic calculations, conditional statements, and function calls, which have a constant time complexity.

## 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.

Categories
Relative Post