Exemplo n.º 1
0
        protected override void OnRender(DrawingContext drawingContext)
        {
            // calculate pixels of color canvas
            var pixels = new byte[(int)ActualWidth * (int)ActualHeight * 4];

            var saturation = Saturation;

            var width  = (int)ActualWidth;
            var height = (int)ActualHeight;
            var i      = 0;

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    var luminance = 1f - (double)y / height;
                    var hue       = x * 365d / width;
                    var color     = new HslRgbColor(hue, saturation, luminance);

                    pixels[i++] = color.B;
                    pixels[i++] = color.G;
                    pixels[i++] = color.R;
                    i++; // Bgr32 format ignores 4th byte
                }
            }

            if (_bitmap == null || (_bitmap.PixelWidth == width && _bitmap.PixelHeight == height))
            {
                _bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr32, null); // heavy ctor, reuse instance
            }
            _bitmap.WritePixels(new Int32Rect(0, 0, (int)ActualWidth, (int)ActualHeight), pixels, (int)ActualWidth * 4, 0);
            drawingContext.DrawImage(_bitmap, new Rect(RenderSize));
        }
        private void UpdateGradient()
        {
            if (_size.Width == 0)
            {
                return;
            }

            var pixels = new byte[(int)_size.Width * 4];

            var width   = (int)_size.Width;
            var channel = ColorChannel;
            var i       = 0;

            for (var x = 0; x < width; x++)
            {
                HslRgbColor color = null;
                var         value = (double)x / width;
                switch (channel)
                {
                case ColorChannel.Red:
                    color = new HslRgbColor((byte)(value * 255), Green, Blue);
                    break;

                case ColorChannel.Green:
                    color = new HslRgbColor(Green, (byte)(value * 255), Blue);
                    break;

                case ColorChannel.Blue:
                    color = new HslRgbColor(Green, Blue, (byte)(value * 255));
                    break;

                case ColorChannel.Hue:
                    color = new HslRgbColor(value * 365d, Saturation, Luminance);
                    break;

                case ColorChannel.Saturation:
                    color = new HslRgbColor(Hue, value, Luminance);
                    break;

                case ColorChannel.Luminance:
                    color = new HslRgbColor(Hue, Saturation, value);
                    break;
                }

                pixels[i++] = color.B;
                pixels[i++] = color.G;
                pixels[i++] = color.R;
                i++;
            }

            if (_bitmap == null || width != _bitmap.PixelWidth)
            {
                _bitmap = new WriteableBitmap(width, 1, 96, 96, PixelFormats.Bgr32, null); // this is a heavy one, reuse as much as possible
            }
            _bitmap.WritePixels(new Int32Rect(0, 0, width, 1), pixels, width * 4, 0);
        }
Exemplo n.º 3
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            GetTemplateParts();

            // ensure all fields contain the initial values because Color was set before ApplyTemplate().
            UpdateAll();

            _hueTextBox.KeyDown += ConvertEnterToTab;
            _hueTextBox.KeyDown += ConvertEnterToTab;
            _hueTextBox.KeyDown += ConvertEnterToTab;

            _hueTextBox.KeyDown += ConvertEnterToTab;
            _hueTextBox.KeyDown += ConvertEnterToTab;
            _hueTextBox.KeyDown += ConvertEnterToTab;


            _hueTextBox.LostFocus        += (sender, args) => { OnValueTextBoxLostFocus(_hueTextBox, ColorChannel.Hue, Color.H, 1, 359); };
            _saturationTextBox.LostFocus += (sender, args) => { OnValueTextBoxLostFocus(_saturationTextBox, ColorChannel.Saturation, Color.S, 100, 1); };
            _luminanceTextBox.LostFocus  += (sender, args) => { OnValueTextBoxLostFocus(_luminanceTextBox, ColorChannel.Luminance, Color.L, 100, 1); };

            _redTextBox.LostFocus   += (sender, args) => { OnValueTextBoxLostFocus(_redTextBox, ColorChannel.Red, Color.R, 1, 255); };
            _greenTextBox.LostFocus += (sender, args) => { OnValueTextBoxLostFocus(_greenTextBox, ColorChannel.Green, Color.G, 1, 255); };
            _blueTextBox.LostFocus  += (sender, args) => { OnValueTextBoxLostFocus(_blueTextBox, ColorChannel.Blue, Color.B, 1, 255); };

            _hueSlider.ValueChanged += (sender, args) => { if (!WasUserChange(sender))
                                                           {
                                                               return;
                                                           }
                                                           ChangeColorChannelValue(ColorChannel.Hue, _hueSlider.Value); };
            _saturationSlider.ValueChanged += (sender, args) => { if (!WasUserChange(sender))
                                                                  {
                                                                      return;
                                                                  }
                                                                  ChangeColorChannelValue(ColorChannel.Saturation, _saturationSlider.Value); };
            _luminanceSlider.ValueChanged += (sender, args) => { if (!WasUserChange(sender))
                                                                 {
                                                                     return;
                                                                 }
                                                                 ChangeColorChannelValue(ColorChannel.Luminance, _luminanceSlider.Value); };

            _redSlider.ValueChanged += (sender, args) => { if (!WasUserChange(sender))
                                                           {
                                                               return;
                                                           }
                                                           ChangeColorChannelValue(ColorChannel.Red, _redSlider.Value); };
            _greenSlider.ValueChanged += (sender, args) => { if (!WasUserChange(sender))
                                                             {
                                                                 return;
                                                             }
                                                             ChangeColorChannelValue(ColorChannel.Green, _greenSlider.Value); };
            _blueSlider.ValueChanged += (sender, args) => { if (!WasUserChange(sender))
                                                            {
                                                                return;
                                                            }
                                                            ChangeColorChannelValue(ColorChannel.Blue, _blueSlider.Value); };

            _hexCodeTextBox.LostFocus += (sender, args) =>
            {
                if (ColorUtils.IsHexCode(_hexCodeTextBox.Text))
                {
                    var color = ColorUtils.FromHex(_hexCodeTextBox.Text);
                    Color = new HslRgbColor(color.R, color.G, color.B);
                }
                else
                {
                    UpdateHexCodeField(); // revert
                }
            };

            _colorCanvas.ColorPicked += (sender, args) => Color = args.Color;

            _isTemplateApplied = true;
        }
Exemplo n.º 4
0
 public ColorRoutedEventArgs(RoutedEvent routedEvent, HslRgbColor color)
     : base(routedEvent)
 {
     Color = color;
 }