public async Task ApplyColorSchemeAsync(string selectedColorScheme, bool followWindowsColor, bool followAlbumCoverColor) { this.followWindowsColor = followWindowsColor; this.followAlbumCoverColor = followAlbumCoverColor; Color accentColor = default(Color); bool applySelectedColorScheme = false; try { if (followWindowsColor) { accentColor = (Color)ColorConverter.ConvertFromString(this.GetWindowsDWMColor()); } else if (followAlbumCoverColor) { byte[] artwork = await this.metadataService.GetArtworkAsync(this.playbackService.CurrentTrack.Value.Path); if (artwork?.Length > 0) { await Task.Run(() => accentColor = ImageUtils.GetDominantColor(artwork)); } else { applySelectedColorScheme = true; } } else { applySelectedColorScheme = true; } } catch (Exception) { applySelectedColorScheme = true; } if (applySelectedColorScheme) { ColorScheme cs = this.GetColorScheme(selectedColorScheme); accentColor = (Color)ColorConverter.ConvertFromString(cs.AccentColor); } else { accentColor = HSLColor.Normalize(accentColor, 30); } if (Application.Current.Resources["RG_AccentColor"] == null) { this.ApplyAccentColor(accentColor); return; } this.InterpolateAccentColorAsync(accentColor); }
public override async Task ApplyColorSchemeAsync(string selectedColorScheme, bool followWindowsColor, bool followAlbumCoverColor, bool isViewModelLoaded = false) { this.FollowWindowsColor = followWindowsColor; this.followAlbumCoverColor = followAlbumCoverColor; Color accentColor = default(Color); bool applySelectedColorScheme = false; try { if (followWindowsColor) { accentColor = (Color)ColorConverter.ConvertFromString(GetWindowsDWMColor()); } else if (followAlbumCoverColor) { byte[] artwork = await this.metadataService.GetArtworkAsync(this.playbackService.CurrentTrack.Value.Path); if (artwork?.Length > 0) { await Task.Run(() => accentColor = ImageUtils.GetDominantColor(artwork)); } else { applySelectedColorScheme = true; } } else { applySelectedColorScheme = true; } } catch (Exception) { applySelectedColorScheme = true; } if (applySelectedColorScheme) { ColorScheme cs = this.GetColorScheme(selectedColorScheme); accentColor = (Color)ColorConverter.ConvertFromString(cs.AccentColor); } else { accentColor = HSLColor.Normalize(accentColor, 30); } if (!isViewModelLoaded) { await Task.Run(() => { int loop = 0; // TODO: // System.Timers.Timer cannot work in actual time, it's much slower // than DispatcherTimer, if we can find some way to fix time by not // using DispatcherTimer, loopMax should be set to 50 to enhance gradient int loopMax = 30; var oldColor = (Color)Application.Current.Resources["RG_AccentColor"]; if (applyColorSchemeTimer != null) { applyColorSchemeTimer.Stop(); applyColorSchemeTimer = null; } applyColorSchemeTimer = new Timer() { Interval = colorSchemeTimeoutSeconds * 1000d / loopMax }; applyColorSchemeTimer.Elapsed += (_, __) => { loop++; var color = AnimatedTypeHelpers.InterpolateColor(oldColor, accentColor, loop / (double)loopMax); Application.Current.Resources["RG_AccentColor"] = color; Application.Current.Resources["RG_AccentBrush"] = new SolidColorBrush(color); if (loop == loopMax) { if (applyColorSchemeTimer != null) { applyColorSchemeTimer.Stop(); applyColorSchemeTimer = null; } // Re-apply theme to ensure brushes referencing AccentColor are updated this.ReApplyTheme(); this.OnColorSchemeChanged(new EventArgs()); } }; applyColorSchemeTimer.Start(); }); } else { Application.Current.Resources["RG_AccentColor"] = accentColor; Application.Current.Resources["RG_AccentBrush"] = new SolidColorBrush(accentColor); // Re-apply theme to ensure brushes referencing AccentColor are updated this.ReApplyTheme(); this.OnColorSchemeChanged(new EventArgs()); } }