You are on page 1of 4

Option Explicit

DefLng A-Z
Private Const sHEXChars = "0123456789ABCDEF"
Public Event ErrorMessage(ByVal strmsg As String)
Public Function RGBToBGR(ByVal lRGB As Long) As Long
Dim sRGBHEX As String
Dim sRED As String
Dim sBLUE As String
Dim sGREEN As String
Dim lRed As Long
Dim lGreen As Long
Dim lBlue As Long

'Cannot use 0 as a return value where there is an error - because its a real col
our
'So use a value outside the possible range for RGB or BRG - highest is 16777215
(white)
On Error GoTo lblError

RGBToBGR = 16777215 + 1
sRGBHEX = Hex(lRGB)
Select Case Len(sRGBHEX)
Case 1
sRED = "00"
sGREEN = "00"
sBLUE = "0" & sRGBHEX
Case 2 'Only values for blue
sRED = "00"
sGREEN = "00"
sBLUE = sRGBHEX
Case 3
sRED = "00"
sGREEN = "0" & Left(sRGBHEX, 1)
sBLUE = Right(sRGBHEX, 2)
Case 4 'Only values for blue and green
sRED = "00"
sGREEN = Left(sRGBHEX, 2)
sBLUE = Right(sRGBHEX, 2)
Case 5
sRED = "0" & Left(sRGBHEX, 1)
sGREEN = Mid(sRGBHEX, 3, 2)
sBLUE = Right(sRGBHEX, 2)
Case 6
sRED = Left(sRGBHEX, 2)
sGREEN = Mid(sRGBHEX, 3, 2)
sBLUE = Right(sRGBHEX, 2)
Case Else 'Illegal value
Exit Function
End Select
lRed = HexToDec(sRED)
lGreen = HexToDec(sGREEN)
lBlue = HexToDec(sBLUE)
'BGR value = (blue * 65536) + (green * 256) + red
RGBToBGR = (lBlue * 65536) + (lGreen * 256) + lRed
Exit Function
lblError:
RaiseEvent ErrorMessage(" Routine RGBtoBGR: " & Err.Description)
End Function
Public Function BGRtoRGB(ByVal lBGR As Long) As Long
Dim sRGBHEX As String
Dim sRED As String
Dim sBLUE As String
Dim sGREEN As String
Dim lRed As Long
Dim lGreen As Long
Dim lBlue As Long

'Cannot use 0 as a return value where there is an error - because its a real col
our
'So use a value outside the possible range for RGB or BRG - highest is 16777215
(white)
On Error GoTo lblError

BGRtoRGB = 16777215 + 1
sRGBHEX = Hex(lBGR)
Select Case Len(sRGBHEX)
Case 1
sBLUE = "00"
sGREEN = "00"
sRED = "0" & sRGBHEX
Case 2 'Only values for red
sBLUE = "00"
sGREEN = "00"
sRED = sRGBHEX
Case 3
sBLUE = "00"
sGREEN = "0" & Left(sRGBHEX, 1)
sRED = Right(sRGBHEX, 2)
Case 4 'Only values for red and green
sBLUE = "00"
sGREEN = Left(sRGBHEX, 2)
sRED = Right(sRGBHEX, 2)
Case 5
sBLUE = "0" & Left(sRGBHEX, 1)
sGREEN = Mid(sRGBHEX, 3, 2)
sRED = Right(sRGBHEX, 2)
Case 6
sBLUE = Left(sRGBHEX, 2)
sGREEN = Mid(sRGBHEX, 3, 2)
sRED = Right(sRGBHEX, 2)
Case Else 'Illegal value
Exit Function
End Select
lRed = HexToDec(sRED)
lGreen = HexToDec(sGREEN)
lBlue = HexToDec(sBLUE)
'RGB value = (red * 65536) + (green * 256) + blue
BGRtoRGB = (lRed * 65536) + (lGreen * 256) + lBlue
Exit Function
lblError:
RaiseEvent ErrorMessage(" Routine BGRtoRGB: " & Err.Description)
End Function
Private Function HexToDec(ByVal sHex As String) As Long
'Takes a 2-char Hexadecimal string and turns it into a long
'Could return an integer instead
Dim iVal1 As Integer
Dim iVal2 As Integer
On Error GoTo lblError
iVal1 = InStr(1, sHEXChars, UCase(Right(sHex, 1))) - 1
iVal2 = InStr(1, sHEXChars, UCase(Left(sHex, 1))) - 1
HexToDec = (iVal1 * 16) + iVal2
Exit Function
lblError:
RaiseEvent ErrorMessage(" Routine HexToDec: " & Err.Description)
End Function
Public Function StyleConverter(ByVal iMiProstyle As Integer) As MapXLib.Style
Dim styConverted As New MapXLib.Style
Dim sHex As String
Dim sTmp As String

'Take an integer style value from MI Pro and break it down into bits to assign
'MapX style properties - as per the following from the MapBasic help for Font()
On Error GoTo labelerror
'Style Value & Description of text style
'
'0 Plain
'1 Bold
'2 Italic
'4 Underline
'16 Outline. (Only supported on the Macintosh.)
'32 Shadow
'256 Halo
'512 All Caps
'1024 Expanded
'
'To specify two or more style attributes, add the values from the left column.
'For example, to specify both the Bold and All Caps attributes, use a style valu
e of 513.
sHex = CStr(Hex(iMiProstyle))
If Len(sHex) > 0 Then
'Get the righthand number first
Select Case Int(Val(Right(sHex, 1)))
Case 0
'Plain - nothing to do
Case 1, 3, 5, 7
'Bold
styConverted.TextFont.Bold = True
Case 2, 3, 6, 7
'Italic
styConverted.TextFont.Italic = True
Case 4, 5, 6, 7
'Underline
styConverted.TextFont.Underline = True
Case Else
'trapdoor
End Select
End If
If Len(sHex) > 1 Then
If Len(sHex) = 2 Then
sTmp = Left(sHex, 1)
Else
sTmp = Mid(sHex, 2, 1)
End If
'Now the next set of values
Select Case Int(Val(sTmp))
Case 1
'Equates to decimal 16 - outline - Mac only - nothing to do
Case 2
'Equates to decimal 32 - shadow
styConverted.TextFontShadow = True
Case Else
'Trapdoor
End Select
End If
If Len(sHex) > 2 Then
'The last set of values
Select Case Int(Val(Left(sHex, 1)))
Case 1, 3, 5, 7
'Equates to decimal 256 etc - Halo
styConverted.TextFontHalo = True
Case 2, 3, 6, 7
'Equates to decimal 512 etc - All caps
styConverted.TextFontAllCaps = True
Case Else 'The other value in MIPro - expanded - not supported by MapX
'trapdoor
End Select
End If
Exit Function
labelerror:
End Function

You might also like