Program to Change RGB color model to HSV color model
Last Updated :
20 Feb, 2023
Given RGB color range, our task is to convert RGB color to HSV color.
RGB Color Model :
The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green, and blue.
HSV Color Model :
HSV - (hue, saturation, value), also known as HSB (hue, saturation, brightness), is often used by artists because it is often more natural to think about a color in terms of hue and saturation than in terms of additive or subtractive color components. HSV is a transformation of an RGB colorspace, and its components and colorimetry are relative to the RGB colorspace from which it was derived.
Examples :
Input : r, g, b = 45, 215, 0
Output :
h, s, v = 107.44186046511628, 100.0, 84.31372549019608
Input : r, g, v = 31, 52, 29
Output :
h, s, v = 114.78260869565217, 44.230769230769226, 20.392156862745097
Approach :
- Divide r, g, b by 255
- Compute cmax, cmin, difference
- Hue calculation :
- if cmax and cmin are equal, then h = 0
- if cmax equal r then compute h = (60 * ((g - b) / diff) + 360) % 360
- if cmax equal g then compute h = (60 * ((b - r) / diff) + 120) % 360
- if cmax equal b then compute h = (60 * ((r - g) / diff) + 240) % 360
- Saturation computation :
- if cmax = 0, then s = 0
- if cmax does not equal 0 then compute s = (diff/cmax)*100
- Value computation :
Below is the implementation of above approach :
C++
// C++ program change RGB Color
// Model to HSV Color Model
#include <bits/stdc++.h>
using namespace std;
void rgb_to_hsv(double r, double g, double b)
{
// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1
r = r / 255.0;
g = g / 255.0;
b = b / 255.0;
// h, s, v = hue, saturation, value
double cmax = max(r, max(g, b)); // maximum of r, g, b
double cmin = min(r, min(g, b)); // minimum of r, g, b
double diff = cmax - cmin; // diff of cmax and cmin.
double h = -1, s = -1;
// if cmax and cmax are equal then h = 0
if (cmax == cmin)
h = 0;
// if cmax equal r then compute h
else if (cmax == r)
h = fmod(60 * ((g - b) / diff) + 360, 360);
// if cmax equal g then compute h
else if (cmax == g)
h = fmod(60 * ((b - r) / diff) + 120, 360);
// if cmax equal b then compute h
else if (cmax == b)
h = fmod(60 * ((r - g) / diff) + 240, 360);
// if cmax equal zero
if (cmax == 0)
s = 0;
else
s = (diff / cmax) * 100;
// compute v
double v = cmax * 100;
cout << "(" << h << ", " << s << ", " << v << ")"
<< endl;
}
// Driver Code
int main()
{
// rgb_to_hsv(45, 215, 0);
// rgb_to_hsv(31, 52, 29);
rgb_to_hsv(129, 88, 47);
}
// This code is contributed by phasing17
Java
// Java program change RGB Color
// Model to HSV Color Model
class GFG
{
static void rgb_to_hsv(double r, double g, double b)
{
// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1
r = r / 255.0;
g = g / 255.0;
b = b / 255.0;
// h, s, v = hue, saturation, value
double cmax = Math.max(r, Math.max(g, b)); // maximum of r, g, b
double cmin = Math.min(r, Math.min(g, b)); // minimum of r, g, b
double diff = cmax - cmin; // diff of cmax and cmin.
double h = -1, s = -1;
// if cmax and cmax are equal then h = 0
if (cmax == cmin)
h = 0;
// if cmax equal r then compute h
else if (cmax == r)
h = (60 * ((g - b) / diff) + 360) % 360;
// if cmax equal g then compute h
else if (cmax == g)
h = (60 * ((b - r) / diff) + 120) % 360;
// if cmax equal b then compute h
else if (cmax == b)
h = (60 * ((r - g) / diff) + 240) % 360;
// if cmax equal zero
if (cmax == 0)
s = 0;
else
s = (diff / cmax) * 100;
// compute v
double v = cmax * 100;
System.out.println("(" + h + " " + s + " " + v + ")");
}
// Driver Code
public static void main(String[] args)
{
// rgb_to_hsv(45, 215, 0);
// rgb_to_hsv(31, 52, 29);
rgb_to_hsv(129, 88, 47);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program change RGB Color
# Model to HSV Color Model
def rgb_to_hsv(r, g, b):
# R, G, B values are divided by 255
# to change the range from 0..255 to 0..1:
r, g, b = r / 255.0, g / 255.0, b / 255.0
# h, s, v = hue, saturation, value
cmax = max(r, g, b) # maximum of r, g, b
cmin = min(r, g, b) # minimum of r, g, b
diff = cmax-cmin # diff of cmax and cmin.
# if cmax and cmax are equal then h = 0
if cmax == cmin:
h = 0
# if cmax equal r then compute h
elif cmax == r:
h = (60 * ((g - b) / diff) + 360) % 360
# if cmax equal g then compute h
elif cmax == g:
h = (60 * ((b - r) / diff) + 120) % 360
# if cmax equal b then compute h
elif cmax == b:
h = (60 * ((r - g) / diff) + 240) % 360
# if cmax equal zero
if cmax == 0:
s = 0
else:
s = (diff / cmax) * 100
# compute v
v = cmax * 100
return h, s, v
''' Driver Code '''
# print(rgb_to_hsv(45, 215, 0))
# print(rgb_to_hsv(31, 52, 29))
print(rgb_to_hsv(129, 88, 47))
C#
// C# program change RGB Color
// Model to HSV Color Model
using System;
class GFG
{
static void rgb_to_hsv(double r, double g, double b)
{
// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1
r = r / 255.0;
g = g / 255.0;
b = b / 255.0;
// h, s, v = hue, saturation, value
double cmax = Math.Max(r, Math.Max(g, b)); // maximum of r, g, b
double cmin = Math.Min(r, Math.Min(g, b)); // minimum of r, g, b
double diff = cmax - cmin; // diff of cmax and cmin.
double h = -1, s = -1;
// if cmax and cmax are equal then h = 0
if (cmax == cmin)
h = 0;
// if cmax equal r then compute h
else if (cmax == r)
h = (60 * ((g - b) / diff) + 360) % 360;
// if cmax equal g then compute h
else if (cmax == g)
h = (60 * ((b - r) / diff) + 120) % 360;
// if cmax equal b then compute h
else if (cmax == b)
h = (60 * ((r - g) / diff) + 240) % 360;
// if cmax equal zero
if (cmax == 0)
s = 0;
else
s = (diff / cmax) * 100;
// compute v
double v = cmax * 100;
Console.WriteLine("(" + h + " " + s + " " + v + ")");
}
// Driver Code
public static void Main(String[] args)
{
// rgb_to_hsv(45, 215, 0);
// rgb_to_hsv(31, 52, 29);
rgb_to_hsv(129, 88, 47);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// javascript program change RGB Color
// Model to HSV Color Model
function rgb_to_hsv(r , g , b) {
// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1
r = r / 255.0;
g = g / 255.0;
b = b / 255.0;
// h, s, v = hue, saturation, value
var cmax = Math.max(r, Math.max(g, b)); // maximum of r, g, b
var cmin = Math.min(r, Math.min(g, b)); // minimum of r, g, b
var diff = cmax - cmin; // diff of cmax and cmin.
var h = -1, s = -1;
// if cmax and cmax are equal then h = 0
if (cmax == cmin)
h = 0;
// if cmax equal r then compute h
else if (cmax == r)
h = (60 * ((g - b) / diff) + 360) % 360;
// if cmax equal g then compute h
else if (cmax == g)
h = (60 * ((b - r) / diff) + 120) % 360;
// if cmax equal b then compute h
else if (cmax == b)
h = (60 * ((r - g) / diff) + 240) % 360;
// if cmax equal zero
if (cmax == 0)
s = 0;
else
s = (diff / cmax) * 100;
// compute v
var v = cmax * 100;
document.write("(" + h.toFixed(1) + ", " + s + ", " + v + ")");
}
// Driver Code
// rgb_to_hsv(45, 215, 0);
// rgb_to_hsv(31, 52, 29);
rgb_to_hsv(129, 88, 47);
// This code is contributed by todaysgaurav
</script>
Output(30, 63.5659, 50.5882)
Time Complexity: O(1)
Auxiliary Space: O(1), As constant extra space is used.
Similar Reads
Change RGB image color with HSV values with Python OpenCV An open-source library of Python, OpenCV is mainly used for image and video processing. On the other hand, there is also open source Python Library NumPy, short for Numerical Python, which works with large arrays. In this article, we will show you How we can Change RGB image color with HSV values wi
3 min read
Convert the given RGB color code to Hex color code Given three colors, such as R, G, and B, convert these RGB color to a hex color code. If the conversion is not possible, print -1. Examples: Input: R = 0, G = 0, B = 0 Output: #000000 Input: R = 255, G = 255, B = 256 Output: -1 Explanation: A 256 color code is not possible as only the 0-255 range is
9 min read
Matplotlib.colors.rgb_to_hsv() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.rgb_to_hsv() The matplotlib.colors.rgb_to_hsv() function belongs to th
2 min read
Matplotlib.colors.hsv_to_rgb() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.colors.hsv_to_rgb() The matplotlib.colors.hsv_to_rgb() function is used to co
2 min read
Convert RGB to Color Names in Python Converting RGB values to color names is a common task in various applications, from web development to image processing. While RGB values are precise, human-readable color names can make your code and output more understandable. This article will guide you through the process of converting RGB value
4 min read