From 6d291ea40b469e5ddc6d705621d53582332fb6d1 Mon Sep 17 00:00:00 2001 From: Thomas May Date: Thu, 5 Dec 2024 17:00:31 +0000 Subject: [PATCH 1/6] Add lightness detection to banner image --- UniSky/Helpers/Interop/BitmapInterop.cs | 40 ++++++++++ UniSky/Pages/HomePage.xaml | 10 --- UniSky/Pages/HomePage.xaml.cs | 1 + UniSky/Pages/ProfilePage.xaml | 4 +- UniSky/Pages/ProfilePage.xaml.cs | 1 + UniSky/Services/ISafeAreaService.cs | 3 + UniSky/Services/SafeAreaService.cs | 12 ++- UniSky/ViewModels/Post/PostViewModel.cs | 2 +- .../Profile/ProfilePageViewModel.cs | 78 +++++++++++++++---- 9 files changed, 123 insertions(+), 28 deletions(-) diff --git a/UniSky/Helpers/Interop/BitmapInterop.cs b/UniSky/Helpers/Interop/BitmapInterop.cs index 4299ce0..3fea2be 100644 --- a/UniSky/Helpers/Interop/BitmapInterop.cs +++ b/UniSky/Helpers/Interop/BitmapInterop.cs @@ -1,8 +1,11 @@ using System; using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; +using FishyFlip.Lexicon.App.Bsky.Actor; +using Microsoft.Toolkit.Uwp; using Windows.ApplicationModel.DataTransfer; using Windows.Graphics.Imaging; using Windows.Storage; @@ -158,6 +161,43 @@ unsafe interface IMemoryBufferByteAccess void GetBuffer(out byte* buffer, out uint capacity); } + public static async Task GetImageAverageBrightnessAsync(IRandomAccessStream stream) + { + static float RgbToLightness(double r, double g, double b) + { + float _R = (float)(r / 255f); + float _G = (float)(g / 255f); + float _B = (float)(b / 255f); + + float _Min = Math.Min(Math.Min(_R, _G), _B); + float _Max = Math.Max(Math.Max(_R, _G), _B); + float _Delta = _Max - _Min; + + float L = (float)((_Max + _Min) / 2.0f); + + return L; + } + + var decoder = await BitmapDecoder.CreateAsync(stream); + var transform = new BitmapTransform() { ScaledWidth = 1, ScaledHeight = 1, InterpolationMode = BitmapInterpolationMode.Cubic }; + + using var bitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); + using var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.Read); + using var reference = buffer.CreateReference(); + + unsafe + { + ((IMemoryBufferByteAccess)reference).GetBuffer(out var raw, out var capacity); + + var b = raw[0]; + var g = raw[1]; + var r = raw[2]; + var a = raw[3]; + + return RgbToLightness(r, g, b); + } + } + public static async Task SaveBitmapToFileAsync(RandomAccessStreamReference streamReference) { var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync($"{Guid.NewGuid()}.png"); diff --git a/UniSky/Pages/HomePage.xaml b/UniSky/Pages/HomePage.xaml index e8abf70..ef0ac56 100644 --- a/UniSky/Pages/HomePage.xaml +++ b/UniSky/Pages/HomePage.xaml @@ -380,16 +380,6 @@ - diff --git a/UniSky/Pages/HomePage.xaml.cs b/UniSky/Pages/HomePage.xaml.cs index e1b8f40..f6268d5 100644 --- a/UniSky/Pages/HomePage.xaml.cs +++ b/UniSky/Pages/HomePage.xaml.cs @@ -75,6 +75,7 @@ private void OnSafeAreaUpdated(object sender, SafeAreaUpdatedEventArgs e) VisualStateManager.GoToState(this, "Inactive", true); } + AppTitleBar.RequestedTheme = e.SafeArea.Theme; Margin = new Thickness(e.SafeArea.Bounds.Left, 0, e.SafeArea.Bounds.Right, e.SafeArea.Bounds.Bottom); } diff --git a/UniSky/Pages/ProfilePage.xaml b/UniSky/Pages/ProfilePage.xaml index a8c0553..46bac43 100644 --- a/UniSky/Pages/ProfilePage.xaml +++ b/UniSky/Pages/ProfilePage.xaml @@ -184,7 +184,7 @@ Grid.ColumnSpan="2" Grid.Row="1" VerticalAlignment="Bottom" - RequestedTheme="Dark"> + RequestedTheme="{Binding Theme}"> @@ -208,7 +208,7 @@ Tapped="ProfileContainer_Tapped" Margin="-60,-8,-8,-8" Grid.ColumnSpan="2" - Grid.RowSpan="2"/> + Grid.RowSpan="2"/>