转换为RGB颜色HSB [英] Converting RGB to HSB Colors

查看:431
本文介绍了转换为RGB颜色HSB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个HSB颜色转换为RGB。我这样做的方式是

I am trying to convert a HSB Color to RGB. The way I am doing that is

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,我是不是做错了。

I find that when I have FF0000, it will be converted to H = 0, S = 255, L = 127 which converts to RGB FF0E0E. I think Luminosity should be 120? Or am I getting the whole HSB thing wrong? When I look at the color picker in Photoshop, Hue is 0-360 degrees, Saturation, Luminosity is 0-100%. I am having HSB values ranging from 0 - 255, am I doing it wrong?

推荐答案

也许你已经看这个维基百科文章,但要分清楚。

Maybe you already look into this wikipedia article, but to make it clear.

有HSL和HSB(又名HSV)之间的差异。

所以你不能从颜色类取(B)和正确性使用它像一个(L)uminosity。

So you can't take the (B)rightness from the color class and use it like a (L)uminosity.

要充分利用Color类提供的值回 GetHue() GetSaturation() GetBrightness()来一个正常的颜色,你应该给这个扩展方法的机会。

To get back from the Color class provided values GetHue(), GetSaturation() and GetBrightness() to a normal color you should give this extension method a chance.

/// <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);
    }
}

更新

所以,只是为了把事情说清楚。我的code以上和上述使用的HSB(又名HSV)颜色模型的颜色类中的三种方法,但Photoshop使用HSL颜色模式。

Update

So just to make things clear. My code above and the three methods within the Color class mentioned above are using the HSB (aka HSV) color model, but Photoshop uses the HSL color model.

在你的评论中写道,参数色相= 0 饱和度= 1 亮度= 1 给你的code红色和白色在Photoshop以上。当你把这些模式本的差异细看使得完全意义:

In your comment you wrote that the parameters Hue = 0, Saturation = 1 and Brightness = 1 give you with the code above a red color and white in Photoshop. When you take a closer look at the differences of these modes this makes absolutely sense:

的HSL缸

HSL缸


  • 在这两种模式的色调作为同样使用颜色红开始和结束点(零和360度)。

    • 将只看色相你有一个红色的颜色。


    • 因此,通过将其设置为一个你说你想要一个充满光泽的红色。


    • 因此,通过将其设置为一个你说你希望它尽可能明亮而导致的白色。

    的HSB缸

    HSB缸


    • 在这两种模式的色调作为同样使用颜色红开始和结束点(零和360度)。

      • 将只看色相你有一个红色的颜色。


      • 因此,通过将其设置为一个你说你想要一个充满光泽的红色。


      • 因此,通过将其设置为一个你说你希望它充满彩色导致了充满光泽的红色。

      如你所见,Photoshop和.net框架(包括我的扩展功能)使用不同颜色的机型。所以,你如果发现某处其他着色模式的实现,即提供您所需要的结果的转换或别的东西应该检查。

      As you can see, Photoshop and the .Net framework (including my extension function) are using different coloring models. So you should check if you find somewhere an implementation of the other coloring model, a transformation or something else that gives you the results you need.

      这篇关于转换为RGB颜色HSB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆