亮度色温调节__草稿

#SingleInstance force
#NoEnv
SetBatchLines, -1



;20210507_212043   By  jly
;通过滑块 调节显示器亮度 色温


;SetWorkingDir %A_ScriptDir%
;#Include %A_ScriptDir%\Monitor.ahk
;SetWinDelay, -1

Map_idx_Brightness := {}              ;获取显示器原始的亮度和伽马值. 退出脚本时 回复原状.
SysGet, OutputVar, MonitorCount
Loop % OutputVar
	Map_idx_Brightness[A_Index] := {B:Monitor.GetBrightness("\\.\DISPLAY" A_Index)["Current"], G:Monitor.GetGammaRamp("\\.\DISPLAY" A_Index)}

k_default := 4100
Brightness_Gamma_Default := 70


Gap_Col := 10
Gap_Row := 10
yb1 := 150
xb := 30
yb := 30

Wt := 65
Ws := 300
Hs := 30
Hs2 := 40

wg := 600
hg := 400

gui, +hwndhg1 -DPIScale
gui, color,white

yn := yb1
Nm1 := "TextA1"
Gui, Add, Text,% " x"xb " y"yn " w"Wt " h"Hs2 " v"Nm1 " Right "  , % "色温(K)"

Nm1 := "MySlider1"                         ;色温
Gui, Add, Slider,% " x"xb+Wt+Gap_Col " y"yn " w"Ws " h"Hs2 " v"Nm1 " g"Nm1 " hwndh"Nm1 " Range500-20000 Line50 Page100  AltSubmit  TickInterval100  ToolTip ", % k_default

Nm1 := "TextB1"
Gui, Add, Text,% " x"xb+Wt+Ws+Gap_Col*2 " y"yn " w"Wt " h"Hs2 " v"Nm1 " hwndh"Nm1   ,% k_default



;yn := yb1+Hs
yn := yb1+Hs*2 + Gap_Row*2
Nm1 := "TextA2"
Gui, Add, Text,% " x"xb " y"yn " w"Wt " h"Hs2 " v"Nm1 " Right "   , % "亮度(%)"
Nm1 := "MySlider2"                         ;显示器 物理亮度 百分比
Gui, Add, Slider,% " x"xb+Wt+Gap_Col " y"yn " w"Ws " h"Hs2 " v"Nm1 " g"Nm1 " hwndh"Nm1 " AltSubmit  TickInterval2  ToolTip", % Map_idx_Brightness[1].B
Nm1 := "TextB2"
Gui, Add, Text,% " x"xb+Wt+Ws+Gap_Col*2 " y"yn " w"Wt/2 " h"Hs2 " v"Nm1 " hwndh"Nm1   ,% Map_idx_Brightness[1].B

Nm1 := "TextC2"
Gui, Add, Text,% " x"xb+Wt+Wt/2+Ws+Gap_Col*3 " y"yn " w"Wt " h"Hs2 " v"Nm1 " hwndh"Nm1   ,% "物理调节"

;yn := yb1+Hs*2
yn := yb1+Hs + Gap_Row
Nm1 := "TextA3"
Gui, Add, Text,% " x"xb " y"yn " w"Wt " h"Hs2 " v"Nm1 " Right "   , % "亮度(%)"
Nm1 := "MySlider3"                         ;伽马百分比    通过调节伽马值百分比,调节屏幕亮度
Gui, Add, Slider,% " x"xb+Wt+Gap_Col " y"yn " w"Ws " h"Hs2 " v"Nm1 " g"Nm1 " hwndh"Nm1 " AltSubmit TickInterval2 ToolTip", % Brightness_Gamma_Default
Nm1 := "TextB3"
Gui, Add, Text,% " x"xb+Wt+Ws+Gap_Col*2 " y"yn " w"Wt/2 " h"Hs2 " v"Nm1 " hwndh"Nm1   ,% Brightness_Gamma_Default
Nm1 := "TextC3"
Gui, Add, Text,% " x"xb+Wt+Wt/2+Ws+Gap_Col*3 " y"yn " w"Wt " h"Hs2 " v"Nm1 " hwndh"Nm1   ,% "伽马调节"

gui,show,% " w"wg " h"hg, 亮度\色温调节

;GuiControlGet, MySlider1
;GuiControlGet, MySlider3
;f_SetGammaRamp(MySlider1, MySlider3)
f_SetGammaRamp(k_default, Brightness_Gamma_Default)     ;

OnExit("f_restore_Monitor")
return

MySlider1:                                       ;色温
	GuiControlGet, MySlider3
	GuiControl, Text, %hTextB1% , % MySlider1
	f_SetGammaRamp(MySlider1, MySlider3)
return

MySlider3:                                       ;伽马值百分比
	GuiControl, Text, %hTextB3% , % MySlider3
	GuiControlGet, MySlider1
	f_SetGammaRamp(MySlider1, MySlider3)
return

MySlider2:                                         ;显示器 物理亮度 百分比
	f_set_Brightness()
	SetTimer,MySlider2_post,-10
return
MySlider2_post:
	GuiControlGet, TextB2
	if(MySlider2!=TextB2)
	{
		f_set_Brightness()
	}
return



f_SetGammaRamp(MySlider1, MySlider3)
{
	global
	m := f_kelvin_to_RGB(MySlider1)               ;色温 -> RGB(伽马值)
	f_deal_GammaRamp(m,MySlider3)                 ;RGB  -> RGB x 百分比    通过调节伽马值百分比,调节屏幕亮度
	for k, v in Map_idx_Brightness
		Monitor.SetGammaRamp(m.Red, m.Green, m.Blue, Display := "\\.\DISPLAY" k)   ;设置各个显示器的伽马值
}

f_deal_GammaRamp(ByRef m,MySlider3)
{
	for k,v in m
		m[k] := (v)*MySlider3/100-128
}

f_set_Brightness()
{
	global
	GuiControl, Text, %hTextB2% , % MySlider2
	for k, v in Map_idx_Brightness
		Monitor.SetBrightness(MySlider2, "\\.\DISPLAY" k)
}



;#if f_is_over_MySlider()
;影响音量调节(鼠标在屏幕顶部、底部时调节音量)。
;如果使用#if(两者条件不同时) 则音量调节问题可以解决。
;但是,不知道为什么用#if 设置 WheelUp/Down 快捷键,快速调整物理亮度的时候,会卡顿明显(用#if会耗费一定的时间??)!!!
$WheelUp::
	MouseGetPos,,,h_win,h_ctrl,2
	if(h_win=hg1)
	{
		if(h_ctrl=hMySlider1)
		{
			WinActivate,ahk_id %hg1%
			GuiControl, Focus,% hMySlider1
			Send,{Right}
			;Send,{PgDn}
		}else if(h_ctrl=hMySlider2 or h_ctrl=hMySlider3)
			Send,{WheelDown}
		else
			Send {WheelUp}
			;SendRaw {WheelUp}
	}
	else Send {WheelUp}
	;else SendRaw {WheelUp}
return
$WheelDown::
	MouseGetPos,,,h_win,h_ctrl,2
	if(h_win=hg1)
	{
		if(h_ctrl=hMySlider1)
		{
			WinActivate,ahk_id %hg1%
			GuiControl, Focus,% hMySlider1
			Send,{Left}
			;Send,{PgUp}
		}else if(h_ctrl=hMySlider2 or h_ctrl=hMySlider3)
			Send,{WheelUp}
		else
			Send {WheelDown}
			;SendRaw {WheelDown}
	}
	else Send {WheelDown}
	;else SendRaw {WheelDown}
return



;#if
f_is_over_MySlider(){
	global
	MouseGetPos,,,h_win,clssNN
	if(h_win=hg1 )
		return 1
	return 0
}



;$WheelUp::Send,{WheelDown}
;$WheelDown::Send,{WheelUp}


/*
;不知道为什么用#if 设置 WheelUp/Down 快捷键,快速调整物理亮度的时候,会卡顿明显!!!
#if f_is_over_MySlider()
$WheelUp::Send,{WheelDown}
$WheelDown::Send,{WheelUp}
#if
f_is_over_MySlider()
{
	global
	;return 1
	;/*
	MouseGetPos,,,h_win,clssNN
	;if(h_win=hg1 and InStr(clssNN,"msctls_trackbar"))
	if(h_win=hg1 )
		return 1
	return 0
	;*/
}
*/





f_kelvin_to_RGB(kelvin := 6500)
{
	kelvin := (kelvin < 1000) ? 1000 : (kelvin > 40000) ? 40000 : kelvin
	kelvin /= 100

	if (kelvin <= 66) {
		red   := 255
	} else {
		red   := 329.698727446 * ((kelvin - 60) ** -0.1332047592)
		red   := (red < 0) ? 0 : (red > 255) ? 255 : red
	}

	if (kelvin <= 66) {
		green := 99.4708025861 * Ln(kelvin) - 161.1195681661
		green := (green < 0) ? 0 : (green > 255) ? 255 : green
	} else {
		green := 288.1221695283 * ((kelvin - 60) ** -0.0755148492)
		green := (green < 0) ? 0 : (green > 255) ? 255 : green
	}

	if (kelvin >= 66) {
		blue  := 255
	} else if (kelvin <= 19) {
		blue  := 0
	} else {
		blue  := 138.5177312231 * Ln(kelvin - 10) - 305.0447927307
		blue  := (blue < 0) ? 0 : (blue > 255) ? 255 : blue
	}

	GammaRamp := []
	GammaRamp["Red"]   := red
	GammaRamp["Green"] := green
	GammaRamp["Blue"]  := blue
	return GammaRamp
}

f_restore_Monitor()
{
	global
	for k, v in Map_idx_Brightness
	{
		Monitor.SetBrightness(v.B, "\\.\DISPLAY" k)
		Monitor.SetGammaRamp(v.G.Red, v.G.Green, v.G.Blue, Display := "\\.\DISPLAY" k)
	}
}


/*
;亮度
a1=
for k, v in Map_idx_Brightness
	a1 .=  k ": " v "`n"
	MsgBox % a1
return
*/


esc::
	;gui,show,Hide
	WinHide,ahk_id %hg1%
	f_restore_Monitor()
	ExitApp
return








;[Class] Monitor (Brightness, Contrast, Gamma Ramp)       https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7854&hilit=brightness
;[Class] Monitor (Brightness, Contrast, Gamma Ramp)
;https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7854&hilit=brightness
; ===============================================================================================================================
; AutoHotkey wrapper for Monitor Configuration API Functions
;
; Author ....: jNizM
; Released ..: 2015-05-26
; Modified ..: 2020-07-29
; Github ....: https://github.com/jNizM/Class_Monitor
; Forum .....: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=62955
; ===============================================================================================================================


class Monitor
{

	; ===== PUBLIC METHODS ======================================================================================================

	GetBrightness(Display := "")
	{
		if (hMonitor := this.GetMonitorHandle(Display))
		{
			PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
			hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
			Brightness := this.GetMonitorBrightness(hPhysicalMonitor)
			this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
			return Brightness
		}
		return false
	}


	GetContrast(Display := "")
	{
		if (hMonitor := this.GetMonitorHandle(Display))
		{
			PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
			hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
			Contrast := this.GetMonitorContrast(hPhysicalMonitor)
			this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
			return Contrast
		}
		return false
	}


	GetGammaRamp(Display := "")
	{
		if (DisplayName := this.GetDisplayName(Display))
		{
			if (hDC := this.CreateDC(DisplayName))
			{
				GammaRamp := this.GetDeviceGammaRamp(hDC)
				this.DeleteDC(hDC)
				return GammaRamp
			}
			this.DeleteDC(hDC)
		}
		return false
	}


	RestoreFactoryDefault(Display := "")
	{
		if (hMonitor := this.GetMonitorHandle(Display))
		{
			PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
			hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
			this.RestoreMonitorFactoryDefaults(hPhysicalMonitor)
			this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
			return true
		}
		return false
	}


	SetBrightness(Brightness, Display := "")
	{
		if (hMonitor := this.GetMonitorHandle(Display))
		{
			PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
			hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
			GetBrightness    := this.GetMonitorBrightness(hPhysicalMonitor)
			Brightness := (Brightness < GetBrightness["Minimum"]) ? GetBrightness["Minimum"]
						: (Brightness > GetBrightness["Maximum"]) ? GetBrightness["Maximum"]
						: (Brightness)
			this.SetMonitorBrightness(hPhysicalMonitor, Brightness)
			this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
			return Brightness
		}
		return false
	}


	SetContrast(Contrast, Display := "")
	{
		if (hMonitor := this.GetMonitorHandle(Display))
		{
			PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
			hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
			GetContrast      := this.GetMonitorContrast(hPhysicalMonitor)
			Contrast := (Contrast < GetContrast["Minimum"]) ? GetContrast["Minimum"]
					  : (Contrast > GetContrast["Maximum"]) ? GetContrast["Maximum"]
					  : (Contrast)
			this.SetMonitorContrast(hPhysicalMonitor, Contrast)
			this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
			return Contrast
		}
		return false
	}


	SetGammaRamp(Red, Green, Blue, Display := "")
	{
		if (DisplayName := this.GetDisplayName(Display))
		{
			if (hDC := this.CreateDC(DisplayName))
			{
				this.SetDeviceGammaRamp(hDC, Red, Green, Blue)
				this.DeleteDC(hDC)
				return true
			}
			this.DeleteDC(hDC)
		}
		return false
	}


	; ===== PRIVATE METHODS =====================================================================================================

	CreateDC(DisplayName)
	{
		if (hDC := DllCall("gdi32\CreateDC", "str", DisplayName, "ptr", 0, "ptr", 0, "ptr", 0, "ptr"))
			return hDC
		return false
	}


	DeleteDC(hDC)
	{
		if (DllCall("gdi32\DeleteDC", "ptr", hDC))
			return true
		return false
	}


	DestroyPhysicalMonitors(PhysicalMonitorArraySize, PHYSICAL_MONITOR)
	{
		if (DllCall("dxva2\DestroyPhysicalMonitors", "uint", PhysicalMonitorArraySize, "ptr", &PHYSICAL_MONITOR))
			return true
		return false
	}


	EnumDisplayMonitors(hMonitor := "")
	{
		static EnumProc := RegisterCallback(Monitor.EnumProc)
		static DisplayMonitors := {}

		if (MonitorNumber = "")
			DisplayMonitors := {}

		if (DisplayMonitors.MaxIndex() = "")
			if (DllCall("user32\EnumDisplayMonitors", "ptr", 0, "ptr", 0, "ptr", EnumProc, "ptr", &DisplayMonitors, "uint"))
				return (MonitorNumber = "") ? DisplayMonitors : DisplayMonitors.HasKey(MonitorNumber) ? DisplayMonitors[MonitorNumber] : false
		return false
	}


	EnumProc(hDC, RECT, ObjectAddr)
	{
		DisplayMonitors := Object(ObjectAddr)
		MonitorInfo := Monitor.GetMonitorInfo(this)
		DisplayMonitors.Push(MonitorInfo)
		return true
	}


	GetDeviceGammaRamp(hMonitor)
	{
		VarSetCapacity(GAMMA_RAMP, 1536, 0)
		if (DllCall("gdi32\GetDeviceGammaRamp", "ptr", hMonitor, "ptr", &GAMMA_RAMP))
		{
			GammaRamp := []

			GammaRamp["Red"]   := NumGet(GAMMA_RAMP,        2, "ushort") - 128
			GammaRamp["Green"] := NumGet(GAMMA_RAMP,  512 + 2, "ushort") - 128
			GammaRamp["Blue"]  := NumGet(GAMMA_RAMP, 1024 + 2, "ushort") - 128


			;GammaRamp["Red"]   := NumGet(GAMMA_RAMP,        2, "ushort")
			;GammaRamp["Green"] := NumGet(GAMMA_RAMP,  512 + 2, "ushort")
			;GammaRamp["Blue"]  := NumGet(GAMMA_RAMP, 1024 + 2, "ushort")

			return GammaRamp
		}
		return false
	}


	GetDisplayName(Display := "")
	{
		DisplayName := ""

		if (Enum := this.EnumDisplayMonitors()) && (Display != "")
		{
			for k, Mon in Enum
				if (InStr(Mon["Name"], Display))
					DisplayName := Mon["Name"]
		}
		if (DisplayName = "")
			if (hMonitor := this.MonitorFromWindow())
				DisplayName := this.GetMonitorInfo(hMonitor)["Name"]

		return DisplayName
	}


	GetMonitorBrightness(hMonitor)
	{
		if (DllCall("dxva2\GetMonitorBrightness", "ptr", hMonitor, "uint*", Minimum, "uint*", Current, "uint*", Maximum))
			return { "Minimum": Minimum, "Current": Current, "Maximum": Maximum }
		return false
	}


	GetMonitorContrast(hMonitor)
	{
		if (DllCall("dxva2\GetMonitorContrast", "ptr", hMonitor, "uint*", Minimum, "uint*", Current, "uint*", Maximum))
			return { "Minimum": Minimum, "Current": Current, "Maximum": Maximum }
		return false
	}


	GetMonitorHandle(Display := "")
	{
		hMonitor := 0

		if (Enum := this.EnumDisplayMonitors()) && (Display != "")
		{
			for k, Mon in Enum
				if (InStr(Mon["Name"], Display))
					hMonitor := Mon["Handle"]
		}
		if !(hMonitor)
			hMonitor := this.MonitorFromWindow()

		return hMonitor
	}


	GetMonitorInfo(hMonitor)
	{
		NumPut(VarSetCapacity(MONITORINFOEX, 40 + (32 << !!A_IsUnicode)), MONITORINFOEX, 0, "uint")
		if (DllCall("user32\GetMonitorInfo", "ptr", hMonitor, "ptr", &MONITORINFOEX))
		{
			MONITORINFO := []
			MONITORINFO["Handle"]   := hMonitor
			MONITORINFO["Name"]     := Name := StrGet(&MONITORINFOEX + 40, 32)
			MONITORINFO["Number"]   := RegExReplace(Name, ".*(\d+)$", "$1")
			MONITORINFO["Left"]     := NumGet(MONITORINFOEX,  4, "int")
			MONITORINFO["Top"]      := NumGet(MONITORINFOEX,  8, "int")
			MONITORINFO["Right"]    := NumGet(MONITORINFOEX, 12, "int")
			MONITORINFO["Bottom"]   := NumGet(MONITORINFOEX, 16, "int")
			MONITORINFO["WALeft"]   := NumGet(MONITORINFOEX, 20, "int")
			MONITORINFO["WATop"]    := NumGet(MONITORINFOEX, 24, "int")
			MONITORINFO["WARight"]  := NumGet(MONITORINFOEX, 28, "int")
			MONITORINFO["WABottom"] := NumGet(MONITORINFOEX, 32, "int")
			MONITORINFO["Primary"]  := NumGet(MONITORINFOEX, 36, "uint")
			return MONITORINFO
		}
		return false
	}


	GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
	{
		if (DllCall("dxva2\GetNumberOfPhysicalMonitorsFromHMONITOR", "ptr", hMonitor, "uint*", NumberOfPhysicalMonitors))
			return NumberOfPhysicalMonitors
		return false
	}


	GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitorArraySize, ByRef PHYSICAL_MONITOR)
	{
		VarSetCapacity(PHYSICAL_MONITOR, (A_PtrSize + 256) * PhysicalMonitorArraySize, 0)
		if (DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR", "ptr", hMonitor, "uint", PhysicalMonitorArraySize, "ptr", &PHYSICAL_MONITOR))
			return NumGet(PHYSICAL_MONITOR, 0, "ptr")
		return false
	}


	MonitorFromWindow(hWindow := 0)
	{
		static MONITOR_DEFAULTTOPRIMARY := 0x00000001

		if (hMonitor := DllCall("user32\MonitorFromWindow", "ptr", hWindow, "uint", MONITOR_DEFAULTTOPRIMARY))
			return hMonitor
		return false
	}


	RestoreMonitorFactoryDefaults(hMonitor)
	{
		if (DllCall("dxva2\RestoreMonitorFactoryDefaults", "ptr", hMonitor))
			return false
		return true
	}


	SetDeviceGammaRamp(hMonitor, Red, Green, Blue)
	{
		loop % VarSetCapacity(GAMMA_RAMP, 1536, 0) / 6
		{
			NumPut((r := (red   + 128) * (A_Index - 1)) > 65535 ? 65535 : r, GAMMA_RAMP,        2 * (A_Index - 1), "ushort")
			NumPut((g := (green + 128) * (A_Index - 1)) > 65535 ? 65535 : g, GAMMA_RAMP,  512 + 2 * (A_Index - 1), "ushort")
			NumPut((b := (blue  + 128) * (A_Index - 1)) > 65535 ? 65535 : b, GAMMA_RAMP, 1024 + 2 * (A_Index - 1), "ushort")
		}
		if (DllCall("gdi32\SetDeviceGammaRamp", "ptr", hMonitor, "ptr", &GAMMA_RAMP))
			return true
		return false
	}


	SetMonitorBrightness(hMonitor, Brightness)
	{
		if (DllCall("dxva2\SetMonitorBrightness", "ptr", hMonitor, "uint", Brightness))
			return true
		return false
	}


	SetMonitorContrast(hMonitor, Contrast)
	{
		if (DllCall("dxva2\SetMonitorContrast", "ptr", hMonitor, "uint", Contrast))
			return true
		return false
	}

}

; ===============================================================================================================================



return

给TA捐赠
共{{data.count}}人
人已捐赠
其他

利用ACC和UIA获取文本

2021-12-2 16:03:17

其他

录账烟草

2021-12-2 16:06:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索