Android NDK中的颜色叠加效果

14

我希望实现色调/颜色/饱和度颜色叠加效果。我看到了这些宏:

#define ColorBlend_Saturation(T,A,B) ColorBlend_Hls(T,A,B,HueA,LuminationA,SaturationB)

我正在尝试使用颜色#332244#557711在Adobe Photoshop中复制它以获得结果颜色-#431076。然而,在应用这些宏之后,我得到的颜色是#320C59问题1:我该如何复制Photoshop算法来处理色相、饱和度和颜色? 问题2:我该如何调整alpha通道?例如,对于我的颜色和光学== 50,这应该在Photoshop中为#3b195d。

不,我知道这些算法。我想要颜色/饱和度/色调算法。 - VKDev
http://www.cs.rit.edu/~ncs/color/t_convert.html 这个网站提供了RGB到HSV的转换以及反向转换。你可以使用RGB并将它们转换为HSV,或者相反地进行操作,甚至可以使用HSV重新设计这些算法。 - Rolice
Rolice,我知道这个。目前我已经实现了这些混合模式。但是,我该如何设置alpha通道呢?因为Adobe Photoshop的颜色与((uint8_t)(O * A + (1 - O) * B))混合不同。 - VKDev
啊,你需要完全相同的 Photoshop 算法吗? - Rolice
我想让它看起来像Photoshop。Photoshop或GIMP...因为结果颜色有很大的差异。 - VKDev
显示剩余2条评论
2个回答

0
这是不同的Photoshop混合模式方程:
    inline float Blend_Normal( float Base, float Overlay )
    {
        return Base;
    }
    inline float Blend_Lighten( float Base, float Overlay )
    {
        return ( Overlay > Base ) ? Overlay : Base;
    }
    inline float Blend_Darken( float Base, float Overlay )
    {
        return ( Overlay > Base ) ? Base : Overlay;
    }
    inline float Blend_Multiply( float Base, float Overlay )
    {
        return Base * Overlay;
    }
    inline float Blend_Average( float Base, float Overlay )
    {
        return ( Base + Overlay ) / 2.0f;
    }
    inline float Blend_Add( float Base, float Overlay )
    {
        return LMin( Base + Overlay, 1.0f );
    }
    inline float Blend_Subtract( float Base, float Overlay )
    {
        return LMax( Base + Overlay - 1.0f, 0.0f );
    }
    inline float Blend_Difference( float Base, float Overlay )
    {
        return fabs( Base - Overlay );
    }
    inline float Blend_Negation( float Base, float Overlay )
    {
        return 1.0f - fabs( 1.0f - Base - Overlay );
    }
    inline float Blend_Screen( float Base, float Overlay )
    {
        return 1.0f - ( 1.0f - Base ) * ( 1.0f - Overlay );
    }
    inline float Blend_Exclusion( float Base, float Overlay )
    {
        return Base + Overlay - 2 * Base * Overlay;
    }
    inline float Blend_Overlay( float Base, float Overlay )
    {
        return ( Overlay < 0.5f ) ? ( 2.0f * Base * Overlay ) : ( 2.0f * Base - 1.0f ) * ( 1.0f - Overlay );
    }
    inline float Blend_SoftLight( float Base, float Overlay )
    {
        return ( Overlay < 0.5f ) ? ( Base + 0.5f ) * Overlay : ( Base - 0.5f ) * ( 1.0f - Overlay );
    }
    inline float Blend_HardLight( float Base, float Overlay )
    {
        return Blend_Overlay( Overlay, Base );
    }
    inline float Blend_ColorDodge( float Base, float Overlay )
    {
        return ( Overlay > 1.0f - Math::EPSILON ) ? Overlay : LMin( 1.0f, Base / ( 1.0f - Overlay ) );
    }
    inline float Blend_ColorBurn( float Base, float Overlay )
    {
        return ( Overlay < Math::EPSILON ) ? Overlay : LMax( 0.0f, 1.0f - ( 1.0f - Base ) / Overlay );
    }
    inline float Blend_LinearDodge( float Base, float Overlay )
    {
        return Blend_Add( Base, Overlay );
    }
    inline float Blend_LinearBurn( float Base, float Overlay )
    {
        return Blend_Subtract( Base, Overlay );
    }
    inline float Blend_LinearLight( float Base, float Overlay )
    {
        return ( Overlay < 0.5f ) ? Blend_LinearBurn( Base, 2 * Overlay ) : Blend_LinearDodge( Base, ( 2 * ( Overlay - 0.5f ) ) );
    }
    inline float Blend_VividLight( float Base, float Overlay )
    {
        return ( Overlay < 0.5f ) ? Blend_ColorBurn( Base, 2 * Overlay ) : Blend_ColorDodge( Base, ( 2 * ( Overlay - 0.5f ) ) );
    }
    inline float Blend_PinLight( float Base, float Overlay )
    {
        return ( Overlay < 0.5f ) ? Blend_Darken( Base, 2 * Overlay ) : Blend_Lighten( Base, ( 2 * ( Overlay - 0.5f ) ) );
    }
    inline float Blend_HardMix( float Base, float Overlay )
    {
        return ( Blend_VividLight( Base, Overlay ) < 0.5f ) ? 0.0f : 1.0f;
    }
    inline float Blend_Reflect( float Base, float Overlay )
    {
        return ( Overlay  > 1.0f - Math::EPSILON ) ? Overlay : LMin( 1.0f, Base * Base / ( 1.0f - Overlay ) );
    }
    inline float Blend_Glow( float Base, float Overlay )
    {
        return Blend_Reflect( Overlay, Base );
    }
    inline float Blend_Phoenix( float Base, float Overlay )
    {
        return LMin( Base, Overlay ) - LMax( Base, Overlay ) + 1.0f;
    }

来自Linderdaum Engine SDK


不不不!我想要颜色/色调/饱和度与透明度混合。你可以在Photoshop/GIMP/Pixelmator中重现这个效果:创建一个基础图层并填充它,创建一个新图层并也填充它。下一步-将图层叠加模式从正常更改为颜色,并进行光学更改。颜色图层可以使用HSV/HSL实现。但我不知道如何进行光学更改。 - monyag

0

问题1:

Photoshop的色相、饱和度、颜色和亮度混合模式基于一个具有尺寸的颜色空间,文章HSL和HSV称其为色相、色度和亮度。请注意,该空间与HSL和HSV都不同,只有色相维度在三者之间共享;有关详细信息,请参见该文章。

色相混合模式保留底层的亮度和色度,同时采用顶层的色相。

饱和度混合模式保留底层的亮度和色相,同时采用顶层的色度。

颜色混合模式保留底层的亮度,同时采用顶层的色相和色度。

来自http://en.wikipedia.org/wiki/Blend_modes

经过3个多小时的实验,我成功地将HSV -> RGB转换器升级为工作饱和度混合器。其他混合模式应该是类似的。

以下是代码:

#include <cmath>
#include <iostream>

using namespace std;

struct HSVColor
{
    float H,S,V;
};

struct RGBColor
{
    float R,G,B;
    RGBColor() = default;
    RGBColor(int r,int g, int b):
        R(r/255.0),
        G(g/255.0),
        B(b/255.0)
    {
    }
};

HSVColor RGBToHSV(const RGBColor& RGB)
{
    float Max;
    float Min;
    float Chroma;
    HSVColor HSV;

    Min = min(min(RGB.R, RGB.G), RGB.B);
    Max = max(max(RGB.R, RGB.G), RGB.B);
    Chroma = Max - Min;

    //If Chroma is 0, then S is 0 by definition, and H is undefined but 0 by convention.
    if(Chroma != 0)
    {
        if(RGB.R == Max)
        {
            HSV.H = (RGB.G - RGB.B) / Chroma;

            if(HSV.H < 0.0)
            {
                HSV.H += 6.0;
            }
        }
        else if(RGB.G == Max)
        {
            HSV.H = ((RGB.B - RGB.R) / Chroma) + 2.0;
        }
        else //RGB.B == Max
        {
            HSV.H = ((RGB.R - RGB.G) / Chroma) + 4.0;
        }

        HSV.H *= 60.0;
        HSV.S = Chroma / Max;
    }

    HSV.V = Max;

    return HSV;
}

RGBColor Saturate(const HSVColor& HSV,const HSVColor& overlay)
{
    float os = overlay.S;
    float ov = overlay.V;

    float Min;
    float Chroma;
    float Hdash;
    float X;
     RGBColor RGB{0,0,0};

    Chroma = os * ov; // Orginal was HSV.S * HSV.V
    Hdash = HSV.H / 60.0;
    X = Chroma * (1.0 - abs(fmod(Hdash , 2.0) - 1.0));

    if(Hdash < 1.0)
    {
        RGB.R = Chroma;
        RGB.G = X;
    }
    else if(Hdash < 2.0)
    {
        RGB.R = X;
        RGB.G = Chroma;
    }
    else if(Hdash < 3.0)
    {
        RGB.G = Chroma;
        RGB.B = X;
    }
    else if(Hdash < 4.0)
    {
        RGB.G= X;
        RGB.B = Chroma;
    }
    else if(Hdash < 5.0)
    {
        RGB.R = X;
        RGB.B = Chroma;
    }
    else if(Hdash <= 6.0)
    {
        RGB.R = Chroma;
        RGB.B = X;
    }

    Min = ov - Chroma; // Orginal was HSV.V - Chroma

    RGB.R += Min;
    RGB.G += Min;
    RGB.B += Min;

    return RGB;
}

int main(){
    RGBColor base{51, 34, 68};
    RGBColor overly{85, 119, 17};

    RGBColor r = Saturate(RGBToHSV(base),RGBToHSV(overly));

    cout << int(r.R*255) << endl;
    cout << int(r.G*255) << endl;
    cout << int(r.B*255) << endl;
}

原始的HSV <-> RGB转换器代码在这里:http://wiki.beyondunreal.com/HSV-RGB_Conversion

问题2.

有了饱和度,这实际上很容易,在饱和度混合后,在RGB颜色空间中使用普通的alpha混合。

RGBColor base;
RGBColor overly;    
RGBColor saturated = Saturate(base,overly);
RGBColor result = AlphaBlend(base,saturated,overly.alpha);

注意:这可能无法与其他混合模式配合使用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接