Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'from here: http://www.pvladov.com/2012/09/make-color-lighter-or-darker.html
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource Bitmap cowgirl "cowgirl.bmp"
Global hDlg As Dword, hBMP As Dword
%ID_Graphic1 = 500
%ID_Graphic2 = 501
%ID_Graphic3 = 502
Function PBMain() As Long
Dialog New Pixels, 0, "Lighten+Grayscale Image",600,300,375,150, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, 100, "Lighten+Grayscale", 10,10,150,20
Control Add Graphic, hDlg, %ID_Graphic1,"", 10,40,100,100, %WS_Visible ' Or %SS_Sunken
Control Add Graphic, hDlg, %ID_Graphic2,"", 130,40,100,100, %WS_Visible 'Or %SS_Sunken
Control Add Graphic, hDlg, %ID_Graphic3,"", 250,40,100,100, %WS_Visible 'Or %SS_Sunken
Graphic Attach hDlg, %ID_Graphic1 : Graphic Color %Black,%White : Graphic Clear
Graphic Render "cowgirl", (0,0)-(99,99)
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If Cb.Msg = %WM_Command And Cb.Ctl = 100 Then GrayScale
End Function
Sub GrayScale 'uses Long pointer/CVL solution
Local w As Long, h As Long, p,p2 As Long Ptr, i As Long
Local iColor As Long, R,G,B As Single, bmp$, bmp2$
Local correctionfactor As Single
'get the string from ID_Graphic1
Graphic Attach hDlg, %ID_Graphic1
Graphic Get Bits To bmp$
'get width/height of image
w = Cvl(bmp$,1)
h = Cvl(bmp$,5)
bmp2$ = bmp$
p = StrPtr(bmp$)+8 'position of starting position for bits in string
p2 = StrPtr(bmp2$)+8 'position of starting position for bits in string
'get string position of coordinates and modify the string at that position
correctionfactor = 0.7
For i = 1 To w*h
iColor = @p 'result is a BGR color value 0-R-G-B
B = iColor Mod 256 'or this: iColor AND &HFF&
G = (iColor\256) Mod 256 'or this: (iColor AND &HFF00&) \ &H100
R = (iColor\256\256) Mod 256 'or this: (iColor AND &HFF0000&) \ &H10000&
'lighten/darken
If (correctionFactor < 0) Then
correctionFactor = 1 + correctionFactor
R *= correctionFactor
G *= correctionFactor
B *= correctionFactor
Else
R = (255 - R) * correctionFactor + R
G = (255 - G) * correctionFactor + G
B = (255 - B) * correctionFactor + B
End If
@p = Bgr(R,G,B) 'modify string at that position
'grayscale
iColor = 0.299*R + 0.587*G + 0.114*B
@p2 = Bgr(iColor, iColor, iColor)
Incr p : Incr p2
Next i
'put the modified string into ID_Graphic2
Graphic Attach hDlg, %ID_Graphic2 : Graphic Set Bits bmp$
Graphic Attach hDlg, %ID_Graphic3 : Graphic Set Bits bmp2$
End Sub
'gbs_01405
'Date: 10-17-2014
http://www.garybeene.com/sw/gbsnippets.htm