在C#中的图像色调修改 [英] Image hue modification in C#

查看:458
本文介绍了在C#中的图像色调修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形象,我想修改特定的图像,以特定值的色调。
我知道RGB到HSL和HSL到RGB转换的数学公式,但我不能够实现这个东西到C#

I have an image and i want to modify the hue of that particular image to specific value. I know rgb to hsl and hsl to rgb conversion mathematical formula but I am not being able to implement this thing into c#.

以下是伪

for(x=0;x<image_width;x++)
{
  for(y=0;y<image_height;y++)
  {
    Color oldColor=GetColorFromPixel(x,y);
    Color newColor=ModifyHue(oldColor);
    SetColorPixel(x,y,newColor);    
  }
}



感谢

Thanks

推荐答案

由于这样的事实,即的 颜色 结构已经拥有了的 GetHue() ,的 GetSaturation() 和的 GetBrightness() 它会是不错的构造也从这些值的颜色。所以,我发现下面的代码某处网前一段时间(目前无法再次找到它,但它是从微软的家伙博客,他也有一个测试,通过所有的 KnownColor )。

Due to the fact, that the Color structure already has a GetHue(), GetSaturation() and GetBrightness() it would be nice to also construct a color from these values. So i found the following code somewhere on the net a while ago (currently can't find it again, but it was from a Microsoft guy blog and he also had a test that steps through all KnownColor).

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

通过此功能,您能在HSB内工作(或HSV)色彩呈现的不可以的HSL演示文稿中。有关他们之间的分歧进一步信息看看这个维基百科文章

With this function you are able to work within the HSB (or HSV) color presentation not within the HSL presentation. For further informations about their differences take a look at this wikipedia article.

这篇关于在C#中的图像色调修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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