C#写一个hsv转rgb方法 要求输出Color
时间: 2023-06-26 19:10:20 浏览: 215
C# RGB与HSV,HSL,HTML颜色相互之前转换
4星 · 用户满意度95%
可以使用以下代码实现HSV到RGB的转换,并将结果封装在Color对象中:
```
using System.Drawing;
public static Color HsvToRgb(double h, double s, double v)
{
int hi = Convert.ToInt32(Math.Floor(h / 60)) % 6;
double f = h / 60 - Math.Floor(h / 60);
double p = v * (1 - s);
double q = v * (1 - f * s);
double t = v * (1 - (1 - f) * s);
double r, g, b;
switch (hi)
{
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default:
r = v;
g = p;
b = q;
break;
}
return Color.FromArgb((int)(r * 255), (int)(g * 255), (int)(b * 255));
}
```
使用方法如下:
```
Color color = HsvToRgb(hue, saturation, value);
```
阅读全文