-
-
Save charlie89/5670d46c90abf3351ed012b2afd926ca to your computer and use it in GitHub Desktop.
| #Requires AutoHotkey v2.0 | |
| ; This script creates a dark mode for specific windows by inverting their colors. | |
| ; It uses the Windows Magnifier in the background for the color inverting, but it only inverts just the rectangle in place | |
| ; of the specific windows, NOT the whole screen like the Magnifier applicaion does. This is possible through DLL calls. | |
| ; Big thanks to the original source at https://www.autohotkey.com/boards/viewtopic.php?p=563023#p563023. | |
| ; Requirements: | |
| ; - AutoHotkey needs to be installed in C:\Program Files (or use the workaround from https://www.reddit.com/r/AutoHotkey/comments/16ux144/ui_access_without_reinstalling_in_program_files_v2/). | |
| ; - In the 'Launch Settings' of 'AutoHotkey Dash' you need to enable 'UI Access'. | |
| ; Usage: | |
| ; Use Win+i to invert a window manually or add it to the auto_invert_gp group to invert it automatically. | |
| #SingleInstance Force | |
| if (!A_IsCompiled && !InStr(A_AhkPath, "_UIA")) { | |
| Run "*uiAccess " A_ScriptFullPath | |
| ExitApp | |
| } | |
| SetTitleMatchMode "RegEx" | |
| ; Add Programs for automatic dark mode here: | |
| GroupAdd "auto_invert_gp", "ahk_class #32770", , "Find|Öffnen|Speichern unter" ; Windows Explorer properties window and many others (but not the file open/save dialog that is already dark) | |
| GroupAdd "auto_invert_gp", "ahk_class OperationStatusWindow" ; Windows Explorer dialogs | |
| GroupAdd "auto_invert_gp", "ahk_exe AutoHotkeyUX.exe" | |
| GroupAdd "auto_invert_gp", "ahk_exe hh.exe" ; Windows Help | |
| GroupAdd "auto_invert_gp", "ahk_exe mmc.exe" | |
| GroupAdd "auto_invert_gp", "ahk_exe procexp64.exe" ; Process Explorer | |
| GroupAdd "auto_invert_gp", "ahk_exe regedit.exe" | |
| ;GroupAdd "auto_invert_gp", "ahk_exe Taskmgr.exe" | |
| GroupAdd "auto_invert_gp", "ahk_exe WinRAR.exe" | |
| ; Add Programs where automatic dark mode should never be applied here: | |
| ; This is mostly because they have dialogs that match ahk_class #32770 but are already in dark mode | |
| AutoInvIgnProcName:= ["notepad++.exe"] | |
| #MaxThreadsPerHotkey 2 | |
| DetectHiddenWindows true | |
| ; SetBatchLines -1 | |
| SetWinDelay -1 | |
| OnExit Uninitialize | |
| global Inverters := [] | |
| global WINDOWINFO | |
| global pTarget := 0 | |
| ; Color Matrix used to transform the colors | |
| ; Special thanks to https://github.com/mlaily/NegativeScreen/blob/master/NegativeScreen/Configuration.cs, they have many more color matrixes. | |
| ; I'm not too keen on the maths, so maybe I'm missing some things, | |
| ; but here is my basic understanding of what does what in a color matrix, when applied to a color vector: | |
| ; r*=x g+=x*r b+=x*r a+=x*r 0 | |
| ; r+=x*g g*=x b+=x*g a+=x*g 0 | |
| ; r+=x*b g+=x*b b*=x a+=x*b 0 | |
| ; r+=x*a g+=x*a b+=x*a a*=x 0 | |
| ; r+=x g+=x b+=x a+=x 1 | |
| ; Simple Inversion | |
| MatrixInv := "-1|0|0|0|0|" | |
| . "0|-1|0|0|0|" | |
| . "0|0|-1|0|0|" | |
| . "0|0|0|1|0|" | |
| . "1|1|1|0|1" | |
| ; Smart Inversion | |
| MatrixSmart := "0.333|-0.667|-0.667|0|0|" | |
| . "-0.667| 0.333|-0.667|0|0|" | |
| . "-0.667|-0.667|0.333|0|0|" | |
| . "0|0|0|1|0|" | |
| . "1|1|1|0|1" | |
| ; High saturation, good pure colors | |
| MatrixHigh := "1|-1|-1|0|0|" | |
| . "-1|1|-1|0|0|" | |
| . "-1|-1|1|0|0|" | |
| . "0|0|0|1|0|" | |
| . "1|1|1|0|1" | |
| ; set the wanted color matix | |
| global Matrix := MatrixHigh | |
| ; ==== changes below here only for more experienced users, all casual settings are above ==== | |
| ; enable per-monitor DPI awareness | |
| try dac := DllCall("SetThreadDpiAwarenessContext", 'ptr', -3, 'ptr') | |
| ; globals for taskbar height handling | |
| global TaskBarAtBottom := true | |
| global VisibleScreenHeight := A_ScreenHeight | |
| ; A window must never invert over a location that is used by the windows taskbar (ugly). | |
| ; The taskbar can be dynamically shown or hidden, so this calculates how much screen height | |
| ; the inverted window can have from the top down to the taskbar. | |
| refreshVisibleScreenHeight() | |
| { | |
| global TaskBarAtBottom | |
| global VisibleScreenHeight | |
| try { | |
| WinGetPos ,&y,, &h, "ahk_class Shell_TrayWnd" | |
| } catch { | |
| ; an error can occur when you change the windows setting "Automatically hide the taskbar" | |
| ; under Personalization -> Taskbar -> Taskbar behavioors | |
| ; because the taskbar seems to get destroyed and recreated | |
| TaskBarAtBottom := false | |
| VisibleScreenHeight:= A_ScreenHeight | |
| return | |
| } | |
| if ( y + 2 = A_ScreenHeight) { ; Taskbar hidden (it still takes up 2 pixels) | |
| TaskBarAtBottom := true | |
| VisibleScreenHeight := A_ScreenHeight | |
| } | |
| else if (A_ScreenHeight - y <= h) { ; Taskhar shown (or in the move in/out animation) | |
| TaskBarAtBottom := true | |
| VisibleScreenHeight := y | |
| } | |
| else { | |
| TaskBarAtBottom := false | |
| VisibleScreenHeight:= A_ScreenHeight | |
| } | |
| } | |
| inGroup(GroupName, WinTitle := "A", WinText := "", ExcludeTitle := "", ExcludeText := "") | |
| { | |
| GroupIDs := WinGetList("ahk_group " GroupName) | |
| Window := WinExist(WinTitle, WinText, ExcludeTitle, ExcludeText) | |
| Loop GroupIDs.Length | |
| { | |
| if (GroupIDs[A_Index] = Window) | |
| { | |
| ;found match | |
| return Window | |
| } | |
| } | |
| return false | |
| } | |
| class Inverter | |
| { | |
| hTarget := "" | |
| hGui := "" | |
| hGui1 := "" | |
| hGui2 := "" | |
| hChildMagnifier := "" | |
| hChildMagnifier1 := "" | |
| hChildMagnifier2 := "" | |
| xPrev := 0 | |
| yPrev := 0 | |
| wPrev := 0 | |
| hPrev := 0 | |
| VisibleScreenHeightPrev := 0 | |
| stopped := 0 | |
| __New(hTarget) | |
| { | |
| DetectHiddenWindows true | |
| this.hTarget := hTarget | |
| DllCall("LoadLibrary", "str", "magnification.dll") | |
| DllCall("magnification.dll\MagInitialize") | |
| MAGCOLOREFFECT := Buffer(100, 0) | |
| Loop Parse Matrix, "|" | |
| NumPut("Float", A_LoopField, MAGCOLOREFFECT, (A_Index - 1) * 4) | |
| Loop 2 | |
| { | |
| gid := hTarget "_" A_Index | |
| MyGui := Gui(, gid,) | |
| if (A_Index = 2) | |
| MyGui.Opt("+AlwaysOnTop") ; needed for ZBID_UIACCESS | |
| ; +HWNDhGui%A_Index% | |
| MyGui.Opt("+DPIScale +toolwindow -Caption +E0x02000000 +E0x00080000 +E0x20") ; WS_EX_COMPOSITED := E0x02000000 WS_EX_LAYERED := E0x00080000 WS_EX_CLICKTHROUGH := E0x20 | |
| MyGui.Show("NA") | |
| this.hGui%A_Index%:= MyGui.Hwnd | |
| this.hChildMagnifier%A_Index% := DllCall("CreateWindowEx", "uint", 0, "str", "Magnifier", "str", "MagnifierWindow", "uint", WS_CHILD := 0x40000000, "int", 0, "int", 0, "int", 0, "int", 0, "ptr", this.hGui%A_Index%, "uint", 0, "ptr", DllCall("GetWindowLong" (A_PtrSize=8 ? "Ptr" : ""), "ptr", this.hGui%A_Index%, "int", GWL_HINSTANCE := -6 , "ptr"), "uint", 0, "ptr") | |
| DllCall("magnification.dll\MagSetColorEffect", "ptr", this.hChildMagnifier%A_Index%, "ptr", MAGCOLOREFFECT) | |
| } | |
| gid := hTarget "_" 2 | |
| this.hGui := this.hGui1 | |
| this.hChildMagnifier := this.hChildMagnifier1 | |
| return this | |
| } | |
| stop() { | |
| DetectHiddenWindows true | |
| if(!this.stopped) { | |
| this.stopped := 1 | |
| hGui := this.hGui | |
| WinHide "ahk_id " hGui | |
| hGui := this.hGui1 | |
| WinHide "ahk_id " hGui | |
| hGui := this.hGui2 | |
| WinHide "ahk_id " hGui | |
| hChildMagnifier := this.hChildMagnifier | |
| WinHide "ahk_id " hChildMagnifier | |
| hChildMagnifier := this.hChildMagnifier1 | |
| WinHide "ahk_id " hChildMagnifier | |
| hChildMagnifier := this.hChildMagnifier2 | |
| WinHide "ahk_id " hChildMagnifier | |
| } | |
| } | |
| start() { | |
| DetectHiddenWindows true | |
| if(this.stopped) { | |
| this.stopped := 0 | |
| hChildMagnifier := this.hChildMagnifier | |
| WinShow "ahk_id " hChildMagnifier | |
| hGui := this.hGui | |
| WinShow "ahk_id " hGui | |
| } | |
| } | |
| doit() | |
| { | |
| DetectHiddenWindows true | |
| hTarget := this.hTarget | |
| hGui := this.hGui | |
| hGui1 := this.hGui1 | |
| hGui2 := this.hGui2 | |
| hChildMagnifier := this.hChildMagnifier | |
| hChildMagnifier1 := this.hChildMagnifier1 | |
| hChildMagnifier2 := this.hChildMagnifier2 | |
| hideGui := "" | |
| WINDOWINFO := Buffer(60, 0) | |
| if (this.stopped or DllCall("GetWindowInfo", "ptr", hTarget, "ptr", WINDOWINFO) = 0) and (A_LastError = 1400) | |
| { | |
| ; xx("destroyed") | |
| return | |
| } | |
| if (NumGet(WINDOWINFO, 36, "uint") & 0x20000000) or !(NumGet(WINDOWINFO, 36, "uint") & 0x10000000) | |
| { | |
| ; minimized or not visible | |
| if (this.wPrev != 0) | |
| { | |
| WinHide "ahk_id " hGui | |
| this.wPrev := 0 | |
| } | |
| sleep 10 | |
| return 1 | |
| } | |
| x := NumGet(WINDOWINFO, 20, "int") | |
| y := NumGet(WINDOWINFO, 8, "int") | |
| w := NumGet(WINDOWINFO, 28, "int") - x | |
| h := NumGet(WINDOWINFO, 32, "int") - y | |
| move := 0 | |
| if (hGui = hGui1) and ((NumGet(WINDOWINFO, 44, "uint") = 1) or (DllCall("GetAncestor", "ptr", WinExist("A"), "uint", GA_ROOTOWNER := 3, "ptr") = hTarget)) | |
| { | |
| ; xx("activated") | |
| hGui := hGui2 | |
| hChildMagnifier := hChildMagnifier2 | |
| move := 1 | |
| hideGui := hGui1 | |
| } | |
| else if (hGui = hGui2) and (NumGet(WINDOWINFO, 44, "uint") != 1) and ((hr := DllCall("GetAncestor", "ptr", WinExist("A"), "uint", GA_ROOTOWNER := 3, "ptr")) != hTarget) and hr | |
| { | |
| ; deactivated | |
| hGui := hGui1 | |
| hChildMagnifier := hChildMagnifier1 | |
| WinMove x, y, w, h, "ahk_id " hGui | |
| WinMove 0, 0, w, h, "ahk_id " hChildMagnifier | |
| WinShow "ahk_id " hChildMagnifier | |
| DllCall("SetWindowPos", "ptr", hGui, "ptr", hTarget, "int", 0, "int", 0, "int", 0, "int", 0, "uint", 0x0040|0x0010|0x001|0x002) | |
| DllCall("SetWindowPos", "ptr", hTarget, "ptr", 1, "int", 0, "int", 0, "int", 0, "int", 0, "uint", 0x0040|0x0010|0x001|0x002) ; some windows can not be z-positioned before setting them to bottom | |
| DllCall("SetWindowPos", "ptr", hTarget, "ptr", hGui, "int", 0, "int", 0, "int", 0, "int", 0, "uint", 0x0040|0x0010|0x001|0x002) | |
| hideGui := hGui2 | |
| } | |
| else if (x != this.xPrev) or (y != this.yPrev) or (w != this.wPrev) or (h != this.hPrev) | |
| or (TaskBarAtBottom and (VisibleScreenHeight != this.VisibleScreenHeightPrev) and (y + h > VisibleScreenHeight)) | |
| { | |
| ; location changed or taskbar changed and is over window | |
| move := 1 | |
| } | |
| if(move) { | |
| ; never invert over a location that is used by the windows taskbar | |
| if (TaskBarAtBottom and (y + h > VisibleScreenHeight)) { | |
| h := VisibleScreenHeight - y ; escape taskbar | |
| } | |
| WinMove x, y, w, h, "ahk_id " hGui | |
| WinMove 0, 0, w, h, "ahk_id " hChildMagnifier | |
| WinShow "ahk_id " hChildMagnifier | |
| WinShow "ahk_id " hGui | |
| } | |
| if (A_PtrSize = 8) | |
| { | |
| RECT := Buffer(16, 0) | |
| NumPut("int", x, RECT, 0) | |
| NumPut("int", y, RECT, 4) | |
| NumPut("int", w, RECT, 8) | |
| NumPut("int", h, RECT, 12) | |
| DllCall("magnification.dll\MagSetWindowSource", "ptr", hChildMagnifier, "ptr", RECT) | |
| } | |
| else | |
| DllCall("magnification.dll\MagSetWindowSource", "ptr", hChildMagnifier, "int", x, "int", y, "int", w, "int", h) | |
| this.xPrev := x, this.yPrev := y, this.wPrev := w, this.hPrev := h, this.VisibleScreenHeightPrev := VisibleScreenHeight | |
| this.hChildMagnifier := hChildMagnifier | |
| this.hGui := hGui | |
| if hideGui | |
| { | |
| WinHide "ahk_id " hideGui | |
| hideGui := "" | |
| } | |
| return 1 | |
| } | |
| } | |
| ; Automatically turn on inversion filter | |
| loop | |
| { | |
| DetectHiddenWindows false ;would otherwise find many other windows | |
| aTarget := WinExist("A") | |
| if (aTarget != pTarget) { ; other window focused, check if it should be automatically inverted | |
| pTarget:= aTarget | |
| GroupIDs := WinGetList("ahk_group auto_invert_gp") | |
| ;Concat := "" | |
| ;For Each, Element In GroupIDs { | |
| ; If (Concat != "") { | |
| ; Concat .= "`n" | |
| ; } | |
| ; Concat .= Element ": " WinGetProcessName(Element) ;WinGetTitle(Element) | |
| ;} | |
| ;MsgBox Concat | |
| Loop GroupIDs.Length | |
| { | |
| hTarget := GroupIDs[A_Index] | |
| if (hTarget = aTarget) { ;currently focused window in auto_invert_gp group | |
| found:= 0 | |
| For index, tmp in Inverters { | |
| if(tmp.hTarget = hTarget) { | |
| found:= 1 | |
| Break | |
| } | |
| } | |
| if (found = 0) { | |
| ;MsgBox "Invert " hTarget " " WinGetProcessName(hTarget) | |
| hTargetName:= WinGetProcessName(hTarget) | |
| ignore:= 0 | |
| Loop AutoInvIgnProcName.Length | |
| { | |
| if AutoInvIgnProcName[A_Index] = hTargetName { | |
| ignore:= 1 | |
| Break | |
| } | |
| } | |
| if (ignore = 0) { | |
| ToggleInversion(hTarget) | |
| } | |
| } | |
| Break | |
| } | |
| } | |
| } | |
| ; Refresh all inverted windows | |
| first := true | |
| For index, tmp in Inverters { | |
| if(!tmp.stopped) { | |
| ; on first window refresh visible screen height | |
| if (first) { | |
| first := false | |
| refreshVisibleScreenHeight() | |
| } | |
| ret := tmp.doit() | |
| if(!ret) { | |
| tmp.stop() | |
| Inverters.removeAt(index) | |
| Break | |
| } | |
| } | |
| } | |
| } | |
| ToggleInversion(hTarget) { | |
| found:= 0 | |
| For index, tmp in Inverters { | |
| if(tmp.hTarget=hTarget) { | |
| found:= 1 | |
| if(tmp.stopped) { | |
| tmp.start() | |
| } | |
| else { | |
| tmp.stop() | |
| } | |
| Break | |
| } | |
| } | |
| if (found = 0) { | |
| tmp := Inverter(hTarget) | |
| Inverters.push(tmp) | |
| } | |
| } | |
| #i:: ;Win+i | |
| { | |
| ToggleInversion(WinExist("A")) | |
| } | |
| Uninitialize(ExitReason, ExitCode) | |
| { | |
| DllCall("magnification.dll\MagUninitialize") | |
| ExitApp | |
| } |
Beautiful script, thank you! I recently went on a Windows dark mode crusade and ended up installing SecureUXtheme and modifying my own .msstyles files to inject, just to get some Win32 apps to finally be dark - and there was an AHK script for it all along!
...I do see what that other commenter was saying about optimizing it, though. I have one program assigned to the auto invert group, and this script is eating up over 5% of my CPU (on a new gaming PC, no less), while the target program isn't even running! Is there a way to make this script idle more efficiently, or make a helper script call this script only when the target program is running? I thought it already had the latter feature implemented, but maybe not...
Try the fastest ways: other programs, such as Dark Any App, Easy Window Invert, negativescreen and etc
Well sure, but I'm on github because I like open source. Easy Invert is proprietary, negativescreen can't do individual apps as far as I can tell, and Dark Any App is proprietary and triggered my antivirus as soon as I tried to go to their website, which isn't encouraging
Anyway, I did create a helper script to call this one when the target window is created. I think it would be useful to have some logic in this script that detects when there are no longer any target windows existing, to kill the script. I can't figure out where or how to do that, though. I think it should go somewhere in one of the loops?
Hell yeah I figured it out (I'm a pretty novice programmer so I view this as an accomplishment)
At Line 328 insert:
invLen:= Inverters.Length
if(invLen = 0) {
Uninitialize(ExitApp,0)
}
It doesn't work properly:
I invert a window. When the window is inactive, it's inverted. Once I activate it, it turns back to normal.
No issue with the original script.
When the taskbar is hidden the script doesnt convert the area where the taskbar used to be. I'm assuming this is an oversight since most people don't have their taskbar set to auto hide? Otherwise great script, makes fusion 360 less of a pain to use! I just wish it worked on the whole screen.
@BillySp33d: A hidden taskbar is now recognized. That feature was not in the original script and yeah i never hide my taskbar 😄 What still doesn't work is inverting below the taskbar when you have a 2nd monitor that goes below the taskbar (that hasn't worked before).
And i enabled per-monitor DPI awareness, so you could have monitors with different DPI settings or when you change the DPI scaling in windows you don't need to restart the script any more.
@charlie89 Thank you. This helps a lot :)
Looks like the inGroup() function is redundant and can be removed.
The text on my pc (acer EX215-31 win11 24h2) appears to be blurred in all apps I've tried (hh.exe, everything.exe, and AHK msgbox).
See example below with AHK help file showing comparison between this script with MatrixHigh dark mode (Feb 18 2025 version) and in-built dark mode.
Is there a way to correct this text blurring?

Hi,
yeah AI is currently still very bad with autohotkey scripts, i also made that experience. There is a v1 and a v2 syntax, and there are a few similar programs (i.e. there is a version which runs on linux) which aim to be "syntax-compatible" but have partially different syntax, and i guess the AI can't tell the difference of all that.
Do you have performance problems? Because for me it runs just fine on a i5-9500M mobile cpu and uses maybe 2-3% cpu when inverting a few windows. I still don't fully understand the code in the Inverter class (i just took it from the mentioned link and changed it from v1 to v2 syntax), so it's difficult for me to improve anything here.
What i want to improve when i have time for it:
Smart Inversionfilter) doesn't invert the smoothed part of the fonts and that makes text very hard to read. I use a windows VM at home which seems to disable that smoothing so i only noticed that at work.To your suggestions (which sound like they come from an AI..?):
use Widget instead of WinExist: i will look it up.cache the results of calling DllCall("GetWindowInfo", ...): it's only called once and writes in the WINDOWINFO buffer, so i don't see how i could cache something there. Do you have a code example?more efficient algorithm to search for an inverter: Do you have a code example? Because i don't know a faster syntax. Also usually there are only a few inverted windows at max, so looping over those few should still be plenty fast. Or do you have a use-case to invert thousands of windows at once?use DllCall("magnification.dll\MagUninitialize") only if the inverters are actually used: Do you restart that script every few seconds? I mean i see your point, but the usual use-case would probably be to start the script through Windows autostart and it gets closed when you shutdown the PC, that single Dll call shouldn't matter at all. And if one doesn't remove the 'ahk_class #32770' from the script then to 99% there was a inverter used because very many programs (not only windows, alsonotepad++or the modernvisual studio code) still have dialogs with class #32770.