protected override Size ArrangeOverride(Size finalSize) { var fs = base.ArrangeOverride(finalSize); var width = fs.Width; var height = fs.Height; if (cpRender != null) { if (cpRender.Mode == ColorPickerMode.HexagonWheel || cpRender.Mode == ColorPickerMode.Wheel) { float mn1 = width < height ? (float)width : (float)height; float mn2 = cpRender.Bounds.Width; if (mn1 == mn2) { return(fs); } } else if (cpRender.Bounds.Width == width && cpRender.Bounds.Height == height) { return(fs); } else { cpRender = null; } } RenderPicker((int)width, (int)height); return(fs); }
protected override void OnSizeAllocated(double width, double height) { base.OnSizeAllocated(width, height); if (cpRender != null) { if (cpRender.Mode == ColorPickerMode.HexagonWheel || cpRender.Mode == ColorPickerMode.Wheel) { float mn1 = width < height ? (float)width : (float)height; float mn2 = cpRender.Bounds.Width; if (mn1 == mn2) { return; } } else if (cpRender.Bounds.Width == width && cpRender.Bounds.Height == height) { return; } else { cpRender = null; } } RenderPicker((int)width, (int)height); }
private void RenderPicker(int w = 0, int h = 0) { var disp = Dispatcher; double width = this.ActualWidth; double height = this.ActualHeight; double colorVal = this.ColorValue; double offset = this.HueOffset; bool invert = this.InvertSaturation; bool tetra = this.Tetrachromatic; float esize = this.ElementSize; int hwt = this.HueWheelThickness; int ps = this.HuePointerSize; ColorPickerMode mode = this.Mode; _ = Task.Run(() => { ColorPickerRenderer cw; if (w <= 0) { if (double.IsNaN(width)) { return; } w = (int)width; } if (h <= 0) { if (double.IsNaN(height)) { return; } h = (int)height; } if (w < 32 || h < 32) { return; } if (mode == ColorPickerMode.Wheel || mode == ColorPickerMode.HexagonWheel || mode == ColorPickerMode.HueWheel) { int rad; if (h < w) { rad = h / 2; w = h; } else { rad = w / 2; h = w; } if (mode == ColorPickerMode.Wheel) { cw = new ColorPickerRenderer(rad, colorVal, offset, invert); } else if (mode == ColorPickerMode.HueWheel) { rad -= ((ps / 2) + (ps / 5)); cw = new ColorPickerRenderer(rad, colorVal, offset, invert, false, hwt); } else { cw = new ColorPickerRenderer(rad, esize, colorVal, invert); } } else { if (mode == ColorPickerMode.HueBoxHorizontal || mode == ColorPickerMode.HueBoxVertical) { cw = new ColorPickerRenderer(w, h, true, invert, mode == ColorPickerMode.HueBoxVertical, tetra); } else { cw = new ColorPickerRenderer(w, h, colorVal, offset, invert, mode == ColorPickerMode.LinearVertical || mode == ColorPickerMode.HueBarVertical, false, mode == ColorPickerMode.HueBarHorizontal || mode == ColorPickerMode.HueBarVertical); } } disp.Invoke(() => { CursorCanvas.Width = w; CursorCanvas.Height = h; //CursorCanvas.RenderSize = new Size(w, h); cpRender = cw; PickerSite.Source = DataTools.Desktop.BitmapTools.MakeWPFImage(cpRender.Bitmap); UniColor?selc = null; //if (updateForValueChange && SelectedColor is Color scolor && selectedElement is ColorWheelElement selem) //{ // var pp = selem.Center; // foreach (var testelem in cw.Elements) // { // if (testelem.Bounds.Contains(pp)) // { // SetSelectedColor(new UniColor(testelem.Color.ToArgb())); // break; // } // } //} //updateForValueChange = false; if (selc == null) { SetSelectedColor(); } }); }); }
private void RenderPicker(int w = 0, int h = 0) { if (_container == null) { return; } double width = w > 0 ? w : this.Width; double height = h > 0 ? h : this.Height; if (width == -1 || height == -1) { return; } double colorVal = this.ColorValue; double offset = this.HueOffset; bool invert = this.InvertSaturation; float esize = this.ElementSize; ColorPickerMode mode = this.Mode; _ = Task.Run(() => { ColorPickerRenderer cw; if (w <= 0) { if (double.IsNaN(width)) { return; } w = (int)width; } if (h <= 0) { if (double.IsNaN(height)) { return; } h = (int)height; } if (w < 32 || h < 32) { return; } if (mode == ColorPickerMode.Wheel || mode == ColorPickerMode.HexagonWheel) { int rad; if (h < w) { rad = h / 2; w = h; } else { rad = w / 2; h = w; } cw = new ColorPickerRenderer(rad, colorVal, offset, invert, true); } else { cw = new ColorPickerRenderer(w, h, colorVal, offset, invert, mode == ColorPickerMode.LinearVertical, true); } DispatcherQueue.TryEnqueue(async() => { SKImage img; SKBitmap bmp = new SKBitmap((int)cw.Bounds.Width, (int)cw.Bounds.Height, SKColorType.Bgra8888, SKAlphaType.Premul); var ptr = bmp.GetPixels(); unsafe { var gch = GCHandle.Alloc(cw.ImageBytes, GCHandleType.Pinned); Buffer.MemoryCopy((void *)gch.AddrOfPinnedObject(), (void *)ptr, cw.ImageBytes.Length, cw.ImageBytes.Length); gch.Free(); } bmp.SetImmutable(); img = SKImage.FromBitmap(bmp); SKData encoded = img.Encode(); Stream stream = encoded.AsStream(); //var ret = ImageSource.FromStream(() => stream); cpRender = cw; Compositor _compositor = _container.Compositor; SpriteVisual _imageVisual; CompositionSurfaceBrush _imageBrush; _imageBrush = _compositor.CreateSurfaceBrush(); // The loadedSurface has a size of 0x0 till the image has been been downloaded, decoded and loaded to the surface. We can assign the surface to the CompositionSurfaceBrush and it will show up once the image is loaded to the surface. LoadedImageSurface _loadedSurface = LoadedImageSurface.StartLoadFromStream(stream.AsRandomAccessStream()); _imageBrush.Surface = _loadedSurface; _imageVisual = _compositor.CreateSpriteVisual(); _imageVisual.Brush = _imageBrush; _imageVisual.Size = new Vector2(cw.Bounds.Width, cw.Bounds.Height); //_imageVisual.Offset = new Vector3((float)Padding.Left, (float)Padding.Top, 0); _container.Children.RemoveAll(); _container.Children.InsertAtBottom(_imageVisual); currentShape = null; currentSprite = null; currentGeo = null; }); }); }
private void RenderPicker(int w = 0, int h = 0) { var disp = Dispatcher; double width = w > 0 ? w : this.Width; double height = h > 0 ? h : this.Height; if (width == -1 || height == -1) { return; } double colorVal = this.ColorValue; double offset = this.HueOffset; bool invert = this.InvertSaturation; float esize = this.ElementSize; ColorPickerMode mode = this.Mode; _ = Task.Run(() => { ColorPickerRenderer cw; if (w <= 0) { if (double.IsNaN(width)) { return; } w = (int)width; } if (h <= 0) { if (double.IsNaN(height)) { return; } h = (int)height; } if (w < 32 || h < 32) { return; } if (mode == ColorPickerMode.Wheel || mode == ColorPickerMode.HexagonWheel) { int rad; if (h < w) { rad = h / 2; w = h; } else { rad = w / 2; h = w; } cw = new ColorPickerRenderer(rad, colorVal, offset, invert, true); } else { cw = new ColorPickerRenderer(w, h, colorVal, offset, invert, mode == ColorPickerMode.LinearVertical, true); } Device.BeginInvokeOnMainThread(() => { SKImage img; SKBitmap bmp = new SKBitmap((int)cw.Bounds.Width, (int)cw.Bounds.Height, SKColorType.Bgra8888, SKAlphaType.Premul); var ptr = bmp.GetPixels(); unsafe { var gch = GCHandle.Alloc(cw.ImageBytes, GCHandleType.Pinned); Buffer.MemoryCopy((void *)gch.AddrOfPinnedObject(), (void *)ptr, cw.ImageBytes.Length, cw.ImageBytes.Length); gch.Free(); } bmp.SetImmutable(); img = SKImage.FromBitmap(bmp); SKData encoded = img.Encode(); Stream stream = encoded.AsStream(); var ret = ImageSource.FromStream(() => stream); cpRender = cw; PickerSite.Source = ret; }); }); }