给定RGB颜色范围,我们的任务是将RGB颜色转换为HSV颜色。
RGB颜色模型:
RGB颜色模型是一种加法颜色模型,其中以各种方式将红,绿和蓝光加在一起以重现各种颜色。该模型的名称来自三种附加原色(红色,绿色和蓝色)的缩写。
HSV颜色模型:
HSV –(色相,饱和度,值),也称为HSB(色相,饱和度,亮度),艺术家经常使用它,因为从色相和饱和度考虑颜色通常比在加性或饱和度方面考虑颜色更自然。减色分量。 HSV是RGB色彩空间的一种转换,其成分和色度相对于其衍生的RGB色彩空间。
例子 :
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 equal 0, 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 :
- v = cmax*100
下面是上述方法的实现:
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
输出 :
(30.0, 63.56589147286821, 50.588235294117645)