HSY HSL HSI HSV color mix blending mode

I can’t write code, so I’m sorry. I learned from the mixer code of pigment. O(@ EyeOdin) and imitated the existing code of krita. I realized color mixing in krita, but the current problem is that the mixing ratio is fixed at 0.5. Can I use the opacity of the source color as the parameter of the mixing ratio (1-the opacity of the source color)? I tried to add sa as a parameter following AdditionSai, but the build reported an error.

template<class HSXType, class TReal>
inline void cfColorMix(TReal sr, TReal sg, TReal sb, TReal& dr, TReal& dg, TReal& db) {
    TReal hue; 
    TReal hue1 = getHue(sr, sg, sb);
    TReal sat1 = getSaturation<HSXType>(sr, sg, sb);
    TReal lum1 = getLightness<HSXType>(sr, sg, sb);
    TReal hue2 = getHue(dr, dg, db);
    TReal sat2 = getSaturation<HSXType>(dr, dg, db);
    TReal lum2 = getLightness<HSXType>(dr, dg, db);

    if (hue1<=hue2) {
        TReal cond1 = hue2 - hue1;
        TReal cond2 = hue1 + TReal(1.0) - hue2;
        if (cond1 <= cond2) {
            hue = hue1 + TReal(0.5) * cond1;
        }
        else {
            hue = hue1 - TReal(0.5) * cond2;
        }
    }
    else {
        TReal cond1 = hue1 - hue2;
        TReal cond2 = hue2 + TReal(1.0) - hue1;
        if (cond1 <= cond2) {
            hue = hue1 - TReal(0.5) * cond1;
        }
        else {
            hue = hue1 + TReal(0.5) * cond2;
        }
    }

    if (hue < TReal(0.0)) {
        hue = hue + TReal(1.0);
    }
    if (hue > TReal(1.0)) {
        hue = hue - TReal(1.0);
    }

    TReal sat = sat1 + TReal(0.5) * (sat2 - sat1);
    TReal lum = lum1 + TReal(0.5) * (lum2 - lum1);

    getRGB(dr, dg, db, hue);
    setSaturation<HSXType>(dr, dg, db, sat);
    setLightness<HSXType>(dr, dg, db, lum);
}

1 Like