1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Get colors from active window executable icon (#490)

* Get colors from active window executable icon
This commit is contained in:
Waster 2020-10-23 13:00:25 +02:00 committed by GitHub
parent 31d8c345f5
commit c3b34fd397
3 changed files with 49 additions and 3 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using Artemis.Core.DataModelExpansions; using Artemis.Core.DataModelExpansions;
using Artemis.Plugins.Modules.General.DataModels.Windows; using Artemis.Plugins.Modules.General.DataModels.Windows;
using SkiaSharp;
namespace Artemis.Plugins.Modules.General.DataModels namespace Artemis.Plugins.Modules.General.DataModels
{ {
@ -20,4 +21,14 @@ namespace Artemis.Plugins.Modules.General.DataModels
public DateTimeOffset CurrentTime { get; set; } public DateTimeOffset CurrentTime { get; set; }
public TimeSpan TimeSinceMidnight { 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; }
}
} }

View File

@ -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;
using Artemis.Core.DataModelExpansions; using Artemis.Core.DataModelExpansions;
using Artemis.Core.Services;
using SkiaSharp;
namespace Artemis.Plugins.Modules.General.DataModels.Windows namespace Artemis.Plugins.Modules.General.DataModels.Windows
{ {
public class WindowDataModel public class WindowDataModel
{ {
public WindowDataModel(Process process) public WindowDataModel(Process process, IColorQuantizerService _quantizerService)
{ {
Process = process; Process = process;
WindowTitle = process.MainWindowTitle; WindowTitle = process.MainWindowTitle;
@ -14,6 +20,27 @@ namespace Artemis.Plugins.Modules.General.DataModels.Windows
// Accessing MainModule requires admin privileges, this way does not // Accessing MainModule requires admin privileges, this way does not
ProgramLocation = process.GetProcessFilename(); 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<SKColor> 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] [DataModelIgnore]
@ -22,5 +49,6 @@ namespace Artemis.Plugins.Modules.General.DataModels.Windows
public string WindowTitle { get; set; } public string WindowTitle { get; set; }
public string ProcessName { get; set; } public string ProcessName { get; set; }
public string ProgramLocation { get; set; } public string ProgramLocation { get; set; }
public IconColorsDataModel Colors { get; set; }
} }
} }

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Artemis.Core.Services;
using Artemis.Plugins.Modules.General.DataModels; using Artemis.Plugins.Modules.General.DataModels;
using Artemis.Plugins.Modules.General.DataModels.Windows; using Artemis.Plugins.Modules.General.DataModels.Windows;
using Artemis.Plugins.Modules.General.Utilities; using Artemis.Plugins.Modules.General.Utilities;
@ -13,6 +14,12 @@ namespace Artemis.Plugins.Modules.General
{ {
public class GeneralModule : ProfileModule<GeneralDataModel> public class GeneralModule : ProfileModule<GeneralDataModel>
{ {
private readonly IColorQuantizerService _quantizerService;
public GeneralModule(IColorQuantizerService quantizerService) {
_quantizerService = quantizerService;
}
public override void EnablePlugin() public override void EnablePlugin()
{ {
DisplayName = "General"; DisplayName = "General";
@ -51,7 +58,7 @@ namespace Artemis.Plugins.Modules.General
{ {
int processId = WindowUtilities.GetActiveProcessId(); int processId = WindowUtilities.GetActiveProcessId();
if (DataModel.ActiveWindow == null || DataModel.ActiveWindow.Process.Id != processId) 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)) if (DataModel.ActiveWindow != null && string.IsNullOrWhiteSpace(DataModel.ActiveWindow.WindowTitle))
DataModel.ActiveWindow.WindowTitle = Process.GetProcessById(WindowUtilities.GetActiveProcessId()).MainWindowTitle; DataModel.ActiveWindow.WindowTitle = Process.GetProcessById(WindowUtilities.GetActiveProcessId()).MainWindowTitle;
} }