A Visual Studio Look Alike ColorDialog For WPF
When I was learning C# language, I got the idea that WPF has already replaced Win Form, so I jumped directly into WPF with absolutely zero knowledge of Win Form. You could understand why when I was faced with the problem of there being a no ColorDialog in WPF and people suggested using the Win Form solution, I saw it as a no solution. Well I googled and found this, (the best I could come up with at the time that is). I used it till at one time I needed to use it to set a color in my code to white and I couldn’t. That drew my attention to the limited capability of the implementation and the need for me to implement my own ColorDialog.
I started by observing in order to understand the workings of the ColorDialog inside the Visual Studio WPF designer because I wanted to implement something as close to it as possible. The end may not be as fast and probably represent fewer color than the goal, but it is a huge improvement from what I started with. I borrowed the idea of swap images from the link above, but instead of storing images, they are produced on the fly by setting the colors of the pixels of a WriteableBitmap. The Initialization of the WriteableBitmap is as follows;
writeableBitmap = new WriteableBitmap (256, 256, 96, 96, PixelFormats .Bgr32, null );
ColorPalette.Source = writeableBitmap;
ColorPalette is an Image Element. By clicking on a canvas (SideBar for lack of a better name to call it), writeableBitmap is updated according to the height (with respect to SideBar) of the MouseEvent. The method below updates the position of pointers on the SideBar.
/// Updates Pointer position on SideBar
/// Also Updates Class members Rmax, Gmax, and Bmax
UpdateColorPalette() uses the updated values of the class variables Rmax, Gmax and Bmax to calculate the color of each pixel of writeableBitmap. The implementation is shown below.
The method loops through each pixel of writeableBitmap, setting the pixel to the calculated value. Note that the r, g, b components of the calculated color is dependent on the Rmax, Gmax and Bmax variables respectively which in turn depends on the position of the Pointers on the SideBar. The implementation of SetPixelColor( ) is given below.
Clicking anywhere inside the ColorPalette copies the pixel under the mouse and sets the SelectedColor to the value. The concept was borrowed from the CodeProject which link was given above. In fact the code below was copied from the project. The small changes I made to it are only architectural related and not functional.
I implemented the ColorDialog as a class library for reusability. Surprising again that you can’t add a WPF window to this type of project in Visual Studio. To work around this I implemented it as a UserControl and hosted it inside a window. I created a window in the only public method of the class and set the content of the window to an instance of the UserControl.
To use the ColorDialog from your project, add a reference to the dll file, then call the ShowDialog method of an instance of the ColorDialog.
While the project that inspired this uses only three swat images, this project uses a total of 256 distinct images and this is only limited by the height of the SideBar. This hugely increased the number of colors that can be correctly represented. But while the size significantly reduced, there is a trade off in the speed of the application.
POINTS OF INTEREST