# HSV to RGB in C++

In this tutorial, we are going to learn how to convert HSV to RGB in C++. HSV(hue, saturation, value) and RGB(red, green, blue) are color models used for various purposes such as graphics, etc. In the RGB model Red, Green and Blue are added together to produce a variety of colors. Similarly HSV color model is a cylindrical color model in which the variations in Hue, Saturation, and Value produces different colors.

Hue: It indicates the angle of the color in the RGB color circle.
Saturation: Saturation indicates the intensity of the color. 100% saturation indicates the purest form of the respective color.
Value: The brightness of the color is indicated by Value.

Ranges of HSV and RGB:

H(Hue): 0-360 degrees
S(Saturation): 0-100 percent
V(Value): 0-100 percent

R(Red): 0-255
G(Green): 0-255
B(Blue): 0-255

## Conversion of HSV to RGB

The formula for HSV to RGB conversion:

C = (V/100)*(S/100)

X = C*(1 – | ((H/60)mod 2 )-1 |)

m = (V/100) – C

r = C,g = X,b = 0  if(0<= H <60)

r = X,g = C,b = 0  if(60<= H <120)

r = 0,g = C,b = X  if(120<= H <180)

r = 0,g = X,b = C  if(180<= H <240)

r = X,g = 0,b = C  if(240<= H <300)

r = C,g = 0,b = X  if(300<= H <360)

R = (r+m)*255

G = (g+m)*255

B = (b+m)*255

Implementation in C++ program:

```#include <bits/stdc++.h>
using namespace std;
void HSVtoRGB(float H, float S,float V){
if(H>360 || H<0 || S>100 || S<0 || V>100 || V<0){
cout<<"The givem HSV values are not in valid range"<<endl;
return;
}
float s = S/100;
float v = V/100;
float C = s*v;
float X = C*(1-abs(fmod(H/60.0, 2)-1));
float m = v-C;
float r,g,b;
if(H >= 0 && H < 60){
r = C,g = X,b = 0;
}
else if(H >= 60 && H < 120){
r = X,g = C,b = 0;
}
else if(H >= 120 && H < 180){
r = 0,g = C,b = X;
}
else if(H >= 180 && H < 240){
r = 0,g = X,b = C;
}
else if(H >= 240 && H < 300){
r = X,g = 0,b = C;
}
else{
r = C,g = 0,b = X;
}

int R = (r+m)*255;
int G = (g+m)*255;
int B = (b+m)*255;

cout<<"R : "<<R<<endl;
cout<<"G : "<<G<<endl;
cout<<"B : "<<B<<endl;
}
int main(){
HSVtoRGB(100,60,40);
}```

Output:

```R : 61
G : 102
B : 40```

In the above code, the HSVtoRGB() function takes the Hue, Saturation, and Value as arguments and calculates the RGB values by using the formula that was given above.

We hope that you have got a clear idea of how to convert HSV to RGB in C++.