From eb9bb2d6a0995aba2e84c39b2ce17ae143989c3f Mon Sep 17 00:00:00 2001 From: robloo Date: Thu, 28 Apr 2022 20:29:12 -0400 Subject: [PATCH] Standardize ImageBrush usage --- .../ColorSlider/ColorSlider.cs | 2 +- .../ColorSpectrum/ColorSpectrum.cs | 12 ++-- .../Helpers/ColorHelpers.cs | 55 ++----------------- 3 files changed, 13 insertions(+), 56 deletions(-) diff --git a/src/Avalonia.Controls.ColorPicker/ColorSlider/ColorSlider.cs b/src/Avalonia.Controls.ColorPicker/ColorSlider/ColorSlider.cs index b3c8fcb775..987d08ec64 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorSlider/ColorSlider.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorSlider/ColorSlider.cs @@ -102,7 +102,7 @@ namespace Avalonia.Controls.Primitives if (bitmap != null) { - Background = ColorHelpers.BitmapToBrushAsync(bitmap, pixelWidth, pixelHeight); + Background = new ImageBrush(ColorHelpers.CreateBitmapFromPixelData(bitmap, pixelWidth, pixelHeight)); } } } diff --git a/src/Avalonia.Controls.ColorPicker/ColorSpectrum/ColorSpectrum.cs b/src/Avalonia.Controls.ColorPicker/ColorSpectrum/ColorSpectrum.cs index 563fa24c08..3a58fc048f 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorSpectrum/ColorSpectrum.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorSpectrum/ColorSpectrum.cs @@ -1059,8 +1059,8 @@ namespace Avalonia.Controls.Primitives ColorSpectrumComponents components2 = Components; - WriteableBitmap minBitmap = ColorHelpers.CreateBitmapFromPixelData(pixelWidth, pixelHeight, bgraMinPixelData); - WriteableBitmap maxBitmap = ColorHelpers.CreateBitmapFromPixelData(pixelWidth, pixelHeight, bgraMaxPixelData); + WriteableBitmap minBitmap = ColorHelpers.CreateBitmapFromPixelData(bgraMinPixelData, pixelWidth, pixelHeight); + WriteableBitmap maxBitmap = ColorHelpers.CreateBitmapFromPixelData(bgraMaxPixelData, pixelWidth, pixelHeight); switch (components2) { @@ -1076,10 +1076,10 @@ namespace Avalonia.Controls.Primitives case ColorSpectrumComponents.ValueSaturation: case ColorSpectrumComponents.SaturationValue: _hueRedBitmap = minBitmap; - _hueYellowBitmap = ColorHelpers.CreateBitmapFromPixelData(pixelWidth, pixelHeight, bgraMiddle1PixelData); - _hueGreenBitmap = ColorHelpers.CreateBitmapFromPixelData(pixelWidth, pixelHeight, bgraMiddle2PixelData); - _hueCyanBitmap = ColorHelpers.CreateBitmapFromPixelData(pixelWidth, pixelHeight, bgraMiddle3PixelData); - _hueBlueBitmap = ColorHelpers.CreateBitmapFromPixelData(pixelWidth, pixelHeight, bgraMiddle4PixelData); + _hueYellowBitmap = ColorHelpers.CreateBitmapFromPixelData(bgraMiddle1PixelData, pixelWidth, pixelHeight); + _hueGreenBitmap = ColorHelpers.CreateBitmapFromPixelData(bgraMiddle2PixelData, pixelWidth, pixelHeight); + _hueCyanBitmap = ColorHelpers.CreateBitmapFromPixelData(bgraMiddle3PixelData, pixelWidth, pixelHeight); + _hueBlueBitmap = ColorHelpers.CreateBitmapFromPixelData(bgraMiddle4PixelData, pixelWidth, pixelHeight); _huePurpleBitmap = maxBitmap; break; } diff --git a/src/Avalonia.Controls.ColorPicker/Helpers/ColorHelpers.cs b/src/Avalonia.Controls.ColorPicker/Helpers/ColorHelpers.cs index 36ee478c7b..1014c5b270 100644 --- a/src/Avalonia.Controls.ColorPicker/Helpers/ColorHelpers.cs +++ b/src/Avalonia.Controls.ColorPicker/Helpers/ColorHelpers.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; using Avalonia.Layout; @@ -640,16 +641,16 @@ namespace Avalonia.Controls.Primitives } /// - /// + /// Converts the given raw BGRA pre-multiplied alpha pixel data into a bitmap. /// + /// The bitmap (in raw BGRA pre-multiplied alpha pixels). /// The pixel width of the bitmap. /// The pixel height of the bitmap. - /// - /// + /// A new . public static WriteableBitmap CreateBitmapFromPixelData( + IList bgraPixelData, int pixelWidth, - int pixelHeight, - List bgraPixelData) + int pixelHeight) { // Standard may need to change on some devices Vector dpi = new Vector(96, 96); @@ -669,50 +670,6 @@ namespace Avalonia.Controls.Primitives return bitmap; } - /// - /// Converts the given bitmap (in raw BGRA pre-multiplied alpha pixels) into an image brush - /// that can be used in the UI. - /// - /// The bitmap (in raw BGRA pre-multiplied alpha pixels) - /// to convert to a brush. - /// The pixel width of the bitmap. - /// The pixel height of the bitmap. - /// A new . - public static IBrush? BitmapToBrushAsync( - byte[] bgraPixelData, - int pixelWidth, - int pixelHeight) - { - if (bgraPixelData.Length == 0 || - (pixelWidth == 0 && - pixelHeight == 0)) - { - return null; - } - - // Standard may need to change on some devices - Vector dpi = new Vector(96, 96); - - var bitmap = new WriteableBitmap( - new PixelSize(pixelWidth, pixelHeight), - dpi, - PixelFormat.Bgra8888, - AlphaFormat.Premul); - - // Warning: This is highly questionable - using (var frameBuffer = bitmap.Lock()) - { - Marshal.Copy(bgraPixelData, 0, frameBuffer.Address, bgraPixelData.Length); - } - - var brush = new ImageBrush(bitmap) - { - Stretch = Stretch.None - }; - - return brush; - } - /// /// Gets the relative (perceptual) luminance/brightness of the given color. /// 1 is closer to white while 0 is closer to black.