AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Solved] Brightness(Gamma) Control
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
heresy



Joined: 11 Mar 2008
Posts: 291

PostPosted: Wed May 21, 2008 7:27 am    Post subject: [Solved] Brightness(Gamma) Control Reply with quote

I'm trying to success my long hope. Lexikos adviced me on this issue
basically i'm trying to use these two API GetDeviceGammaRamp & SetDeviceGammaRamp
Code:
;from MSDN
;lpRamp : Pointer to a buffer containing the gamma ramp to be set. The gamma ramp is specified in three arrays of 256 WORD elements each, which contain the mapping between RGB values in the frame buffer and digital-analog-converter (DAC) values. The sequence of the arrays is red, green, blue. The RGB values must be stored in the most significant bits of each WORD to increase DAC independence.

BOOL WINAPI GetDeviceGammaRamp(
  HDC hDC,
  LPVOID lpRamp
);

BOOL WINAPI SetDeviceGammaRamp(
  HDC hDC,
  LPVOID lpRamp
);
my poorly try is here
Code:
hDC := DllCall("GetDC", "Int", 0)
VarSetCapacity(Gamma, 1536)
Result := DllCall("GetDeviceGammaRamp", "UInt", hdc, "UInt", &Gamma)
it returns 1 which is boolen True but i don't get the values in Gamma variable
i really don't get the numget/numput things.
but can anyone make me advance on this issue? i'm trying this simple dllcall for over a month Sad
hope i get advance this time.
_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com


Last edited by heresy on Tue Jun 10, 2008 12:26 pm; edited 1 time in total
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 3244
Location: Simi Valley, CA

PostPosted: Wed May 21, 2008 11:30 am    Post subject: Reply with quote

Code:
hDC := DllCall("GetDC", "UInt", 0)
VarSetCapacity(Gamma, 256 * 3 * 2)
Result := DllCall("GetDeviceGammaRamp", "UInt", hdc, "UInt", &Gamma)
loop 768
   result .= (Mod(A_Index-1, 16) ? "`t" : "`n") NumGet(Gamma, 2*A_Index-1, "UShort")
msgbox % result

Though I'm not sure what you'd do with such a thing...
_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
heresy



Joined: 11 Mar 2008
Posts: 291

PostPosted: Wed May 21, 2008 1:15 pm    Post subject: Reply with quote

thanks for answer [VxE] and hi again.
you're the only answerer for this issue for past one month Sad
as i told you, my goal is controlling brightness
i thought like GetDeviceGammaRamp would store 'current gamma' into variable.
but it seems not. what should i pass to SetDeviceGammaRamp then?
Code:
hDC := DllCall("GetDC", "UInt", 0)
VarSetCapacity(Gamma, 256 * 3 * 2)
Result := DllCall("GetDeviceGammaRamp", "UInt", hdc, "UInt", &Gamma)
loop 768
   result .= (Mod(A_Index-1, 16) ? "`t" : "`n") NumGet(Gamma, 2*A_Index-1, "UShort")
msgbox % result
NewGamma := NumGet(Gamma, 2*768-1, "UShort")
Result := DllCall("SetDeviceGammaRamp", "UInt", hdc, "UInt", &NewGamma)
msgbox % result ;failed
although i don't fully understand Numget/Numput things i was trying to get the value at the end of array and pass to SetDeviceGammaRamp
but failed. i just can't believe that we don't have wrapper for this. Sad
_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Wed May 21, 2008 4:22 pm    Post subject: Reply with quote

There are various ways to alter the gamma ramp, but what nirsoft does is this:
Code:
br := 128   ; brightness, in the range of 0 - 255, where 128 is normal
VarSetCapacity(gr, 512*3)
Loop,   256
{
   If  (nValue:=(br+128)*(A_Index-1))>65535
        nValue:=65535
   NumPut(nValue, gr,      2*(A_Index-1), "Ushort")
   NumPut(nValue, gr,  512+2*(A_Index-1), "Ushort")
   NumPut(nValue, gr, 1024+2*(A_Index-1), "Ushort")
}
hDC := DllCall("GetDC", "Uint", 0)
DllCall("SetDeviceGammaRamp", "Uint", hDC, "Uint", &gr)
DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 3244
Location: Simi Valley, CA

PostPosted: Wed May 21, 2008 9:37 pm    Post subject: Reply with quote

lulz:
Code:
br := 128   ; brightness, in the range of 0 - 255, where 128 is normal
#WheelUp::
#WheelDown::
br += (InStr(A_ThisHotkey, "down") ? -8 : 8 )
If ( br > 256 )
   br := 256
If ( br < 0 )
   br := 0
VarSetCapacity(gr, 512*3)
Loop,   256
{
   If  (nValue:=(br+128)*(A_Index-1))>65535
        nValue:=65535
   NumPut(nValue, gr,      2*(A_Index-1), "Ushort")
   NumPut(nValue, gr,  512+2*(A_Index-1), "Ushort")
   NumPut(nValue, gr, 1024+2*(A_Index-1), "Ushort")
}
hDC := DllCall("GetDC", "Uint", 0)
DllCall("SetDeviceGammaRamp", "Uint", hDC, "Uint", &gr)
DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)
return

so that's how it works Razz
_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Thu May 22, 2008 1:01 am    Post subject: Reply with quote

BTW, I was not aware of (G)SetDeviceGammaRamp API before. I only knew IDirectDrawGammaControl object which I suspect the above APIs are wrapping.
Back to top
View user's profile Send private message
heresy



Joined: 11 Mar 2008
Posts: 291

PostPosted: Thu May 22, 2008 6:04 am    Post subject: Reply with quote

Thanks for the answer Sean and welcome back.

that's exactly what i was trying for past few weeks. it works perfectly!
now we have wrapper for Gamma Ramp. Smile
this thread will be referenced many times from now as an answer thread for all the brightness control questions
i just couldn't believe that nobody was interesting on brightness control since autohotkey's born
once again, thanks for your help almighty Sean and thanks to [VxE] too.
--------
for studying purpose.
i coudln't get the Loop part. i just don't get that where the 65535 came from.
i mean why is it 256*256-1? if anyone can enlighten me on this. please
_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com
Back to top
View user's profile Send private message
Guest






PostPosted: Thu May 22, 2008 1:30 pm    Post subject: Reply with quote

heresy wrote:
i just don't get that where the 65535 came from. i mean why is it 256*256-1?

65535, 0xFFFF in hex, is the highest possible unsigned short integer. If omitting the step, the actually assigned values for integers greater than 65535 will be totally different (lower) ones as the hi-word part will be truncated when casted into ushort. E.g., 65536 will be 0, 65537 will be 1 etc. I think this will result in failure.
Back to top
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Thu May 22, 2008 1:31 pm    Post subject: Reply with quote

The above post was by me.
Back to top
View user's profile Send private message
heresy



Joined: 11 Mar 2008
Posts: 291

PostPosted: Thu May 22, 2008 3:29 pm    Post subject: Reply with quote

Thanks for your help Sean
i didn't aware of that because i haven't experienced static typing languages before
much appreciated and it was good to see you coming back
_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Thu May 22, 2008 11:40 pm    Post subject: Reply with quote

heresy wrote:
Thanks for your help Sean
i didn't aware of that because i haven't experienced static typing languages before
much appreciated and it was good to see you coming back

I can't stay in front of computer for long now, so I'm dubious that I'll be able to visit the forum frequently. Anyway thanks for the warm welcome.
Back to top
View user's profile Send private message
pojitonov
Guest





PostPosted: Sat Nov 14, 2009 10:52 pm    Post subject: Reply with quote

I added a progress bar to change the brightness, but I have it slightly wrong shows progress, who can help

Code:
#NoTrayIcon    ; не отображать иконку скрипта в трее
#MaxHotkeysPerInterval 200
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
SetBatchLines -1

br := 128   ; brightness, in the range of 0 - 255, where 128 is normal

#F2::
#F1::
Brght_Step = 8

; -------------------
; START CONFIGURATION
; -------------------
; The percentage by which to raise or lower the volume each time
; How long to display the volume level bar graphs (in milliseconds)
vol_DisplayTime = 1500
; Transparency of window (0-255)
vol_TransValue = 200
; Bar's background colour
vol_CW = EEEEEE   
vol_Width = 200  ; width of bar
vol_Thick = 20   ; thickness of bar
; Bar's screen position
vol_PosX := A_ScreenWidth/2 - vol_Width/2
vol_PosY := A_ScreenHeight/1.8 - vol_Thick/2
; --------------------
; END OF CONFIGURATION
; --------------------
vol_BarOptionsMaster = 1:B1 ZH%vol_Thick% ZX8 ZY4 W%vol_Width% X%vol_PosX% Y%vol_PosY% CW%vol_CW%


br += (InStr(A_ThisHotkey, "F1") ? -Brght_Step : Brght_Step )
If ( br > 256 )
   br := 256
If ( br < 0)
   br := 0
VarSetCapacity(gr, 512*3)
Loop,   256
{
   If  (nValue:=(br+128)*(A_Index-1))>65535
        nValue:=65535
   NumPut(nValue, gr,      2*(A_Index-1), "Ushort")
   NumPut(nValue, gr,  512+2*(A_Index-1), "Ushort")
   NumPut(nValue, gr, 1024+2*(A_Index-1), "Ushort")
}
hDC := DllCall("GetDC", "Uint", 0)
DllCall("SetDeviceGammaRamp", "Uint", hDC, "Uint", &gr)
DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)


vol_ShowBars:
; Get volumes in case the user or an external program changed them:
vol_Master := br
if vol_Master <> 128
{
  vol_Colour = Blue   
  vol_Text = Brightness
}
else
{
;  vol_Colour = Red
  vol_Text = Brightness (normal)

}


; To prevent the "flashing" effect, only create the bar window if it doesn't already exist:
IfWinNotExist, BrightnessOSDxyz
{
    Progress, %vol_BarOptionsMaster% CB%vol_Colour% CT%vol_Colour%, , %vol_Text%, BrightnessOSDxyz
    WinSet, Transparent, %vol_TransValue%, BrightnessOSDxyz
}


Progress, 1:%vol_Master% , , %vol_Text%
SetTimer, vol_BarOff, %vol_DisplayTime%
return


vol_BarOff:
SetTimer, vol_BarOff, off
Progress, 1:Off
return
Back to top
awannaknow



Joined: 14 Jun 2009
Posts: 324

PostPosted: Mon Apr 05, 2010 6:36 pm    Post subject: Reply with quote

Just what I was looking for :
How to solve my Gamma problem.
Control Gamma's monitor with AHK
Spent much time to find a way to do it.
Thank you a lot.
It's far above my head.

I tried to change the bar color to show blue when below 128

Red when above
Green when 128 (Normal)
Add a F1 hotkey to reset to normal 128
Changes F1 F2 hotkey to - +
All those 3 shortcuts are the default PowerStrip Shortcut.
Failed in everything.
But anyways it'll do what's needed : control Gamma.


GGG RRR EEE AAA TTT ! ! !
Yeah Razz
Back to top
View user's profile Send private message
Murx
Guest





PostPosted: Mon Apr 05, 2010 7:53 pm    Post subject: Reply with quote

Will this provide the option/configuration to display the desktop in greyscale, if yes - how?
Back to top
awannaknow



Joined: 14 Jun 2009
Posts: 324

PostPosted: Tue Apr 06, 2010 5:23 am    Post subject: Reply with quote

Well, I did change the progress bar color to
Blue when below 128
Green when 128 (Normal)
Red when above
And Hotkeys are
F1:: ; reduce the brightness F1
F2:: ; increase the brightness F2
here is the code :
Code:

;#NoTrayIcon    don't display the script icon in the tray
#MaxHotkeysPerInterval 200
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force



SetBatchLines -1



br := 128   ; brightness, in the range of 0 - 255, where 128 is normal

F1:: ; reduce the brightness  F1
F2:: ; increase the brightness F2
Brght_Step = 8

; -------------------
; START CONFIGURATION
; -------------------
; The percentage by which to raise or lower the volume each time
; How long to display the volume level bar graphs (in milliseconds)
vol_DisplayTime = 1500
; Transparency of window (0-255)
vol_TransValue = 200
; Bar's background colour
vol_CW = EEEEEE   
vol_Width = 200  ; width of bar
vol_Thick = 20   ; thickness of bar
; Bar's screen position
vol_PosX := A_ScreenWidth/2 - vol_Width/2
vol_PosY := A_ScreenHeight/1.8 - vol_Thick/2
; --------------------
; END OF CONFIGURATION
; --------------------
vol_BarOptionsMaster = 1:B1 ZH%vol_Thick% ZX8 ZY4 W%vol_Width% X%vol_PosX% Y%vol_PosY% CW%vol_CW%


br += (InStr(A_ThisHotkey, "F1") ? -Brght_Step : Brght_Step )
If ( br > 256 )
   br := 256
If ( br < 0)
   br := 0
VarSetCapacity(gr, 512*3)
Loop,   256

{
   If  (nValue:=(br+128)*(A_Index-1))>65535
        nValue:=65535
   NumPut(nValue, gr,      2*(A_Index-1), "Ushort")
   NumPut(nValue, gr,  512+2*(A_Index-1), "Ushort")

   NumPut(nValue, gr, 1024+2*(A_Index-1), "Ushort")
}
hDC := DllCall("GetDC", "Uint", 0)
DllCall("SetDeviceGammaRamp", "Uint", hDC, "Uint", &gr)
DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)


vol_ShowBars:
; Get volumes in case the user or an external program changed them:
vol_Master := br
if vol_Master > 128
{
  vol_Colour = Red   
  vol_Text = Brightness
}
if vol_Master < 128
{
  vol_Colour = Blue   
  vol_Text = Brightness
}

else
{
if vol_Master = 128
  vol_Colour = Green
  vol_Text = Brightness (normal)

}


; To prevent the "flashing" effect, only create the bar window if it doesn't already exist:

IfWinNotExist, BrightnessOSDxyz
{
    Progress, %vol_BarOptionsMaster% CB%vol_Colour% CT%vol_Colour%, , %vol_Text%, BrightnessOSDxyz
    WinSet, Transparent, %vol_TransValue%, BrightnessOSDxyz
}


Progress, 1:%vol_Master% ,, %vol_Text%
SetTimer, vol_BarOff, %vol_DisplayTime%

return


vol_BarOff:
SetTimer, vol_BarOff, off


Progress, 1:Off
return

Finally I progressed a bit to make the code do more of what I want Embarassed :
F1:: ; reduce the brightness F1
F2:: ; Reset to 50%
F3:: ; increase the brightness F3
Display Brighness progression in %
The progression is ok now. In the previous code the display was not accurate.
I moved the bar down center just above the task bar : It was in the screen center, eventually masking watching movie when called.
Lost color changing.
eg: 50% : green, increase : red, decrease : blue.
I still plan to have it back someday, when able to do it.
Code:
;#NoTrayIcon    don't display the script icon in the tray
#MaxHotkeysPerInterval 200
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
SetBatchLines -1
F2:: ; Set Brightness back to 50 % F2
br := 120 ; brightness, in the range of 0 - 255, where 128 is normal



F1:: ; reduce the brightness  F1
F3:: ; increase the brightness F3
Brght_Step = 8

; -------------------
; START CONFIGURATION
; -------------------
; The percentage by which to raise or lower the volume each time
; How long to display the volume level bar graphs (in milliseconds)
vol_DisplayTime = 1500
; Transparency of window (0-255)
vol_TransValue = 200
; Bar's background colour
vol_CW = EEEEEE   
vol_Width = 300  ; width of bar
vol_Thick = 20   ; thickness of bar
; Bar's screen position
vol_PosX := A_ScreenWidth/2 - vol_Width/2
vol_PosY := A_ScreenHeight/1.08 - vol_Thick/2
; --------------------
; END OF CONFIGURATION
; --------------------
vol_BarOptionsMaster = 1:B1 ZH%vol_Thick% ZX8 ZY4 W%vol_Width% X%vol_PosX% Y%vol_PosY% CW%vol_CW%


br += (InStr(A_ThisHotkey, "F1") ? -Brght_Step : Brght_Step )
If ( br > 256 )
   br := 256 
If ( br < 0)
   br := 0   
VarSetCapacity(gr, 512*3)
Loop, 256
{
   If  (nValue:=(br+128)*(A_Index-1))>65535
        nValue:=65535
   NumPut(nValue, gr,      2*(A_Index-1), "Ushort")
   NumPut(nValue, gr,  512+2*(A_Index-1), "Ushort")
   NumPut(nValue, gr, 1024+2*(A_Index-1), "Ushort")
}
hDC := DllCall("GetDC", "Uint", 0) ;NULL for entire screen
DllCall("SetDeviceGammaRamp", "Uint", hDC, "Uint", &gr)
DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)




vol_ShowBars:
; Get volumes in case the user or an external program changed them:
vol_Master := (br/2.5)
vbr := (br*100)//256 
if vol_Master = 128
{
  vol_Colour = Green
  vol_Text = Brightness %vbr%`%
}

if vol_Master > 128
{
  vol_Colour = Red   
  vol_Text = Brightness %vbr%`%
}
if vol_Master < 128
{
  vol_Colour = Blue

  vol_Text = Brightness %vbr%`%
}

; To prevent the "flashing" effect, only create the bar window if it doesn't already exist:
IfWinNotExist, BrightnessOSDxyz
{
    Progress, %vol_BarOptionsMaster% CB%vol_Colour% CT%vol_Colour%, , %vol_Text%, BrightnessOSDxyz
    WinSet, Transparent, %vol_TransValue%, BrightnessOSDxyz
}


Progress, 1:%vol_Master% ,, %vol_Text%
SetTimer, vol_BarOff, %vol_DisplayTime%
return


vol_BarOff:
SetTimer, vol_BarOff, off
Progress, 1:Off
return

Thanks to all the coders, see previous posts, for this useful code.


Last edited by awannaknow on Thu May 13, 2010 8:54 am; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group