开发者

Is there a way to desaturate an Image on a Button thats disabled?

开发者 https://www.devze.com 2023-01-27 05:29 出处:网络
Is there a way I can desaturate images in Buttons that are disabled? 开发者_如何学运维eg. ICommand.CanExecute = false? or do I need to use separate images + Style/TriggersI\'m using a special style fo

Is there a way I can desaturate images in Buttons that are disabled? 开发者_如何学运维eg. ICommand.CanExecute = false? or do I need to use separate images + Style/Triggers


I'm using a special style for this, which reduces the opacity of the image when the button gets disabled (yes, this also works if the button is bound to a command). Technically, this is not desaturation, but it looks similar and it might help you derive a solution on your own:

<Style x:Key="buttonImage">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
            <Setter Property="Image.Opacity" Value="0.25"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>


Here is a shader that desaturates (Desaturate.fx):

sampler2D  inputSampler : register(S0);
/// <summary>The strength of the effect.</summary>
/// <minValue>0</minValue>
/// <maxValue>1</maxValue>
/// <defaultValue>0</defaultValue>
float Strength : register(C0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 srcColor = tex2D(inputSampler, uv);
    float3 rgb = srcColor.rgb;
    float3 c = (1 - Strength)*rgb + Strength* dot(rgb, float3(0.30, 0.59, 0.11));
    return float4(c, srcColor.a);
}

Compile it and add the .ps file as a resource and then wrap it like this:

public class Desaturate : ShaderEffect
{
    public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty(
        "Input",
        typeof(Desaturate),
        0);

    public static readonly DependencyProperty StrengthProperty = DependencyProperty.Register(
        "Strength",
        typeof(double),
        typeof(Desaturate),
        new UIPropertyMetadata(((double)(0D)), PixelShaderConstantCallback(0)));

    public Desaturate()
    {
        PixelShader pixelShader = new PixelShader();
        pixelShader.UriSource = new Uri("/So.Wpf;component/Effects/Desaturate.ps", UriKind.Relative);
        this.PixelShader = pixelShader;

        this.UpdateShaderValue(InputProperty);
        this.UpdateShaderValue(StrengthProperty);
    }
    public Brush Input
    {
        get
        {
            return ((Brush)(this.GetValue(InputProperty)));
        }
        set
        {
            this.SetValue(InputProperty, value);
        }
    }
    /// <summary>The strength of the effect. 0 is unchanged and 1 is monochrome</summary>
    public double Strength
    {
        get
        {
            return ((double)(this.GetValue(StrengthProperty)));
        }
        set
        {
            this.SetValue(StrengthProperty, value);
        }
    }
}

Then you can use it in Xaml like this, the strength is bindable so np hooking it up to enabled via trigger:

<Grid>
    <Image Source="http://i.imgur.com/CN63KcN.jpg">
        <Image.Effect>
            <effects:Desaturate Strength="{Binding ElementName=SaturationSlider, Path=Value}"/>
        </Image.Effect>
    </Image>
    <Slider x:Name="SaturationSlider" Minimum="0" Maximum="1" VerticalAlignment="Bottom"/>
</Grid>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号