Exemple #1
0
        /// <summary>
        /// Raised when the <see cref="Color"/> property is modified.
        /// </summary>
        private void OnColorChanged()
        {
            bool isInitializing = !templateApplied && initializingProperty == null;

            if (isInitializing)
            {
                initializingProperty = ColorProperty;
            }

            if (!interlock)
            {
                InternalColor = ColorHSV.FromColor(Color);
                var colorRGBA = InternalColor.ToColor();
                interlock = true;

                SetCurrentValue(RedProperty, (byte)(Math.Round(colorRGBA.R * 255.0f)));
                SetCurrentValue(GreenProperty, (byte)(Math.Round(colorRGBA.G * 255.0f)));
                SetCurrentValue(BlueProperty, (byte)(Math.Round(colorRGBA.B * 255.0f)));
                SetCurrentValue(AlphaProperty, (byte)(Math.Round(colorRGBA.A * 255.0f)));

                SetCurrentValue(HueProperty, InternalColor.H);
                SetCurrentValue(SaturationProperty, InternalColor.S * 100.0f);
                SetCurrentValue(BrightnessProperty, InternalColor.V * 100.0f);
                interlock = false;
            }

            if (!suspendBindingUpdates)
            {
                RenderColorPickerSurface();
            }
            else if (colorPreviewRenderSurface != null)
            {
                colorPreviewRenderSurface.Fill = new SolidColorBrush(Color.ToSystemColor());
            }
            UpdateBinding(ColorProperty);

            if (isInitializing)
            {
                initializingProperty = null;
            }
        }
Exemple #2
0
        /// <summary>
        /// Update the color surface brush according to the current <see cref="Hue"/> value.
        /// </summary>
        private void RenderColorPickerSurface()
        {
            if (colorPreviewRenderSurface != null)
            {
                colorPreviewRenderSurface.Fill = new SolidColorBrush(Color.ToSystemColor());
            }
            if (colorPickerRenderSurface != null)
            {
                // Ensure the color picker is loaded
                if (!double.IsNaN(colorPickerRenderSurface.Width) && !double.IsNaN(colorPickerRenderSurface.Height))
                {
                    var width  = (int)colorPickerRenderSurface.Width;
                    var height = (int)colorPickerRenderSurface.Height;

                    PixelFormat pf        = PixelFormats.Bgr32;
                    int         rawStride = (width * pf.BitsPerPixel + 7) / 8;
                    var         rawImage  = new byte[rawStride * height];

                    for (int j = 0; j < height; ++j)
                    {
                        float y = j / (float)(height - 1);

                        for (int i = 0; i < width; ++i)
                        {
                            float x = i / (float)(width - 1);

                            var color4 = new ColorHSV(Hue, x, y, 1.0f).ToColor();
                            var color  = new Color(color4);
                            rawImage[(i + j * width) * 4 + 0] = color.B;
                            rawImage[(i + j * width) * 4 + 1] = color.G;
                            rawImage[(i + j * width) * 4 + 2] = color.R;
                        }
                    }

                    colorPickerRenderSurface.Fill = new DrawingBrush(new ImageDrawing(BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride), new Rect(0.0f, 0.0f, width, height)));
                }
            }
        }