Home > Software engineering >  Vb to realize an HSL adjustment
Vb to realize an HSL adjustment

Time:10-04

'Option Explicit
This functionality is implemented, to be duplicated code, verify adaptation of an HSL is correct, how to improve,
'control: picture 1 load source images
'the picture2 empty, no picture
'picture3 shows hue 0-360 degree
'HScroll1 min=0 Max=360' hue
'HScroll2 min=0 Max=100' saturation
'HScroll3 min=0 Max=100' brightness

Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long
Private Declare Function SetDIBits Lib "gdi32" (ByVal HDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, ByVal wUsage As Long) As Long
Private Type BitMapInfoHeader 'file information head - BitMapInfoHeader
BiSize As Long
BiWidth As Long
BiHeight As Long
BiPlanes As Integer
BiBitCount As Integer
BiCompression As Long
BiSizeImage As Long
BiXPelsPerMeter As Long
BiYPelsPerMeter As Long
BiClrUsed As Long
BiClrImportant As Long
End Type

Private Type RGBQuad
RgbBlue As Byte
RgbGreen As Byte
RgbRed As Byte
"' rgbReserved As Byte
End Type

Private Type BitMapInfo
BmiHeader As BitMapInfoHeader
BmiColors As RGBQuad
End Type



Private Sub Form_Load ()
Dim W As Long
Dim H As Long
Dim As Long a
Dim As Long I
Dim As Long c
W=Picture3. Width Width
'H=Picture3. Height 'Height
A=W/6 'image is divided into six parts

For I=0 To a
C=I * 255/a
Picture3. Line (I + a * 0, 0) - (I + a * 0, H), RGB (255, c, 0)
Picture3. Line (I + a * 1, 0) - (I + 1, a * H), RGB (255 - c, 255, 0)



Picture3. Line (I + a * 2, 0) - (I + a * 2, H), RGB (0, 255, c)
Picture3. Line (I + a * 3, 0) - (I + a * 3, H), RGB (0, 255 - c, 255)
Picture3. Line (I + a * 4, 0) - (I + a * 4, H), RGB (c, 0, 255)
Picture3. Line (I + a * 5, 0) - (I + a * 5, H), RGB (255, 0, 255 - c)
Next I

For I=0 To 6
If I & lt;> 6 Then
Picture3. CurrentX=I * a - TextWidth (I * 60)/2

The Else
Picture3. - TextWidth CurrentX=W (" 36000 ")
End the If
Picture3. CurrentY=H/2 - TextHeight (I)

Picture3. Print I * 60
Next
End Sub

Public Sub RGBtoHSL (ByVal R As Byte, ByVal G As Byte, ByVal B As Byte, H As Single, S As Single, L As Single)

Dim Max As Single
Dim Min As Single
Dim delta As Single
Dim rR As Single, rG As Single, rB As Single

'-- Given RGB each in [0, 1]
'- the Desired: H in [0240] and S in [0, 1], except if S=0, then H=UNDEFINED
RR=R/255: rG=G/255: rB=B/255

Max=pvMaximum (rR, rG, rB)
Min=pvMinimum (rR, rG, rB)
L=(Max + Min)/2

'==Calculate saturation:

'-- Achromatic case
If (Max=Min) Then
S=0
H=0

'-- Chromatic case
The Else
'- the First calculate the saturation
If (L & lt;=0.5) Then
S=(Max - Min)/(Max + Min)
The Else
S=(Max - Min)/(2 - Max - Min)
End the If
'-- Next calculate the hue
The delta=Max - Min
If (rR=Max) Then
H=(rG - rB)/delta 'Resulting color is between yellow and magenta
ElseIf (rG=Max) Then
H=2 + (rB - rR)/delta 'Resulting color is between cyan and yellow
ElseIf (rB=Max) Then
H=4 + (rR - rG)/delta 'Resulting color is between magenta and cyan
End the If
End the If
End Sub

Public Sub HSLtoRGB (ByVal H As Single, ByVal S As Single, ByVal L As Single, R As Byte, G As Byte, B As Byte)
On the Error Resume Next
Dim rR As Single, rG As Single, rB As Single
Dim Min As Single, Max As Single

'-- Achromatic case:
If (S=0) Then
RR=L: rG=L: rB=L

'-- Chromatic case:
The Else
If (L & lt;=0.5) Then
'- S=(Max - Min)/(Max + Min)
Min=L * (1 - S)
The Else
'- S=(Max - Min)/(2 - Max - Min)
Min=L - S * (1 - L)
End the If
Max=2 * L - Min

'-- Now depending on sector we can evaluate the H, L, S:
If (H & lt; 1) Then
RR=Max
If (H & lt; 0) Then
RG=Min
RB=rG - H * (Max - Min)
The Else
The rB=Min
RG=H * (Max - Min) + rB
End the If
ElseIf (H & lt; 3) Then
RG=Max
If (H & lt; 2) Then
The rB=Min
RR=rB - (H - 2) * (Max - Min)
The Else
RR=Min
RB=(H - 2) * (Max - Min) + rR
End the If
The Else
RB=Max
If (H & lt; 4) Then
RR=Min
RG=rR - (H - 4) * (Max - Min)
The Else
RG=Min
RR=(H - 4) * (Max - Min) + rG
End the If
End the If
End the If
R=rR * 255: G=rG * 255: B=rB * 255
If R & gt; 255 Then R=255
If G & gt; Then 255 G=255
If B & gt; 255 Then B=255
End Sub

Private Function pvMaximum (rR As Single, rG As Single, rB As Single) As Single
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related