English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C program to change the RGB color model to HSV color model

Given the RGB color range (in integer form); the task is to find the appropriate HSV color by converting the RGB color range

What is the RGB color model

The RGB color model consists of red, green, and blue three colors. The RGB model is a color model widely used in display technology. It is an additive model where we add these three colors of different intensities to produce millions of different colors on display devices.

What is the HSV color model?

The HSV color model includes hue, saturation, and value, also known as HSB (hue, saturation, brightness). HSV is an alternative representation to the RGB color model. It is adjusted in a way that aligns with human visual perception of color attributes. Due to its natural color scheme, this color model is often used by artists. The three properties of HSV can be added or subtracted.

What we need to do in the program

We must obtain input from the user of the RGB model values and then calculate the output in the HSV color model in a mathematical way.

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)

The method we will use to solve the given problem-

  • Input in red(r), green(g), and blue(b) three colors.

  • Divide all color values by255.

  • Now calculate cmax, cmin, and the difference.

  • Check-

    • If cmax and cmin equal 0, then hue or h will be 0.

    • If cmax equals Red(r), then hue(h) = (60 *((g – b)/ diff)+ 360)%360.

    • If cmax equals Green(g), then hue(h) = (60 *((b – r)/ diff)+ 120)%360.

    • If cmax equals Blue(b), then hue(h) = (60 *((r – g)/ diff)+ 240)%360.

  • To find the saturation, we will check-

    • If cmax = 0, then the saturation (s) = 0.

    • If cmax is not equal to zero, then, saturation(s) = (diff / cmax)* 100

  • Value calculation-

    • 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 divided by255-
   //Change the range from 0..255Change 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; // Difference 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 is zero
      if (cmax == 0)
         s = 0;
      else
         s = (diff / cmax) * 100;
   //Calculate v-
   v = cmax * 100;
   printf("h s v=(%f, %f, %f)\n", 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 Result

h s v=(107.441864, 100.000000, 84.313728)