WPF效果第二百零六篇之快速黑白灰效果

一大早就看到群友讨论怎么快速让界面黑白灰效果,这不突然想起来N年前咱简单通过ShaderEffects调节过饱和度、对比度、亮度;今天再次玩耍一下;来看看最终实现的效果:d5a88e6043cce80a4712b5ec70cb8e66.gif1、核心代码:

sampler2D implicitInput : register(s0);
float factor : register(c0);
float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(implicitInput, uv);
    float gray = color.r * 0.3 + color.g * 0.59 + color.b *0.11;  
    float4 result;    
    result.r = (color.r - gray) * factor + gray;
    result.g = (color.g - gray) * factor + gray;
    result.b = (color.b - gray) * factor + gray;
    result.a = color.a;
    return result;
}

2、在Shazzam中编译生成cs和ps文件:

b47bef0b1264798c6dd365f858d501d5.png3、来看看cs文件:

public class GrayscaleEffectEffect : ShaderEffect
    {
        public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(GrayscaleEffectEffect), 0);
        public static readonly DependencyProperty FactorProperty = DependencyProperty.Register("Factor", typeof(double), typeof(GrayscaleEffectEffect), new UIPropertyMetadata(((double)(0D)), PixelShaderConstantCallback(0)));
        public GrayscaleEffectEffect()
        {
            PixelShader pixelShader = new PixelShader();
            pixelShader.UriSource = new Uri("/MapDemo;component/ShaderEffects/GrayscaleEffectEffect.ps", UriKind.Relative);
            this.PixelShader = pixelShader;


            this.UpdateShaderValue(InputProperty);
            this.UpdateShaderValue(FactorProperty);
        }
        public Brush Input
        {
            get
            {
                return ((Brush)(this.GetValue(InputProperty)));
            }
            set
            {
                this.SetValue(InputProperty, value);
            }
        }
        public double Factor
        {
            get
            {
                return ((double)(this.GetValue(FactorProperty)));
            }
            set
            {
                this.SetValue(FactorProperty, value);
            }
        }
    }

4、前台调用就很简单

<Grid Grid.Row="1">
    <Grid.Background>
        <ImageBrush ImageSource="Images/Back.jpg" Stretch="Fill"/>
    </Grid.Background>
    <Grid.Effect>
        <ShaderEffects:GrayscaleEffectEffect Factor="{Binding ElementName=BlackTbtn,Path=IsChecked,Converter={StaticResource ToggleButtonIsCheckedToGrayscaleEffectEffectConverter}}"/>
    </Grid.Effect>
</Grid>

5、最后来个参考

http://bursjootech.blogspot.com/2008/06/grayscale-effect-pixel-shader-effect-in.html

最终简单的效果先这样吧2f2a9e67c90d25342d63cc45c86324d2.png;以后有时间的话,可以再去摸索一下更复杂的效果9d075d890e5f740afc7983eeb530998b.png;编程不息、Bug不止、无Bug、无生活7dbb35b934079f32f51656b84f4fc4b0.png;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值