如何创建拾色器颜色的画布(WPF) [英] How to create a color canvas for color picker (wpf)

查看:981
本文介绍了如何创建拾色器颜色的画布(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个自定义颜色选择器像Visual Studio或混合或在这里(的 http://www.codeproject.com/Articles/779105/Color-Canvas-and-Color-Picker-WPF-Toolkit )。
和我有一个问题。我不知道怎么样在上面的链接创建色彩的画布。 (可能是它不是画布上。这是别的东西)
我看起来像一个非常不寻常的梯度画布......我不知道如何使它在XAML。我试图把它画在Visual Studio中,但没有运气....
任何帮助将不胜感激。

I want to create a custom color picker like in Visual Studio or Blend or here (http://www.codeproject.com/Articles/779105/Color-Canvas-and-Color-Picker-WPF-Toolkit). And I have a problem. I don't know how create color canvas like at the link above. (may be it's not canvas. it's something else) I looks like a canvas with very unusual gradient... and I have no idea how to make it in xaml. I tried to draw it in Visual Studio, but no luck.... Any help will be appreciated.

在此先感谢

推荐答案

色相栏可以使用创建普通的一个LinearGradientBrush 。水平/饱和面板可以与一个LinearGradientBrush 沿X轴的适当的颜色和另一作为沿Y不透明掩模来完成,与在黑色绘制整个事背景

The hue bar can be created with a regular LinearGradientBrush. The Level/Saturation panel can be done with a LinearGradientBrush of the appropriate color along the X axis and another as an opacity mask along the Y, with the whole thing drawn against a black background.

<Window.Resources>

    <!-- Change this to any pure hue i.e. no more than 2 rgb components set and at least 1 set to FF -->
    <Color x:Key="CurrentColor">#00FF00</Color>

    <LinearGradientBrush x:Key="HueBrush" StartPoint="0,0" EndPoint="0,1">
        <LinearGradientBrush.GradientStops>
            <GradientStop Color="#FF0000" Offset="0" />
            <GradientStop Color="#FFFF00" Offset="0.167" />
            <GradientStop Color="#00FF00" Offset="0.333" />
            <GradientStop Color="#00FFFF" Offset="0.5" />
            <GradientStop Color="#0000FF" Offset="0.667" />
            <GradientStop Color="#FF00FF" Offset="0.833" />
            <GradientStop Color="#FF0000" Offset="1" />
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <VisualBrush x:Key="LevelSaturationBrush" TileMode="None">
        <VisualBrush.Visual>
            <Canvas Background="Black" Width="1" Height="1" SnapsToDevicePixels="True">
                <Rectangle Width="1" Height="1" SnapsToDevicePixels="True">
                    <Rectangle.Fill>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="White" Offset="0" />
                                <GradientStop Color="{DynamicResource CurrentColor}" Offset="1" />
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                    <Rectangle.OpacityMask>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="#FFFFFFFF" Offset="0"/>
                                <GradientStop Color="#00FFFFFF" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Rectangle.OpacityMask>
                </Rectangle>
            </Canvas>
        </VisualBrush.Visual>
    </VisualBrush>

</Window.Resources>

<StackPanel Orientation="Horizontal">
    <Rectangle Fill="{StaticResource LevelSaturationBrush}" Width="200" Height="200" Margin="10" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" />
    <Rectangle Fill="{StaticResource HueBrush}" Width="20" Height="200" Margin="10" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" />
</StackPanel>



结果:

Result:

这篇关于如何创建拾色器颜色的画布(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆