-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsoleColorPickerTool.cs
More file actions
142 lines (120 loc) · 4.55 KB
/
Copy pathConsoleColorPickerTool.cs
File metadata and controls
142 lines (120 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
ConsoleColorPicker — sample integrated external tool for ImageGlass v10.
Copyright (C) 2026 DUONG DIEU PHAP
Project homepage: https://imageglass.org
MIT License
Behavior:
- On launch (OnExecute): logs metadata of the current photo.
- On photo change in ImageGlass: logs metadata of the new photo.
- On click in the ImageGlass viewer: logs the RGBA value of the clicked pixel.
*/
using ImageGlass.SDK.Tools;
namespace ConsoleColorPicker;
/// <summary>
/// Sample tool that logs photo metadata and the color of clicked pixels.
/// </summary>
internal sealed class ConsoleColorPickerTool : ToolBase
{
public override string ToolId => "Tool_ConsoleColorPicker";
protected override async Task OnInitializedAsync()
{
Log.Write("============================================");
Log.Write(" ConsoleColorPicker — connected to ImageGlass");
Log.Write("============================================");
Log.Write($"DataDirectory: {DataDirectory}");
// Subscribe to pointer-pressed events so we can read the pixel under the click.
try
{
await HostApi.SubscribeEventsAsync(new ToolEventSubscriptions
{
PointerPressed = true,
}).ConfigureAwait(false);
Log.Write("Subscribed to PointerPressed events.");
}
catch (Exception ex)
{
Log.Write($"SubscribeEventsAsync failed: {ex}");
}
}
protected override async Task OnExecuteAsync(CancellationToken ct)
{
Log.Write("[EXECUTE] User opened the tool.");
await PrintCurrentPhotoAsync().ConfigureAwait(false);
}
protected override void OnPhotoChanged(PhotoChangedEventArgs e)
{
// Quick info from the event itself, then fetch full metadata in the background.
Log.Write("[PHOTO CHANGED]");
if (string.IsNullOrEmpty(e.FilePath))
{
Log.Write(" (no photo loaded)");
return;
}
Log.Write($" File: {e.FilePath}");
Log.Write($" Size: {e.Width} x {e.Height} px");
Log.Write($" Format: {e.Format ?? "(unknown)"}");
Log.Write($" Frames: {e.FrameCount}{(e.CanAnimate ? " (animated)" : "")}");
// Fire-and-forget the richer metadata fetch; isolate exceptions.
_ = Task.Run(async () =>
{
try { await PrintCurrentPhotoAsync().ConfigureAwait(false); }
catch (Exception ex) { Log.Write($" Failed to read metadata: {ex}"); }
});
}
protected override void OnPointerPressed(PointerEventArgs e)
{
var x = (int)Math.Round(e.SourceX);
var y = (int)Math.Round(e.SourceY);
// Fire-and-forget the host call; isolate exceptions so async-void doesn't crash dispatch.
_ = Task.Run(async () =>
{
try
{
var color = await HostApi.ReadPixelAsync(x, y).ConfigureAwait(false);
var hex = $"#{color.R:X2}{color.G:X2}{color.B:X2}{color.A:X2}";
Log.Write($"[CLICK] ({x}, {y}) RGBA = ({color.R}, {color.G}, {color.B}, {color.A}) {hex}");
}
catch (Exception ex)
{
Log.Write($"[CLICK] Failed to read pixel at ({x}, {y}): {ex}");
}
});
}
protected override Task OnShutdownAsync()
{
Log.Write("ConsoleColorPicker shutting down. Bye!");
return Task.CompletedTask;
}
private async Task PrintCurrentPhotoAsync()
{
ToolPhotoMetadata? meta;
try
{
meta = await HostApi.GetPhotoMetadataAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
Log.Write($"GetPhotoMetadataAsync failed: {ex}");
return;
}
if (meta is null)
{
Log.Write(" (no photo loaded)");
return;
}
Log.Write($" File: {meta.FilePath}");
Log.Write($" Size: {meta.Width} x {meta.Height} px");
if (meta.Width != meta.OriginalWidth || meta.Height != meta.OriginalHeight)
{
Log.Write($" Source: {meta.OriginalWidth} x {meta.OriginalHeight} px");
}
Log.Write($" Format: {meta.Format ?? "(unknown)"}");
Log.Write($" Frames: {meta.FrameCount}{(meta.CanAnimate ? " (animated)" : "")}");
Log.Write($" Alpha: {(meta.HasAlpha ? "yes" : "no")}");
Log.Write($" Bytes: {meta.FileSizeInBytes:N0}");
if (!string.IsNullOrEmpty(meta.ColorProfileName))
{
Log.Write($" Profile: {meta.ColorProfileName}");
}
}
}