【问题标题】:C# Color Blend and calculating opacity / alpha like PhotoshopC# 颜色混合和计算不透明度/alpha 像 Photoshop
【发布时间】:2015-10-28 20:57:37
【问题描述】:

我正在尝试将图像处理成非常具体的外观。我在 Photoshop 中找到了一个教程,并且一直在慢慢地、一步一步地重新创建 Photoshop 效果。最新的是一种颜色混合模式,我能够开始工作,但他们给它添加了一个 Alpha,这让我很难过。

Photoshop 通过创建滤镜、设置颜色、选择“混合”然后应用 Alpha 来实现此目的。 alpha 适用于滤镜而不是基础图像。

我正在做的是采用两种颜色,基本图像和叠加颜色进行混合,并将它们转换为 HSL。我通过从覆盖层中获取色相/饱和度和从基础中获取亮度并创建新的 HSL 颜色来计算混合。然后我将其转换为 RGB 并将该像素写入位图中的原始基色。

我不知道如何应用 alpha。似乎我应该采用基色和混合产品并计算它们之间的 alpha。我不确定这是否正确,或者在哪里可以找到颜色之间的正确计算。

我认为这个问题不需要代码,但如果你需要,我可以发布一些。

谢谢

【问题讨论】:

标签: c# image-processing photoshop alpha blending


【解决方案1】:

我刚刚这样解决了这个问题(background.png 和 colorWithAlpha.png 具有相同的尺寸):

Bitmap bg = new Bitmap("background.png");
Bitmap overlay = new Bitmap("colorWithAlpha.png");
float m = 0;
Color c;
for (int i = 0; i < bg.Width; i++)
{
    for (int j = 0; j < bg.Height; j++)
    {
        m = overlay.GetPixel(i, j).A / 255f;
        c = HSVtoRGB(overlay.GetPixel(i,j).GetHue(), overlay.GetPixel(i,j).GetSaturation(), bg.GetPixel(i, j).GetBrightness());
        c = Color.FromArgb((int)(m * c.R + (1 - m) * bg.GetPixel(i, j).R), (int)(m * c.G + (1 - m) * bg.GetPixel(i, j).G), (int)(m * c.B + (1 - m) * bg.GetPixel(i, j).B));
        bg.SetPixel(i, j, c);
    }
}

public static Color HSVtoRGB(float hue, float saturation, float value)
{
    // HSV contains values scaled as in the color wheel:
    // that is, all from 0 to 255.

    // for ( this code to work, HSV.Hue needs
    // to be scaled from 0 to 360 (it//s the angle of the selected
    // point within the circle). HSV.Saturation and HSV.value must be
    // scaled to be between 0 and 1.

    double h;
    double s;
    double v;

    double r = 0;
    double g = 0;
    double b = 0;

    // Scale Hue to be between 0 and 360. Saturation
    // and value scale to be between 0 and 1.
    h = ((double)hue / 255 * 360) % 360;
    s = (double)saturation / 255;
    v = (double)value / 255;

    if (s == 0)
    {
        // If s is 0, all colors are the same.
        // This is some flavor of gray.
        r = v;
        g = v;
        b = v;
    }
    else
    {
        double p;
        double q;
        double t;

        double fractionalSector;
        int sectorNumber;
        double sectorPos;

        // The color wheel consists of 6 sectors.
        // Figure out which sector you//re in.
        sectorPos = h / 60;
        sectorNumber = (int)(Math.Floor(sectorPos));

        // get the fractional part of the sector.
        // That is, how many degrees into the sector
        // are you?
        fractionalSector = sectorPos - sectorNumber;

        // Calculate values for the three axes
        // of the color.
        p = v * (1 - s);
        q = v * (1 - (s * fractionalSector));
        t = v * (1 - (s * (1 - fractionalSector)));

        // Assign the fractional colors to r, g, and b
        // based on the sector the angle is in.
        switch (sectorNumber)
        {
            case 0:
                r = v;
                g = t;
                b = p;
                break;

            case 1:
                r = q;
                g = v;
                b = p;
                break;

            case 2:
                r = p;
                g = v;
                b = t;
                break;

            case 3:
                r = p;
                g = q;
                b = v;
                break;

            case 4:
                r = t;
                g = p;
                b = v;
                break;

            case 5:
                r = v;
                g = p;
                b = q;
                break;
        }
    }
    // return an RGB structure, with values scaled
    // to be between 0 and 255.
    return Color.FromArgb((int)(r * 255), (int)(g * 255), (int)(b * 255));
}

函数 HSVtoRGB 取自 https://bytes.com/topic/c-sharp/answers/236124-how-would-i-change-hue-bitmap,稍作调整,因为原始函数期望所有输入都是 0-255 之间的值,因此必须删除缩放。

【讨论】:

  • 小提示,因为 GetPixel 实在是太慢了,也许把它保存在一个变量中,或者更好的是,使用 lockbits
猜你喜欢
  • 2017-09-23
  • 2012-05-10
  • 2011-04-09
  • 2021-02-18
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 2021-05-24
  • 2015-02-03
相关资源
最近更新 更多