highp vec3 blend_normal(highp vec4 base, highp vec4 stroke, highp float alpha_tot)
{
    return mix(
        base.rgb,
        stroke.rgb,
        stroke.a/alpha_tot
    );
}

highp vec3 blend_multiply(highp vec4 base, highp vec4 stroke, highp float alpha_tot)
{
    return mix(
        stroke.rgb, 
        mix(
            base.rgb, 
            base.rgb*stroke.rgb, 
            stroke.a/alpha_tot
        ), 
        base.a/alpha_tot
    );
}

highp vec3 blend_screen(highp vec4 base, highp vec4 stroke, highp float alpha_tot)
{
    return mix(
        stroke.rgb, 
        mix(
            base.rgb, 
            1.0-(1.0-base.rgb)*(1.0-stroke.rgb), 
            stroke.a/alpha_tot
        ), 
        base.a/alpha_tot
    );
}

highp vec3 blend_colorDodge(highp vec4 base, highp vec4 stroke, highp float alpha_tot)
{
    return mix(
        stroke.rgb, 
        mix(
            base.rgb, 
            base.rgb/(1.0-stroke.rgb), 
            stroke.a/alpha_tot
        ), 
        base.a/alpha_tot
    );
}

highp vec3 blend_overlay(highp vec4 base, highp vec4 stroke, highp float alpha_tot)
{
    return mix(
        stroke.rgb, 
        mix(
            base.rgb, 
            mix(
                2.0*base.rgb*stroke.rgb, 
                1.0-2.0*(1.0-base.rgb)*(1.0-stroke.rgb), 
                floor(base.rgb*2.0)
            ), 
            stroke.a/alpha_tot
        ), 
        base.a/alpha_tot
    );
}

highp vec4 blend(highp vec4 base, highp vec4 stroke, int mode)
{
    mediump float contribution = (1.0 - base.a) * stroke.a;
    mediump float alpha_tot = base.a + contribution;
    if      (mode == 0) return vec4(blend_normal(base, stroke, alpha_tot), alpha_tot);
    else if (mode == 1) return vec4(blend_multiply(base, stroke, alpha_tot), alpha_tot);
    else if (mode == 2) return vec4(blend_screen(base, stroke, alpha_tot), alpha_tot);
    else if (mode == 3) return vec4(blend_colorDodge(base, stroke, alpha_tot), alpha_tot);
    else if (mode == 4) return vec4(blend_overlay(base, stroke, alpha_tot), alpha_tot);
    else return vec4(1.0, 0.0, 0.0, 1.0);
}
