Search code examples
c#xamluwpwindows-10win2d

Win2D effect to make content darker only if it isn't already?


I'm working with some Win2D effects and I'm having a hard time finding a proper way to make my UI content dark enough so that the text above it is easy enough to read.

Right now this is a part of my code:

ArithmeticCompositeEffect composite = new ArithmeticCompositeEffect
{
    MultiplyAmount = 0,
    Source1Amount = 0.2f,
    Source2Amount = 0.8f,
    // The Source1 parameter will be assigned later on with the EffectFactory
    Source1 = new CompositionEffectSourceParameter(nameof(myBackground)),
    Source2 = new ColorSourceEffect { Color = Colors.Black }
};

So I'm mixing my content (Source1) with a uniform black color, and this effectively makes the whole thing darker. I have an issue though:

  • This makes dark content too dark, and light content not dark enough

I heard that it's possible to use a BlendEffect with the mode set to BlendEffectMode.Exclusion to solve this problem, but I don't know how to properly set that up. I've tried using this effect to combine my first effect with a uniform black color, but nothing changed in the result.

So my question is:

Which Win2D effect (not necessarily an exclusion blend if that's not the right choice here) can I apply to make sure that my content is always darker than a given threshold (so dark enough), without making content that's already dark practically black?

Thanks for your help!


Solution

  • I found out that it's possible to solve this problem using the LuminanceToAlphaEffect Win2D effect and overlaying the resulting map over the original effect (possibly with certain intensity in order to make the original image more or less dark depending on the situation).

    An example would be:

    LuminanceToAlphaEffect alphaEffect = new LuminanceToAlphaEffect 
    { 
        Source = new CompositionEffectSourceParameter(nameof(myBackground))
    };
    ArithmeticCompositeEffect composite = new ArithmeticCompositeEffect
    {
        MultiplyAmount = 0,
        Source1Amount = 1 - intensity, // Intensity is in the [0..1] range
        Source2Amount = intensity,
        // The Source1 parameter will be assigned later on with the EffectFactory
        Source1 = new CompositionEffectSourceParameter(nameof(myBackground)),
        Source2 = alphaEffect    
    };