Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. This conversion helps translate between the hardware-oriented RGB model and the more intuitive HSV model used in graphics and design applications.
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 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 its natural color scheme. The three attributes of the HSV can be additive as well as subtractive.
Syntax
void rgb_to_hsv(float r, float g, float b, float *h, float *s, float *v);
Algorithm
The conversion follows these mathematical steps −
- Normalize RGB values by dividing by 255
- Find maximum (cmax) and minimum (cmin) values
- Calculate difference = cmax - cmin
-
Hue calculation:
- If cmax == cmin, then h = 0
- If cmax == r, then h = (60 * ((g - b) / diff) + 360) % 360
- If cmax == g, then h = (60 * ((b - r) / diff) + 120) % 360
- If cmax == b, then h = (60 * ((r - g) / diff) + 240) % 360
- Saturation: s = (cmax == 0) ? 0 : (diff/cmax) * 100
- Value: v = cmax * 100
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));
}
void 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 */
/* Hue calculation */
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);
/* Saturation calculation */
if (cmax == 0)
s = 0;
else
s = (diff / cmax) * 100;
/* Value calculation */
v = cmax * 100;
printf("RGB(%d, %d, %d) = HSV(%.2f, %.2f, %.2f)<br>",
(int)(r*255), (int)(g*255), (int)(b*255), h, s, v);
}
int main() {
/* Test with different RGB values */
printf("Converting RGB to HSV:<br>");
rgb_to_hsv(45, 215, 0);
rgb_to_hsv(31, 52, 29);
rgb_to_hsv(129, 88, 47);
return 0;
}
Converting RGB to HSV: RGB(45, 215, 0) = HSV(107.44, 100.00, 84.31) RGB(31, 52, 29) = HSV(114.78, 44.23, 20.39) RGB(129, 88, 47) = HSV(30.00, 63.57, 50.59)
Key Points
- Hue represents the color type (0-360 degrees on color wheel)
- Saturation represents color intensity (0-100%)
- Value represents brightness (0-100%)
- The
fmod()function ensures hue stays within 0-360 range
Conclusion
RGB to HSV conversion is essential for color manipulation in graphics applications. The mathematical formulas normalize RGB values and calculate HSV components based on color relationships, providing a more intuitive color representation for artists and designers.
