UWP Composition Api 是否支持颜色替换?
Does UWP Composition Api support color replacement?
我一直在寻找与颜色替换相关的示例,这里是一个使用 Photoshop 的示例,例如,可以将蓝色阴影替换为红色阴影:
之前:
之后:
是否可以在最新版本的 Composition Api 中使用 Composition Effects?
我看过与色调旋转、温度和色调相关的示例:
https://xamlbrewer.wordpress.com/2016/04/08/uwp-composition-effects-hue-rotation/
https://xamlbrewer.wordpress.com/2016/04/19/uwp-composition-effects-temperature-and-tint/
但我想知道 api 是否能够使用效果器切换图像中的颜色范围??
我有一个您可能会喜欢的解决方案。示例如下所示:
为了实现这一点,我在一个链中使用了 3 个效果:PixelShaderEffect
、GaussianBlurEffect
和 BlendEffect
来自 Win2d API .
XAML 有 CanvasAnimatedControl
来绘制结果加上一些助手,例如源(我们要替换的颜色)并替换颜色选择器和阈值滑块。
<Grid>
<xaml:CanvasAnimatedControl x:Name="AnimatedControl"
CreateResources="AnimatedControl_OnCreateResources"
Draw="AnimatedControl_OnDraw"/>
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Bottom">
<TextBlock Text="Source Color" FontSize="32" Foreground="White" TextAlignment="Center"/>
<ColorSpectrum Width="256" Height="256" ColorChanged="ColorSpectrum_OnColorChanged"/>
</StackPanel>
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Bottom">
<TextBlock Text="Replace Color" FontSize="32" Foreground="White" TextAlignment="Center"/>
<ColorSpectrum Width="256" Height="256" ColorChanged="ColorSpectrum_OnColorChanged1"/>
</StackPanel>
<Slider Width="512" ValueChanged="RangeBase_OnValueChanged" VerticalAlignment="Center"/>
</Grid>
后面的代码:
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
有编译 hlsl 着色器的 cmd 文件。如果您需要帮助 - 请发表评论。玩得开心!
我一直在寻找与颜色替换相关的示例,这里是一个使用 Photoshop 的示例,例如,可以将蓝色阴影替换为红色阴影:
之前:
之后:
是否可以在最新版本的 Composition Api 中使用 Composition Effects?
我看过与色调旋转、温度和色调相关的示例:
https://xamlbrewer.wordpress.com/2016/04/08/uwp-composition-effects-hue-rotation/
https://xamlbrewer.wordpress.com/2016/04/19/uwp-composition-effects-temperature-and-tint/
但我想知道 api 是否能够使用效果器切换图像中的颜色范围??
我有一个您可能会喜欢的解决方案。示例如下所示:
为了实现这一点,我在一个链中使用了 3 个效果:PixelShaderEffect
、GaussianBlurEffect
和 BlendEffect
来自 Win2d API .
XAML 有 CanvasAnimatedControl
来绘制结果加上一些助手,例如源(我们要替换的颜色)并替换颜色选择器和阈值滑块。
<Grid>
<xaml:CanvasAnimatedControl x:Name="AnimatedControl"
CreateResources="AnimatedControl_OnCreateResources"
Draw="AnimatedControl_OnDraw"/>
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Bottom">
<TextBlock Text="Source Color" FontSize="32" Foreground="White" TextAlignment="Center"/>
<ColorSpectrum Width="256" Height="256" ColorChanged="ColorSpectrum_OnColorChanged"/>
</StackPanel>
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Bottom">
<TextBlock Text="Replace Color" FontSize="32" Foreground="White" TextAlignment="Center"/>
<ColorSpectrum Width="256" Height="256" ColorChanged="ColorSpectrum_OnColorChanged1"/>
</StackPanel>
<Slider Width="512" ValueChanged="RangeBase_OnValueChanged" VerticalAlignment="Center"/>
</Grid>
后面的代码:
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
有编译 hlsl 着色器的 cmd 文件。如果您需要帮助 - 请发表评论。玩得开心!