Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

c# - How to create a color canvas for color picker (wpf)

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.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...