也许你已经查看了这个维基百科文章,但为了让它更清晰明了。
HSL和HSB(又称HSV)之间存在差异。
因此,您不能从颜色类中获取(B)亮度并像使用(L)亮度一样使用它。
要从提供的Color类值GetHue()
、GetSaturation()
和GetBrightness()
返回到普通颜色,您应该尝试使用这个扩展方法。
public static Color FromAhsb(int alpha, float hue, float saturation, float brightness)
{
if (0 > alpha
|| 255 < alpha)
{
throw new ArgumentOutOfRangeException(
"alpha",
alpha,
"Value must be within a range of 0 - 255.");
}
if (0f > hue
|| 360f < hue)
{
throw new ArgumentOutOfRangeException(
"hue",
hue,
"Value must be within a range of 0 - 360.");
}
if (0f > saturation
|| 1f < saturation)
{
throw new ArgumentOutOfRangeException(
"saturation",
saturation,
"Value must be within a range of 0 - 1.");
}
if (0f > brightness
|| 1f < brightness)
{
throw new ArgumentOutOfRangeException(
"brightness",
brightness,
"Value must be within a range of 0 - 1.");
}
if (0 == saturation)
{
return Color.FromArgb(
alpha,
Convert.ToInt32(brightness * 255),
Convert.ToInt32(brightness * 255),
Convert.ToInt32(brightness * 255));
}
float fMax, fMid, fMin;
int iSextant, iMax, iMid, iMin;
if (0.5 < brightness)
{
fMax = brightness - (brightness * saturation) + saturation;
fMin = brightness + (brightness * saturation) - saturation;
}
else
{
fMax = brightness + (brightness * saturation);
fMin = brightness - (brightness * saturation);
}
iSextant = (int)Math.Floor(hue / 60f);
if (300f <= hue)
{
hue -= 360f;
}
hue /= 60f;
hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
if (0 == iSextant % 2)
{
fMid = (hue * (fMax - fMin)) + fMin;
}
else
{
fMid = fMin - (hue * (fMax - fMin));
}
iMax = Convert.ToInt32(fMax * 255);
iMid = Convert.ToInt32(fMid * 255);
iMin = Convert.ToInt32(fMin * 255);
switch (iSextant)
{
case 1:
return Color.FromArgb(alpha, iMid, iMax, iMin);
case 2:
return Color.FromArgb(alpha, iMin, iMax, iMid);
case 3:
return Color.FromArgb(alpha, iMin, iMid, iMax);
case 4:
return Color.FromArgb(alpha, iMid, iMin, iMax);
case 5:
return Color.FromArgb(alpha, iMax, iMin, iMid);
default:
return Color.FromArgb(alpha, iMax, iMid, iMin);
}
}
更新
为了澄清事情,我的代码和 Color 类中提到的三种方法都使用 HSB(也称为 HSV)颜色模型,但 Photoshop 使用的是 HSL 颜色模型。
在您的评论中,您写道参数 Hue = 0
、Saturation = 1
和 Brightness = 1
在上面的代码中给出了红色,在 Photoshop 中则是白色。当您仔细观察这些模式的差异时,这完全合理:
HSL 圆柱体
![HSL cylinder](https://istack.dev59.com/AerCk.webp)
(来源:wikimedia.org)
- 在两种模型中,色调都以红色为起点和终点(零度和 360 度)。
- 饱和度定义了颜色的不透明程度或白色比例的多少。
- 光照现在定义了颜色中黑色和白色部分的多少。将其设置为零会得到黑色,设置为一表示白色,0.5 表示权重完美。
- 因此,将其设置为 1 表示您希望它尽可能明亮,这将导致白色。
HSB 圆柱体
![HSB cylinder](https://istack.dev59.com/JwKou.webp)
(来源:wikimedia.org)
- 在两种模型中,色调都以红色为起点和终点(零度和 360 度)。
- 饱和度定义了颜色的不透明程度或白色比例的多少。
- 亮度(或值)现在定义了颜色中黑色部分的多少(而不是白色部分)。
- 因此,将其设置为 1 表示您希望它完全有色,从而得到完美闪亮的红色。
正如您所看到的,Photoshop 和 .Net Framework(包括我的扩展函数)使用不同的着色模型。因此,您应该检查是否可以找到其他着色模型的实现、变换或其他内容,以获得所需的结果。