Fun With Gamma
Posted: Tue Jul 22, 2014 5:59 am
Here is a tool for working with a display's gamma in Windows. It generates a gamma ramp (transfer curve) and loads it using SetDeviceGammaRamp().
Note: it is based on the de-facto gamma standard of 2.2. This is represented as the constant #STANDARD_GAMMA.
Note: it is based on the de-facto gamma standard of 2.2. This is represented as the constant #STANDARD_GAMMA.
Code: Select all
Global DISPLAY_GAMMA.d, USER_GAMMA.d, result
#STANDARD_GAMMA = 2.2 ; DE-FACTO STANDARD
USER_GAMMA = #STANDARD_GAMMA
Global Dim iGammaRamp.w(3,255)
Procedure SetDisplayGamma()
DISPLAY_GAMMA.d = USER_GAMMA / #STANDARD_GAMMA
; If DISPLAY_GAMMA < 0.9 Or DISPLAY_GAMMA > 1.1
; MessageRequester("Error", "Display gamma must be not less than 0.9 and not more than 1.1.")
; End
; EndIf
DISPLAY_GAMMA.d = 1/DISPLAY_GAMMA
For ct = 0 To 255
iValue.d = (Pow(ct, DISPLAY_GAMMA) * 257)
If iValue > 65535: iValue = 65535: EndIf
iGammaRamp(0,ct) = iValue
iGammaRamp(1,ct) = iGammaRamp(0,ct)
iGammaRamp(2,ct) = iGammaRamp(0,ct)
;GIVE R G AND B CHANNELS THE SAME GAMMA VALUE
Next
;REWRITE THE GAMMA TABLE
result = SetDeviceGammaRamp_(GetDC_(0),@iGammaRamp())
EndProcedure
Procedure DrawWindow()
SetDisplayGamma()
StartDrawing(WindowOutput(1))
For ct = 0 To 10
Box((ct * 74)+25, 250, 75, 100, RGB(ct * 25, ct*25,ct*25))
Next
DrawText(160, 100, "Gamma = " + StrD(1/DISPLAY_GAMMA * #STANDARD_GAMMA, 2),#White)
; If result = #True
; DrawText(50, 100, "Success",#White)
; EndIf
StopDrawing()
EndProcedure
OpenWindow(1, 0, 0, 930, 500, "Gamma", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
TrackBarGadget(1, 50, 145, 300, 20, 199, 242)
SetGadgetState(1, 220)
DrawWindow()
Repeat
event = WaitWindowEvent()
If event = #PB_Event_CloseWindow:
If USER_GAMMA <> 2.2
message = MessageRequester("Restore gamma?", "Restore gamma to default (2.2)?", #PB_MessageRequester_YesNo)
If message = #PB_MessageRequester_Yes: USER_GAMMA = 2.2: DrawWindow(): End: ;EndIf
Else
End
EndIf
Else
End
EndIf
ElseIf event = #PB_Event_Gadget
Select EventGadget()
Case 1
USER_GAMMA = GetGadgetState(1) / 100
DrawWindow()
EndSelect
ElseIf event = #PB_Event_Repaint: DrawWindow()
EndIf
ForEver