C++ Program to Change RGB to HSV color model

In this tutorial, we will learn how to change RGB color model to HSV color model using a C++ program.

Colors form a very important part of computer graphics. The way define colors in computers is a bit different from how we normally see them. Over the years, we have developed many systems with which we will be able to define these colors in a language that computers can comprehend them easily and also modify and play around with them as per our wish. Two of those systems or rather, models are the RGB and the HSV models. We will be looking at only these two today although these are just two of the many.

RGB refers to RED, GREEN, BLUE model while HSV refers to HUE, SATURATION, VALUE model.

Change RGB values to HSV using C++

RGB MODEL:

The RGB model makes use of the fact that when we mix these three primary colors, we will be able to achieve other colors. For that purpose, we represent these primary colors alone in three values (red, green, blue) respectively. Based on this combination, we can define other colors by bringing about the mix of the specified values. These individual values can range from 0, 255. To put this in simple words it specifies the ‘amount’ of that particular color in the overall color that we are trying to achieve. 0 means OFF, basically indicating that the color is black.  255 refers to the maximum extent of the color.

To make this clearer, here are some examples:

(0,0,0) = Black (no amount of any color)

(255,255,255)= White (max amount of all colors, gives us white)

(255,0,0) = Red (only red exists)

(0,255,0)=Green (only green exists)

(0,0,255)=Blue (only blue exists)

(128,0,128)= Purple

(255, 215, 0) =yellowish gold

We can define the RGB model as a three axis system with each value plotted on one of the axes. This leaves us with a system that is basically a cube because of the three axes.

HSV MODEL:

Coming to the HSV model, we can define this as a cylindrical system in contrast to the cube system we saw earlier. Imagine a cylinder along whose perimeter the color changes. That represents the hue change. Moving along the perimeter will introduce us to new colors. Now, we move along the height bottom to top of the cylinder along which we will discover that the value of the color changes. Finally, when we traverse along the radius of the cylinder keeping others constant, we see that the saturation levels of the color changes. As we shift the radius vector along different angles, we see that we can feel red at 0 degrees, green at 120 degrees and blue at 240 degrees. Later you will see that these values are important in calculating hue.

Code Explanation:

We will look at the code in C++ for achieving this task.

From the user, we get the values of the RGB values correspondingly and store them in a C++ vector. This is done because it is easier to calculate the minimum and maximum of these three in our function. The function returnHSVvalues will do the job. We pass this vector by reference to avoid creating a copy and work on it. The array hsv will store the hsv values once they are calculated. Also note that hsv values need to be stored in double values. For that reason, in our attempt to calculate hue, we need to use fmod function because % operator won’t work with double values.

Variables maxval and minval refer to the maximum and minimum of the three RGB values respectively. Variable difference is the difference between maxval and minval.

To calculate hue, we need to check the maxval variable. If the difference between maxval and minval is 0, then hue is also zero.  If not and maxval equals a color value(either Red, green or blue in that sequence), then hue is calculated using the other two color values as shown.

Note the values 360, 240 and 120 we add to them and the colors they correspond to on the cylinder.

Again if maxval is 0, then the saturation value also becomes zero. Otherwise, it is calculated as 100*(difference/maxval)

Value of the color is calculated as 100*maxval.

We append all these values to our hsv array and update it. We then display it for the user on the screen.

Code:

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>
#include "math.h"


void returnHSVvalues(vector<double>&rgb, double hsv[3]){       //Function declaration. This function will perform our task
    
                                      
    
    double hue, sat;
    
  
    double maxval, minval;
    
    maxval=*max_element(rgb.begin(), rgb.end());
    
    minval=*min_element(rgb.begin(), rgb.end());
    
    double difference=maxval-minval;
    
    double red, green, blue;
    red=rgb.at(0);
    green=rgb.at(1);
    blue=rgb.at(2);
    
    
    if(difference==0)
    hue=0;
    else if(red==maxval)
    hue= fmod(((60*((green-blue)/difference))+360), 360.0);
    else if(green=maxval)
    hue= fmod(((60*((blue-red)/difference))+120), 360.0);
    else if(blue=maxval)
    hue= fmod(((60*((red-green)/difference))+240),360.0);
    
    hsv[0] = (hue);
    
    
    if(maxval==0)
    sat=0;
    else
    sat=100*(difference/maxval);
    
    hsv[1] = (sat);
    
    hsv[2] = (maxval*100);

}


int main()
{
    cout<<"Enter RGB values (in the same order)"<<endl;
    
    double red, green, blue;
    cin>>red>>green>>blue;
    vector<double> rgb;
    rgb.push_back(red/255.0);
    rgb.push_back(green/255.0);
    rgb.push_back(blue/255.0);
    double hsv[3];
    
    cout<<"The RGB Values are "<<endl;
    for(int i=0; i<rgb.size(); i++)
    cout<<rgb.at(i)*255.0<<' ';
    cout<<endl;
    returnHSVvalues(rgb, hsv);
    cout<<"The HSV Values are "<<endl;
    for(int i=0; i<3; i++)
    cout<<hsv[i]<<" ";

    return 0;
}

 

Output:

Enter RGB values (in the same order)                                                                                                                                               

50 150 0                                                                                                                                                                           

The RGB Values are                                                                                                                                                                 

50 150 0                                                                                                                                                                           

The HSV Values are                                                                                                                                                                 

100 100 58.8235

Leave a Reply