C#, WPF, vb.Net, vba, SQL Server, Access Programmierer, Excel, ASP Core MVC Webforms Telefon: 07022/9319004 freib. Software Entwickler   Kontakt
#

WPF: ColorWheel zeichnen

 

Der folgende Code erstellt ein ColorWheel, welches man benötigt um Farbe in einer Anwendung auszuwählen.

Die Lösung ist so gestaltet, dass das Programm mit der Methode draw_Lines() automatisch Linien von den Rändern eines Rechtecks direkt in die Mitte des Rechtecks zeichnet.

Dabei werden die Farben Rot, Grün und Blau durchlaufen.

Zum Rand werden die Linien mit einer blasseren Fabe bis gegen Weiß gezeichnet.

Im C# Code sind die Parameter des Color-Wheels natürlich direkt einstellbar.

 

 

 

 

 

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 mit relativen Positionen und Größen
»
C#, WPF, UWP: ScreenShot einer App oder Fenster-Bereich als Datei speichern
»
C#: Windows ScreenShot als Datei speichern
»
WPF Color Picker on a White-Black Color Cube
»
WPF: ColorWheel in C# zeichnen
»
WPF: Snap Element Size to Next Lines
»
WPF Code: Usercontrol mit anpassbarer Größe zur Laufzeit
»
WPF: Speichern des Hintergrunds einer Windows App als ScreenShot
»
WPF: Transparentes Fenster App erstellen
»
WPF: Relative Position eines Elements ermitteln

.

Jobs, Projekte, Angebote für Freiberufler, Selbstständig an Raimund.Popp@Microsoft-Programmierer.de