gpt4 book ai didi

c# - 将 RGB 转换为 HSB 颜色

转载 作者:太空狗 更新时间:2023-10-29 18:17:03 26 4
gpt4 key购买 nike

我正在尝试将 HSB 颜色转换为 RGB。我这样做的方式是

System.Windows.Media.Color winColor = value;
System.Drawing.Color drawColor = System.Drawing.Color.FromArgb(winColor.R, winColor.G, winColor.B);
Hue = (byte)(drawColor.GetHue()*255);
Saturation = (byte)(drawColor.GetSaturation()*255);
Luminosity = (byte)(drawColor.GetBrightness()*255);

我发现当我有 FF0000 时,它会被转换为 H = 0, S = 255, L = 127 转换为 RGB FF0E0E。我认为亮度应该是120?还是我把整个 HSB 都弄错了?当我在 Photoshop 中查看颜色选择器时,色调是 0-360 度,饱和度,亮度是 0-100%。我的 HSB 值在 0 - 255 之间,我做错了吗?

最佳答案

也许您已经研究过这个 wikipedia article , 但要说清楚。

HSL 和 HSB(又名 HSV)之间存在差异。

所以你不能从颜色类中获取 (B)rightness 并将其用作 (L)uminosity。

要从 Color 类提供的值 GetHue()GetSaturation()GetBrightness() 返回到正常颜色,您应该给这个扩展方法一个机会。

/// <summary>
/// Creates a Color from alpha, hue, saturation and brightness.
/// </summary>
/// <param name="alpha">The alpha channel value.</param>
/// <param name="hue">The hue value.</param>
/// <param name="saturation">The saturation value.</param>
/// <param name="brightness">The brightness value.</param>
/// <returns>A Color with the given values.</returns>
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 = 0Saturation = 1Brightness = 1 为您提供了上面的代码Photoshop 中的红色和白色。当您仔细研究这些模式的差异时,您会发现这是绝对有道理的:

HSL 圆柱体

HSL cylinder
(来源:wikimedia.org)

  • 在两个模型中,通过使用红色作为起点和终点(零度和 360 度),色调表现相同。
    • 只要看色调,您就会发现红色。
  • 饱和度定义了颜色不透明的程度或白度部分的大小。
    • 因此,通过将其设置为一个,您表示您想要一种完全 Shiny 的红色。
  • 现在,光照定义了颜色中黑色和白色部分的比例。通过将它设置为零,您得到黑色,1 表示白色,0.5 表示完美加权。
    • 因此,通过将它设置为一个,您说您希望它尽可能亮,从而产生白色。

HSB气缸

HSB cylinder
(来源:wikimedia.org)

  • 在两个模型中,通过使用红色作为起点和终点(零度和 360 度),色调表现相同。
    • 只要看色调,您就会发现红色。
  • 饱和度定义了颜色不透明的程度或白度部分的大小。
    • 因此,通过将其设置为一个,您表示您想要一种完全 Shiny 的红色。
  • 亮度(或值)现在定义了颜色中的黑色部分(不是白色部分)有多少。
    • 因此,通过将它设置为一个,您说您希望它是全彩色的,从而导致完全 Shiny 的红色。

如您所见,Photoshop 和 .Net 框架(包括我的扩展功能)使用不同的着色模型。因此,您应该检查是否在某个地方找到了其他着色模型的实现、转换或其他可以为您提供所需结果的东西。

关于c# - 将 RGB 转换为 HSB 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4106363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com