Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

WPF: Draw ColorWheel

 

The following code creates a ColorWheel that is needed to select color in an application.

The solution is designed to automatically draw lines from the edges of a rectangle directly into the center of the rectangle using the draw_Lines () method.

The colors red, green and blue are passed through.

To the edge, the lines are drawn with a paler fence up to white.

In the C # code the parameters of the color wheel are, of course, directly adjustable.

 

 

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace wpf_Color_Selector._UserControls

{

    /// <summary>

    /// Interaction logic for UcColorSelector.xaml

    /// </summary>

    public partial class UcColorSelector : UserControl

    {

        public UcColorSelector()

        {

            InitializeComponent();

        }

 

        private void btnDraw_Click(object sender, RoutedEventArgs e)

        {

            draw_Lines();

        }

 

        private void draw_Lines()

        {

            //----------------< draw_Lines() >----------------

            double total_points = (CanvasMain.ActualWidth + CanvasMain.ActualHeight) * 2;

 

            for(int i = 0; i< total_points; i++)

            { add_line(i); }

            //----------------</ draw_Lines() >----------------

        }

 

 

        private void add_line(long iLine)

        {

            //----------------< add_line() >----------------

            //< init >

            double total_points = (CanvasMain.ActualWidth + CanvasMain.ActualHeight) * 2;

            double third_points = total_points / 3;

            //</ init >

 

            //--------< color parts >--------

            byte part_red =  get_Distance_Byte_Value (iLine, 0,total_points );

            byte part_green = get_Distance_Byte_Value(iLine, third_points , total_points);

            byte part_blue = get_Distance_Byte_Value(iLine, third_points*2, total_points);           

            //--------</ color parts >--------

 

 

 

            Color color_line = new Color();

            color_line = Color.FromRgb(part_red, part_green, part_blue);

 

            //if(iLine % 10==0) System.Diagnostics.Debug.WriteLine(iLine + " " + (int) distance + " " + third_points) ;

 

 

            //--------< Start XY >--------

            double X1 = 0;

            double Y1 = 0;

            if(iLine<CanvasMain.ActualWidth)

            {

                //----< Top >----

                X1 = iLine;

                Y1 = 0;

                //----</ Top >----

            }

            else

            {

                if (iLine < (CanvasMain.ActualWidth+CanvasMain.ActualHeight))

                {

                    //----< Right >----

                    X1 = CanvasMain.ActualWidth ;

                    Y1 = iLine-CanvasMain.ActualWidth;

                    //----</ Right >----

                }

                else

                {

                    if (iLine < ((CanvasMain.ActualWidth*2) + CanvasMain.ActualHeight))

                    {

                        //----< Bottom >----

                        X1 = CanvasMain.ActualWidth- (iLine-(CanvasMain.ActualWidth + CanvasMain.ActualHeight)) ;

                        Y1 = CanvasMain.ActualHeight;

                        //----</ Bottom >----

                    }

                    else

                    {

                        //----< Left >----

                        X1 = 0;

                        Y1 = CanvasMain.ActualHeight- (iLine-((CanvasMain.ActualWidth * 2) + CanvasMain.ActualHeight));

                        //----</ Left >----

                    }

                }

            }

            //--------</ Start XY >--------

 

 

            //------< Line >------

            Line line = new Line();

            line.Opacity = 0.9;

            line.StrokeThickness = 2;

            line.X1 = X1;

            line.Y1 = Y1;

            line.X2 = CanvasMain.ActualWidth/2;  //*center

            line.Y2 = CanvasMain.ActualHeight/2; //*center

 

            //--< ColorFlow >--

            LinearGradientBrush linear_brush = new LinearGradientBrush();

            linear_brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;

            //linear_brush.SpreadMethod = GradientSpreadMethod.Pad;

            linear_brush.MappingMode = BrushMappingMode.Absolute;

            GradientStop grad_start = new GradientStop();

            GradientStop grad_end = new GradientStop();

 

 

            //double proz = Math.Cos((line.Y2 - line.Y1) / (line.X2 - line.X1)) ;

            grad_start = new GradientStop(color_line, 0 );

            //grad_end = new GradientStop(Colors.White, 1);

            grad_end = new GradientStop(Colors.White  ,5);

            //grad_end.Color = Colors.Black  ;

 

 

            linear_brush.GradientStops.Add(grad_start);

            linear_brush.GradientStops.Add(grad_end);

            line.Stroke = linear_brush;

           

            //--</ ColorFlow >--

 

            //< add >

            CanvasMain.Children.Add(line);

            //</ add >

            //------</ Line >------

            //----------------</ add_line() >----------------

        }

 

 

        #region Region: math_functions

 

 

        private byte get_Distance_Byte_Value(double position, double target, double limit)

        {

            //----------------< get_Distance_Byte_Value() >----------------

            double distance = get_Distance(position, target, limit);

            return  get_Distance_Byte(distance,limit/3);

            //----------------</ get_Distance_Byte_Value() >----------------

        }

 

        private double get_Distance(double position, double target, double limit)

        {

            //----------------< get_Distance() >----------------

            //*calculate distance from position 0,1,2

            double range = limit / 3;

            double distance = target - position;

            if (target ==0)

            {

                //----< target-0-Limit >----

                if (position < range)

                {

                    //< 0 range >

                    distance = position;

                    //</ 0 range >

                }

                else

                {

                    if (position > (limit - range))

                    {

                        //< limit range >

                        distance = limit - position;

                        //</ limit range >

                    }

                    else

                    {

                        //< overrange >

                        distance = range;

                        //</ overrange >

                    }

                }

                //----< target-0-Limit >----

            }

            else

            {

                //----< target-1,2 >----

                distance = Math.Abs(target - position);

 

                //< overrange >

                if (distance > range) distance = range;

                //</ overrange >

                //----</ target-1,2 >----

            }

 

            //< out >

            return distance;

            //</ out >

            //----------------</ get_Distance() >----------------

        }

 

        private byte get_Distance_Byte(double distance_points, double range_points)

        {

            //----------------< get_Distance_Byte() >----------------

            double percent_distance = 0;

            if (distance_points < range_points)

            {

                percent_distance = (range_points - distance_points) / range_points;

            }

            else

            {

                percent_distance = 0;

            }

            return (byte)(percent_distance * 255);

            //----------------</ get_Distance_Byte() >----------------

        }

        #endregion /Region:  math_functions

    }

}

 

 

UserControl UcColorSelector

Xaml

<UserControl x:Class="wpf_Color_Selector._UserControls.UcColorSelector"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             xmlns:local="clr-namespace:wpf_Color_Selector._UserControls"

             mc:Ignorable="d"

             d:DesignHeight="300" d:DesignWidth="300">

    <Canvas x:Name="CanvasMain">

        <Button x:Name="btnDraw" Content="draw"  Canvas.Left="215" Canvas.Top="27" Width="48" Click="btnDraw_Click"/>

 

    </Canvas>

</UserControl>

 

 

Mobile
»
WPF / UWP: Canvas with relative positions and sizes
»
C #, WPF, UWP: ScreenShot to save an app or window area as a file
»
C #: Windows ScreenShot save as file
»
WPF Color Picker on Color Cube
»
WPF: Draw ColorWheel in C# by drawing Lines
»
WPF: Snap In to Next Lines of Elements
»
WPF Code: Usercontrol with customizable size at runtime
»
WPF: Save the background of a Windows app as a ScreenShot
»
WPF, UWP: Create transparent app window
»
WPF: Determine the relative position of an element

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de