UWP成分Api是否支持颜色替换?

我一直在寻找一个与Color Replacement相关的例子,这里是一个使用Photoshop的例子,例如,可以采用蓝色阴影并用红色阴影替换它:

之前: 在此处输入图像描述

后: 在此处输入图像描述

这是否可以使用最新版本的Composition Api中的Composition Effects?

我见过与Hue Rotations,Temperature和Tint有关的例子:

UWP Composition Effects: Hue Rotation

UWP Composition Effects: Temperature and Tint

但我想知道api是否能够使用Effects来切换图像中的颜色范围?

我有一个你可能喜欢的解决方案。 示例如下所示:

在此处输入图像描述

为了达到这个目的,我在链中使用了3个效果: PixelShaderEffectGaussianBlurEffectWin2d API的BlendEffect

XAML有CanvasAnimatedControl来绘制结果以及一些帮助器,比如source(我们想要替换的颜色)并替换颜色选择器和阈值滑块。

             

代码背后:

  private PixelShaderEffect _textureShader; private GaussianBlurEffect _blur; private BlendEffect _blend; private void AnimatedControl_OnCreateResources(CanvasAnimatedControl sender, CanvasCreateResourcesEventArgs args) { args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction()); } private async Task CreateResourcesAsync(CanvasAnimatedControl sender) { var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Shaders/TextureTest.bin")); var buffer = await FileIO.ReadBufferAsync(file); var sourceImage = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/image.jpg")); _textureShader = new PixelShaderEffect(buffer.ToArray()) { Source1 = sourceImage }; _blur = new GaussianBlurEffect { BlurAmount = 4, Source = _textureShader }; _blend = new BlendEffect { Foreground = _blur, Background = sourceImage, Mode = BlendEffectMode.Color }; } private void AnimatedControl_OnDraw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) { args.DrawingSession.DrawImage(_blend); } private void RangeBase_OnValueChanged(object sender, RangeBaseValueChangedEventArgs e) { _textureShader.Properties["threshold"] = (float)e.NewValue / 100; } private void ColorSpectrum_OnColorChanged(ColorSpectrum sender, ColorChangedEventArgs args) { _textureShader.Properties["sourceColor"] = new Vector3((float)args.NewColor.R / 255, (float)args.NewColor.G / 255, (float)args.NewColor.B / 255); } private void ColorSpectrum_OnColorChanged1(ColorSpectrum sender, ColorChangedEventArgs args) { _textureShader.Properties["replaceColor"] = new Vector3((float)args.NewColor.R / 255, (float)args.NewColor.G / 255, (float)args.NewColor.B / 255); } 

最令人兴奋的是像素着色器:

 #define D2D_INPUT_COUNT 1 #define D2D_INPUT0_SIMPLE #include "d2d1effecthelpers.hlsli" float3 sourceColor; float3 replaceColor; float threshold; D2D_PS_ENTRY(main) { float3 color = D2DGetInput(0).rgb; if (abs(color.r - sourceColor.r) < threshold && abs(color.g - sourceColor.g) < threshold && abs(color.b - sourceColor.b) < threshold) { float3 newColor = color - sourceColor + replaceColor; return float4(newColor.r, newColor.g, newColor.b, 1); } else { return float4(0, 0, 0, 0); } } 

要编译它,你应该在这里参观: https : //github.com/Microsoft/Win2D-Samples/tree/master/ExampleGallery/Shared/Shaders

有cmd文件可以编译你的hlsl着色器。 如果你需要帮助 - 评论它。 玩得开心!