C Program to Change RGB color model to HSV color model


Given RGB color range in form of integers; the task is to find its appropriate HSV color by converting the RGB color range

What is RGB color model

RGB color model comprised of three colors Red, Green and Blue. RGB model is a color model which is widely used in the display technologies; it is an additive model in which we add these three colors of with different intensities to produce millions of different colors on a display device.

What is HSV color model?

HSV color model comprises Hue, Saturation, Value also known as HSB (Hue, Saturation, Brightness).HSV is alternative representation of RGB color model. It is aligned in a way how human vision perceives color making attributes. This color model more often used by artists due to it’s natural color scheme. The three attributes of the HSV can be additive as well as subtractive.

What we have to do in the program

We have to take input from the user of RGB model values and then mathematically calculate the output in HSV color model.

Example

Input: r = 31, g = 52, b = 29
Output: h s v = (114.782608, 44.230770, 20.392157)
Input: r = 129, g = 88, b = 47
Output: h s v=(30.000000, 63.565895, 50.588238)

Approach we will be using to solve the given problem −

  • Taking the input in 3 colors Red(r), Green(g) and Blue(b).
  • Dividing all the color values with 255.
  • Now calculate cmax, cmin and difference.
  • Check −
    • If cmax and cmin equal to 0, then Hue or h will be 0.
    • If cmax equal to Red(r) then Hue(h) = (60 * ((g – b) / diff) + 360) % 360.
    • If cmax equal to Green(g) then Hue(h) = (60 * ((b – r) / diff) + 120) % 360.
    • If cmax equal to Blue(b) then Hue(h) = (60 * ((r – g) / diff) + 240) % 360.
  • To find saturation we will check −
    • If cmax = 0 then Saturation (s) = 0.
    • If cmax does not equal to zero then Saturation (s) = (diff/cmax)*100
  • Value computation −
    • Value (v) = cmax *100

Algorithm

Start
Step 1 -> In function float max(float a, float b, float c)
   Return (a > b)? (a > c ? a : c) : (b > c ? b : c)
Step 2 -> In function float min(float a, float b, float c)
   Return (a < b)? (a < c ? a : c) : (b < c ? b : c)
Step 3 -> In function int rgb_to_hsv(float r, float g, float b)
   Declare float h, s, v
      Set r = r / 255.0
      Set g = g / 255.0
      Set b = b / 255.0
      Set cmax = max(r, g, b)
      Set cmin = min(r, g, b)
      Set diff = cmax-cmin
      If cmax == cmin then,
         Set h = 0
      End if
      Else if cmax == r then,
         Set h = fmod((60 * ((g - b) / diff) + 360), 360.0)
      End Else if
      Else if cmax == g then,
         Set h = fmod((60 * ((b - r) / diff) + 120), 360.0)
      End Else if
      Else if cmax == b then,
         Set h = fmod((60 * ((r - g) / diff) + 240), 360.0)
      End Else if
         If cmax == 0 then,
         Set s = 0
      End if
      Else
         Set s = (diff / cmax) * 100
      End Else
      v = cmax * 100;
      Print h, s, v
      Step 4 -> int main(int argc, char const *argv[])
      Declare and initialize r = 45, g = 215, b = 0
      Call function rgb_to_hsv(r, g, b)
Stop

Example

#include <stdio.h>
#include <math.h>
float max(float a, float b, float c) {
   return ((a > b)? (a > c ? a : c) : (b > c ? b : c));
}
float min(float a, float b, float c) {
   return ((a < b)? (a < c ? a : c) : (b < c ? b : c));
}
int rgb_to_hsv(float r, float g, float b) {
   // R, G, B values are divided by 255
   // to change the range from 0..255 to 0..1:
   float h, s, v;
   r /= 255.0;
   g /= 255.0;
   b /= 255.0;
   float cmax = max(r, g, b); // maximum of r, g, b
   float cmin = min(r, g, b); // minimum of r, g, b
   float diff = cmax-cmin; // diff of cmax and cmin.
   if (cmax == cmin)
      h = 0;
   else if (cmax == r)
      h = fmod((60 * ((g - b) / diff) + 360), 360.0);
   else if (cmax == g)
      h = fmod((60 * ((b - r) / diff) + 120), 360.0);
   else if (cmax == b)
      h = fmod((60 * ((r - g) / diff) + 240), 360.0);
   // if cmax equal zero
      if (cmax == 0)
         s = 0;
      else
         s = (diff / cmax) * 100;
   // compute v
   v = cmax * 100;
   printf("h s v=(%f, %f, %f)
", h, s, v );    return 0; } //main function int main(int argc, char const *argv[]) {    int r = 45, g = 215, b = 0;    rgb_to_hsv(r, g, b);    return 0; }

Output

h s v=(107.441864, 100.000000, 84.313728)

Updated on: 20-Nov-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements