我在文档中搜索了一个HsbToRgb转换器,但没有发现任何包含"hsb"或"hsl"的内容,所以我猜它只是不存在.但是,确保是否有支持此转换的类?
更新
我最终选择了这个,这与0xA3略有不同.我还添加了一个,AhsbToArgb所以我可以转换为RGB并一次设置alpha通道.
AhsbToArgb - 允许alpha通道:
public static Color AhsbToArgb(byte a, double h, double s, double b)
{
var color = HsbToRgb(h, s, b);
return Color.FromArgb(a, color.R, color.G, color.B);
}
Run Code Online (Sandbox Code Playgroud)
HsbToRgb - 将色调饱和度 - 亮度转换为红 - 绿 - 蓝色:
public static Color HsbToRgb(double h, double s, double b)
{
if (s == 0)
return RawRgbToRgb(b, b, b);
else
{
var sector = h / 60;
var sectorNumber = (int)Math.Truncate(sector);
var sectorFraction = sector - sectorNumber;
var b1 = b * (1 - s);
var b2 = b * (1 - s * sectorFraction);
var b3 = b * (1 - s * (1 - sectorFraction));
switch (sectorNumber)
{
case 0:
return RawRgbToRgb(b, b3, b1);
case 1:
return RawRgbToRgb(b2, b, b1);
case 2:
return RawRgbToRgb(b1, b, b3);
case 3:
return RawRgbToRgb(b1, b2, b);
case 4:
return RawRgbToRgb(b3, b1, b);
case 5:
return RawRgbToRgb(b, b1, b2);
default:
throw new ArgumentException("Hue must be between 0 and 360");
}
}
}
Run Code Online (Sandbox Code Playgroud)
RawRgbToRgb - 将双精度转换为整数并返回颜色对象:
private static Color RawRgbToRgb(double rawR, double rawG, double rawB)
{
return Color.FromArgb(
(int)Math.Round(rawR * 255),
(int)Math.Round(rawG * 255),
(int)Math.Round(rawB * 255));
}
Run Code Online (Sandbox Code Playgroud)
Dir*_*mar 14
不,据我所知,不是.但算法并不是很复杂,您可以在Web上找到工作代码,例如Guillaume Leparmentier 在.NET上操作颜色的 CodeProject文章中的代码:
public static Color HSBtoRGB(double hue, double saturation, double brightness)
{
double r = 0;
double g = 0;
double b = 0;
if (saturation == 0)
{
r = g = b = brightness;
}
else
{
// The color wheel consists of 6 sectors.
// Figure out which sector you're in.
//
double sectorPos = hue / 60.0;
int sectorNumber = (int)(Math.Floor(sectorPos));
// get the fractional part of the sector
double fractionalSector = sectorPos - sectorNumber;
// calculate values for the three axes of the color.
double p = brightness * (1.0 - saturation);
double q = brightness * (1.0 - (saturation * fractionalSector));
double t = brightness * (1.0 - (saturation * (1 - fractionalSector)));
// assign the fractional colors to r, g, and b
// based on the sector the angle is in.
switch (sectorNumber)
{
case 0:
r = brightness;
g = t;
b = p;
break;
case 1:
r = q;
g = brightness;
b = p;
break;
case 2:
r = p;
g = brightness;
b = t;
break;
case 3:
r = p;
g = q;
b = brightness;
break;
case 4:
r = t;
g = p;
b = brightness;
break;
case 5:
r = brightness;
g = p;
b = q;
break;
}
}
return Color.FromArgb(
(int)(r * 255.0 + 0.5),
(int)(g * 255.0 + 0.5),
(int)(b * 255.0 + 0.5));
}
Run Code Online (Sandbox Code Playgroud)
不,在.NET中(直至并包括4.0)的Color类只自动地从RGB转换为HSB(通过GetHue,GetSaturation和GetBrightness方法).您必须使用自己的方法从HSB值转换为RGB值,或使用已编写的示例,如:
http://www.codeproject.com/KB/GDI-plus/HSBColorClass.aspx
| 归档时间: |
|
| 查看次数: |
593 次 |
| 最近记录: |