/// <summary> /// Constructor that initializes to ColorPicker to the specified color. /// </summary> /// <param name="initialColor"></param> public ColorPicker(Color initialColor) { this.InitializeComponent(); this.SelectedColor = initialColor; this.SelectedBrightness = 255; }
/// <summary> /// Sets a new Selected Color based on the color of the pixel under the mouse pointer. /// </summary> private void UpdateColor() { // Test to ensure we do not get bad mouse positions along the edges int imageX = (int)Mouse.GetPosition(this.canvasImage).X; int imageY = (int)Mouse.GetPosition(this.canvasImage).Y; if ((imageX < 0) || (imageY < 0) || (imageX > this.ColorImage.Width - 1) || (imageY > this.ColorImage.Height - 1)) return; // Get the single pixel under the mouse into a bitmap and copy it to a byte array CroppedBitmap cb = new CroppedBitmap(this.ColorImage.Source as BitmapSource, new Int32Rect(imageX, imageY, 1, 1)); byte[] pixels = new byte[4]; cb.CopyPixels(pixels, 4, 0); // Update the mouse cursor position and the Selected Color this.ellipsePixel.SetValue(Canvas.LeftProperty, (double)(Mouse.GetPosition(this.canvasImage).X - (this.ellipsePixel.Width / 2.0))); this.ellipsePixel.SetValue(Canvas.TopProperty, (double)(Mouse.GetPosition(this.canvasImage).Y - (this.ellipsePixel.Width / 2.0))); this.canvasImage.InvalidateVisual(); // Set the Selected Color based on the cursor pixel and Alpha Slider value this.SelectedColor = new Color(pixels[2], pixels[1], pixels[0]); this.SelectedBrightness = (byte)this.AlphaSlider.Value; }
/// <summary> /// Update the mouse cursor ellipse position. /// </summary> private void UpdateCursorEllipse(Color searchColor) { // Scan the canvas image for a color which matches the search color CroppedBitmap cb; Color tempColor = new Color(); byte[] pixels = new byte[4]; int searchY = 0; int searchX = 0; for (searchY = 0; searchY <= this.canvasImage.Width - 1; searchY++) { for (searchX = 0; searchX <= this.canvasImage.Height - 1; searchX++) { cb = new CroppedBitmap(this.ColorImage.Source as BitmapSource, new Int32Rect(searchX, searchY, 1, 1)); cb.CopyPixels(pixels, 4, 0); tempColor = new Color(pixels[2], pixels[1], pixels[0]); if (tempColor == searchColor) break; } if (tempColor == searchColor) break; } // Default to the top left if no match is found if (tempColor != searchColor) { searchX = 0; searchY = 0; } // Update the mouse cursor ellipse position this.ellipsePixel.SetValue(Canvas.LeftProperty, ((double)searchX - (this.ellipsePixel.Width / 2.0))); this.ellipsePixel.SetValue(Canvas.TopProperty, ((double)searchY - (this.ellipsePixel.Width / 2.0))); }
/// <summary> /// Update SelectedColor Alpha based on Slider value. /// </summary> private void AlphaSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { this.SelectedBrightness = (byte)this.AlphaSlider.Value; this.SelectedColor = new Color(this.SelectedColor.Red, this.SelectedColor.Green, this.SelectedColor.Blue); }