Please explain this color blending mode formula so I can replicate it in PHP/ImageMagick

后端 未结 4 445
梦如初夏
梦如初夏 2021-01-12 21:46

I\'ve been trying to use ImageMagick to replicate Photoshops Colour Blend Mode. I found the following formulas in an online guide but I don\'t know what they mean. Do I just

4条回答
  •  借酒劲吻你
    2021-01-12 22:23

    These color blending formulas are quite tricky if you need to incorporate also the alpha channel. I was not able to reproduce the blending of Photoshop, but Gimp works like this:

    Color mix_hsv(
        ColorMixMode::Enum mode, // blending mode
        Color cd,                // destination color (bottom pixel)
        Color cs)                // source color (top pixel)
    {
        // Modify the source color
        float dh, ds, dv; // destination hsv
        float sh, ss, sv; // source hsv
        cd.GetHsv(dh, ds, dv);
        cs.GetHsv(sh, ss, sv);
    
        switch (mode) {
            case HUE:        cs.InitFromHsv(sh, ds, dv); break;
            case SATURATION: cs.InitFromHsv(dh, ss, dv); break;
            case COLOR:      cs.InitFromHsv(sh, ss, dv); break;
            case LUMINOSITY: cs.InitFromHsv(dh, ds, sv); break;
        }
        cs.A = std::min(cd.A, cs.A);
    
        // Blend the modified source color onto the destination color
        unsigned char cd_A_orig = cd.A;
        cd = mix(NORMAL, cd, cs); // normal blending
        cd.A = cd_A_orig;
        return cd;
    }
    

    If you use premultiplied alpha, don't forget to correctly handle it in the above code. I was not able to find the code for blending in Gimp's source code, but the resulting images are very similar.

    Photoshop's color blending is clearly different, so if anyone finds a way to implement it, please let us all know :o)

    Miso

提交回复
热议问题