Skip to content

Commit

Permalink
从图片背景应用主题色
Browse files Browse the repository at this point in the history
  • Loading branch information
natsurainko committed Nov 1, 2024
1 parent a3bb09c commit f932e88
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
42 changes: 42 additions & 0 deletions Natsurainko.FluentLauncher/Utils/DominantColorHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;

namespace Natsurainko.FluentLauncher.Utils;

internal static class DominantColorHelper
{
public static async Task<Windows.UI.Color> GetColorFromImageAsync(string imageFile)
{
var colorDictionary = new Dictionary<Color, int>();
using var bitmap = new Bitmap(imageFile);

for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
Color pixelColor = bitmap.GetPixel(x, y);
if (!colorDictionary.ContainsKey(pixelColor))
colorDictionary[pixelColor] = 1;
else colorDictionary[pixelColor] += 1;
}
}

var resultColor = colorDictionary
.OrderByDescending(kvp => kvp.Value)
.First(kvp =>
{
double brightness = 0.299 * kvp.Key.R + 0.587 * kvp.Key.G + 0.114 * kvp.Key.B;
return brightness > 128 && brightness < 240;
}).Key;

return await Task.FromResult(new Windows.UI.Color
{
A = 255,
R = resultColor.R,
G = resultColor.G,
B = resultColor.B,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
using CommunityToolkit.Mvvm.Input;
using FluentLauncher.Infra.Settings.Mvvm;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Win32;
using Natsurainko.FluentLauncher.Services.Settings;
using Natsurainko.FluentLauncher.Services.UI;
using Natsurainko.FluentLauncher.Utils;
using Natsurainko.FluentLauncher.ViewModels.Common;
using Natsurainko.FluentLauncher.XamlHelpers.Converters;
using System.IO;
using System.Threading.Tasks;
using Windows.UI;

#nullable disable
Expand All @@ -17,10 +21,13 @@ internal partial class AppearanceViewModel : SettingsViewModelBase, ISettingsVie
{
[SettingsProvider]
private readonly SettingsService _settingsService;
private readonly NotificationService _notificationService;

public AppearanceViewModel(SettingsService settingsService)
public AppearanceViewModel(SettingsService settingsService, NotificationService notificationService)
{
_settingsService = settingsService;
_notificationService = notificationService;

(this as ISettingsViewModel).InitializeSettings();
}

Expand All @@ -30,6 +37,7 @@ public AppearanceViewModel(SettingsService settingsService)

[ObservableProperty]
[BindToSetting(Path = nameof(SettingsService.BackgroundMode))]
[NotifyPropertyChangedFor(nameof(CanUseImageThemeColor))]
private int backgroundMode;

[ObservableProperty]
Expand All @@ -44,6 +52,7 @@ public AppearanceViewModel(SettingsService settingsService)
[ObservableProperty]
[BindToSetting(Path = nameof(SettingsService.ImageFilePath))]
[NotifyPropertyChangedFor(nameof(ImageFileExists))]
[NotifyPropertyChangedFor(nameof(CanUseImageThemeColor))]
private string imageFilePath;

[ObservableProperty]
Expand Down Expand Up @@ -71,6 +80,8 @@ public AppearanceViewModel(SettingsService settingsService)

public bool ImageFileExists => File.Exists(ImageFilePath);

public bool CanUseImageThemeColor => BackgroundMode == 3 && ImageFileExists;

private Flyout backgroundColorFlyout;
private Flyout themeColorFlyout;

Expand All @@ -96,6 +107,17 @@ private void SelectColorConfirm(Button button)
private void RadioButtonChecked(int index)
=> BackgroundMode = index;

[RelayCommand]
private async Task UseImageThemeColor()
{
CustomThemeColor = await DominantColorHelper.GetColorFromImageAsync(ImageFilePath);
var converter = (ColorHexCodeConverter)Application.Current.Resources["ColorHexCodeConverter"];

_notificationService.NotifyWithoutContent(
$"Image theme color successfully set, {converter.Convert(CustomThemeColor, null, null, null)}",
icon: "\uE73E");
}

/// <summary>
/// 神金 Command 无法被触发
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions Natsurainko.FluentLauncher/Views/Settings/AppearancePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
</Setter.Value>
</Setter>
</Style>
<converters:BoolToVisibilityConverter x:Key="InvertedBoolToVisibilityConverter" Inverted="True" />
</Page.Resources>

<ScrollViewer>
Expand Down Expand Up @@ -442,6 +443,25 @@
</Button>
</StackPanel>
</controls:SettingsCard>
<controls:SettingsCard
Command="{Binding UseImageThemeColorCommand}"
Header="Use image theme color"
IsClickEnabled="True"
IsEnabled="{Binding CanUseImageThemeColor}">
<controls:SettingsCard.Description>
<StackPanel>
<TextBlock
Foreground="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"
Style="{ThemeResource CaptionTextBlockStyle}"
Text="Select a theme color from the currently selected image background" />
<TextBlock
Foreground="{ThemeResource SystemErrorTextColor}"
Style="{ThemeResource CaptionTextBlockStyle}"
Text="Must use valid image background"
Visibility="{Binding CanUseImageThemeColor, Converter={ThemeResource InvertedBoolToVisibilityConverter}}" />
</StackPanel>
</controls:SettingsCard.Description>
</controls:SettingsCard>
</controls:SettingsExpander.Items>

</controls:SettingsExpander>
Expand Down

0 comments on commit f932e88

Please sign in to comment.