From c3b34fd397c804a11b0ebed40e576f8b1fa8b0c4 Mon Sep 17 00:00:00 2001 From: Waster Date: Fri, 23 Oct 2020 13:00:25 +0200 Subject: [PATCH] Get colors from active window executable icon (#490) * Get colors from active window executable icon --- .../DataModels/GeneralDataModel.cs | 11 +++++++ .../DataModels/Windows/WindowDataModel.cs | 32 +++++++++++++++++-- .../GeneralModule.cs | 9 +++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Plugins/Artemis.Plugins.Modules.General/DataModels/GeneralDataModel.cs b/src/Plugins/Artemis.Plugins.Modules.General/DataModels/GeneralDataModel.cs index e986d74ee..1986dfb3f 100644 --- a/src/Plugins/Artemis.Plugins.Modules.General/DataModels/GeneralDataModel.cs +++ b/src/Plugins/Artemis.Plugins.Modules.General/DataModels/GeneralDataModel.cs @@ -1,6 +1,7 @@ using System; using Artemis.Core.DataModelExpansions; using Artemis.Plugins.Modules.General.DataModels.Windows; +using SkiaSharp; namespace Artemis.Plugins.Modules.General.DataModels { @@ -20,4 +21,14 @@ namespace Artemis.Plugins.Modules.General.DataModels public DateTimeOffset CurrentTime { get; set; } public TimeSpan TimeSinceMidnight { get; set; } } + + public class IconColorsDataModel : DataModel + { + public SKColor Vibrant { get; set; } + public SKColor LightVibrant { get; set; } + public SKColor DarkVibrant { get; set; } + public SKColor Muted { get; set; } + public SKColor LightMuted { get; set; } + public SKColor DarkMuted { get; set; } + } } \ No newline at end of file diff --git a/src/Plugins/Artemis.Plugins.Modules.General/DataModels/Windows/WindowDataModel.cs b/src/Plugins/Artemis.Plugins.Modules.General/DataModels/Windows/WindowDataModel.cs index be1af9789..a374ffdf1 100644 --- a/src/Plugins/Artemis.Plugins.Modules.General/DataModels/Windows/WindowDataModel.cs +++ b/src/Plugins/Artemis.Plugins.Modules.General/DataModels/Windows/WindowDataModel.cs @@ -1,12 +1,18 @@ -using System.Diagnostics; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.IO; +using System.Linq; using Artemis.Core; using Artemis.Core.DataModelExpansions; +using Artemis.Core.Services; +using SkiaSharp; namespace Artemis.Plugins.Modules.General.DataModels.Windows { public class WindowDataModel { - public WindowDataModel(Process process) + public WindowDataModel(Process process, IColorQuantizerService _quantizerService) { Process = process; WindowTitle = process.MainWindowTitle; @@ -14,6 +20,27 @@ namespace Artemis.Plugins.Modules.General.DataModels.Windows // Accessing MainModule requires admin privileges, this way does not ProgramLocation = process.GetProcessFilename(); + + // Get Icon colors + if(File.Exists(ProgramLocation)) + { + using MemoryStream mem = new MemoryStream(); + Icon.ExtractAssociatedIcon(ProgramLocation).Save(mem); + mem.Seek(0, SeekOrigin.Begin); + using SKBitmap skbm = SKBitmap.Decode(mem); + mem.Close(); + + List skClrs = _quantizerService.Quantize(skbm.Pixels.ToList(), 256).ToList(); + Colors = new IconColorsDataModel + { + Vibrant = _quantizerService.FindColorVariation(skClrs, ColorType.Vibrant, true), + LightVibrant = _quantizerService.FindColorVariation(skClrs, ColorType.LightVibrant, true), + DarkVibrant = _quantizerService.FindColorVariation(skClrs, ColorType.DarkVibrant, true), + Muted = _quantizerService.FindColorVariation(skClrs, ColorType.Muted, true), + LightMuted = _quantizerService.FindColorVariation(skClrs, ColorType.LightMuted, true), + DarkMuted = _quantizerService.FindColorVariation(skClrs, ColorType.DarkMuted, true), + }; + } } [DataModelIgnore] @@ -22,5 +49,6 @@ namespace Artemis.Plugins.Modules.General.DataModels.Windows public string WindowTitle { get; set; } public string ProcessName { get; set; } public string ProgramLocation { get; set; } + public IconColorsDataModel Colors { get; set; } } } \ No newline at end of file diff --git a/src/Plugins/Artemis.Plugins.Modules.General/GeneralModule.cs b/src/Plugins/Artemis.Plugins.Modules.General/GeneralModule.cs index 9fc1b14f2..fdf6f02a0 100644 --- a/src/Plugins/Artemis.Plugins.Modules.General/GeneralModule.cs +++ b/src/Plugins/Artemis.Plugins.Modules.General/GeneralModule.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using Artemis.Core; using Artemis.Core.Modules; +using Artemis.Core.Services; using Artemis.Plugins.Modules.General.DataModels; using Artemis.Plugins.Modules.General.DataModels.Windows; using Artemis.Plugins.Modules.General.Utilities; @@ -13,6 +14,12 @@ namespace Artemis.Plugins.Modules.General { public class GeneralModule : ProfileModule { + private readonly IColorQuantizerService _quantizerService; + + public GeneralModule(IColorQuantizerService quantizerService) { + _quantizerService = quantizerService; + } + public override void EnablePlugin() { DisplayName = "General"; @@ -51,7 +58,7 @@ namespace Artemis.Plugins.Modules.General { int processId = WindowUtilities.GetActiveProcessId(); if (DataModel.ActiveWindow == null || DataModel.ActiveWindow.Process.Id != processId) - DataModel.ActiveWindow = new WindowDataModel(Process.GetProcessById(processId)); + DataModel.ActiveWindow = new WindowDataModel(Process.GetProcessById(processId), _quantizerService); if (DataModel.ActiveWindow != null && string.IsNullOrWhiteSpace(DataModel.ActiveWindow.WindowTitle)) DataModel.ActiveWindow.WindowTitle = Process.GetProcessById(WindowUtilities.GetActiveProcessId()).MainWindowTitle; }