![]() ![]() / /// gets or privately sets the selected This.Loaded += new RoutedEventHandler(ColorPicker_Loaded) Private Color selectedColor = Colors.Transparent Private DrawingAttributes drawingAttributes = new DrawingAttributes() Public partial class ColorPicker : UserControl Here is the entire XAML for the ColorPicker control:Īnd here is all the C# code-behind for the ColorPicker control: In essence, that is how it works, so probably time for some code. And the nice checker background is easily achieved in WPF using a DrawingBrush. One of the nice things about this control is the Preview, which is simply using a WPF InkPresenter control and showing some strokes on it. The following diagram may help to explain it a little better. Basically, a single pixel under the mouse is retrieved from the current static swatch image, and this is made into a 1*1 byte array, and then the values within this 1*1 byte array are used to create a Color, taking into account the current Alpha slider value. The SelectedColor is worked out based on where the mouse is in relationship to the current static swatch image. The user is then able to use the mouse to move around the image, and also adjust the Alpha value using the slider. That the user may pick from, using the image buttons provided at the top of the ColorPicker control. There are a number of static swatch images: The actual ColorPicker control works as follows: RectColorPicked.Fill = new SolidColorBrush(colorDialog.SelectedColor) Private void btnPickColor_Click( object sender, RoutedEventArgs e)ĬolorDialog colorDialog = new ColorDialog() There is a WPF Window called ColorDialog which holds the actual ColorPicker control. Well yep, this next section explains all that is involved, which ain't that much. So how does it all work? Well, it is actually so simple you probably would think "Ha, is that it". Read more about his alternative honeycomb color picker over at his blog. ShawnVN's blog: For his idea of having a preview area and an Alpha slider.Lee Brimelow: For his idea of having an image swatch image, and copying the pixel under the mouse.I should point out before I start that I did borrow an idea or two from other excellent sources from the Special Thanks Goes Out To No trains/buses/taxis were on, so guess what I did? That's right, I wrote a color picker. I joked to a fellow CodeProject author that I just may have to try and write a color picker for WPF, as this article sparked my interest a bit.Īs luck would have it, I woke up today (Monday) and tried to get into work (I work in London and live 50 miles away), but was faced with the worst snow storm in the UK for some time. The only problem was the code was not attached to that article and the article text gave you no clue as to how the damn thing worked. Yesterday, someone published a WPF color picker that looked really cool. Yesterday, I posted an article about creating a WPF Graph, and when I post a new article, I always have a look at the new articles, including those awaiting public status.
0 Comments
Leave a Reply. |