VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Convert Long Number Colors to RGB or Convert RGB Colors to Long Number Colors

by Anonymous (267 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 19th December 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Convert Long Number Colors to RGB or Convert RGB Colors to Long Number Colors

Rate Convert Long Number Colors to RGB or Convert RGB Colors to Long Number Colors




'This may be copied into the design code in it's entirety......

'Create a New Standard Exe and add the following objects to the Form:

'In Project select Componts and Check 'Microsoft Common Dialog Control 6.0' - Click Apply and  OK
'Place it anywhere on the Form
'If desired, Set Form1 StartupPosition to 2 - Center Screen (This cannot be done during runtime)

'The placement and properties of the following will be set by the program (if indicated Names are used)
'Add Eight (8) CommandButtons to the Form with the following Names:
'cmdClearL
'cmdClearRGB
'cmdConvert - Index 0
'cmdConvert - Index 1
'cmdDisplayL
'cmdDisplayRGB
'cmdPickColor
'cmdResetAll

'Add a Label (Default Name Label1) and copy it into an array (0 to 11)

'Add Two (2) PictureBoxes (Default Names - Picture1 and Picture2)

'Add a TextBox (Default Name Text1) and copy it into an array (0 to 2)

'Add a TextBox (Default Name Text2)

'You're done! When the program runs the placement and properties will be set
'to useable values.
'------------------------------------------------------------------------------

Dim i       As Variant
Dim R       As Integer
Dim G       As Integer
Dim B       As Integer
Dim X       As New StdFont
Dim uEntry  As Boolean

'----------------Start of Setup------------------------------------------------

Private Sub Form_Initialize()
    'Exit Sub
    ' UnRem the Exit Sub above if Setup is NOT needed
    ' None of the below is needed if you design and label your program YOURSELF!
    ' This is for setup only
    With Form1
        .BorderStyle = 3
        .Caption = "Color Converter"
        .Height = 5175
        .Width = 6855
    End With
    With Label1(0)
        .Alignment = 2
        .Caption = "Enter Long Number"
        .Height = 255
        .Left = 840
        .Top = 360
        .Width = 1455
    End With
    With Text2
        .Alignment = 2
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        .Font.Size = 10
        .Height = 285
        .Left = 720
        .Text = 0
        .Top = 600
        .Width = 1575
    End With
    With Label1(1)
        .AutoSize = True
        .Caption = "OR"
        .Left = 1440
        .Top = 960
    End With
    With cmdPickColor
        .Caption = "Pick Long Color to Use"
        .Height = 255
        .Left = 360
        .Top = 1200
        .Width = 2295
    End With
    With cmdConvert(0)
        .Caption = "Convert Long to RGB"
        .Height = 255
        .Left = 360
        .Top = 1560
        .Width = 2295
    End With
    With cmdDisplayL
        .Caption = "Display Long Color"
        .Height = 255
        .Left = 360
        .Top = 1920
        .Width = 2295
    End With
    With Label1(2)
        .Alignment = 2
        .Caption = "Long Number Color"
        .Height = 255
        .Left = 729
        .Top = 2280
        .Width = 1455
    End With
    With Picture1
        .Height = 1575
        .Left = 720
        .Top = 2520
        .Width = 1455
    End With
    With cmdClearL
        .Caption = "Clear Long"
        .Height = 255
        .Left = 720
        .Top = 4200
        .Width = 1455
    End With
    With cmdResetAll
        .Caption = "Reset All to '0'"
        .Height = 255
        .Left = 2280
        .Top = 4200
        .Width = 1815
    End With
    With Label1(3)
        .Alignment = 2
        .Caption = "Enter R G B Numbers"
        .Height = 255
        .Left = 3960
        .Top = 360
        .Width = 2295
    End With
    With Label1(4)
        .AutoSize = True
        .Caption = "RGB("
        .Font.Bold = True
        .Font.Size = 12
        .Left = 3240
        .Top = 600
    End With
    With Text1(0)
        .Alignment = 2
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        .Font.Size = 10
        .Height = 285
        .Left = 3960
        .MaxLength = 3
        .TabStop = False
        .Text = 0
        .Top = 600
        .Width = 615
    End With
    With Label1(5)
        .AutoSize = True
        .Caption = ","
        .Font.Bold = True
        .Font.Size = 12
        .Left = 4560
        .Top = 600
    End With
    With Text1(1)
        .Alignment = 2
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        .Font.Size = 10
        .Height = 285
        .Left = 4800
        .MaxLength = 3
        .TabStop = False
        .Text = 0
        .Top = 600
        .Width = 615
    End With
    With Label1(6)
        .AutoSize = True
        .Caption = ","
        .Font.Bold = True
        .Font.Size = 12
        .Left = 5400
        .Top = 600
    End With
    With Text1(2)
        .Alignment = 2
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        .Font.Size = 10
        .Height = 285
        .Left = 5640
        .MaxLength = 3
        .TabStop = False
        .Text = 0
        .Top = 600
        .Width = 615
    End With
    With Label1(7)
        .AutoSize = True
        .Caption = ")"
        .Font.Bold = True
        .Font.Size = 12
        .Left = 6270
        .Top = 600
    End With
    With Label1(8)
        .BackColor = vbRed         'Long = 255,      RGB = RGB(255, 0, 0)
        .BorderStyle = 1
        .Caption = ""
        .Height = 255
        .Left = 4140
        .Top = 960
        .Width = 255
    End With
    With Label1(9)
        .BackColor = vbGreen       'Long = 65280,    RGB = RGB(0, 255, 0)
        .BorderStyle = 1
        .Caption = ""
        .Height = 255
        .Left = 4980
        .Top = 960
        .Width = 255
    End With
    With Label1(10)
        .BackColor = vbBlue        'Long = 16711680, RGB = RGB(0, 0, 255)
        .BorderStyle = 1
        .Caption = ""
        .Height = 255
        .Left = 5835
        .Top = 960
        .Width = 255
    End With
    With cmdConvert(1)
        .Caption = "Convert RGB to Long"
        .Height = 255
        .Left = 3960
        .Top = 1560
        .Width = 2055
    End With
    With cmdDisplayRGB
        .Caption = "Display RGB Color"
        .Height = 255
        .Left = 3960
        .Top = 1920
        .Width = 2055
    End With
    With Label1(11)
        .Alignment = 2
        .Caption = "RGB Number Color"
        .Height = 255
        .Left = 4200
        .Top = 2289
        .Width = 1455
    End With
    With Picture2
        .Height = 1575
        .Left = 4200
        .Top = 2520
        .Width = 1455
    End With
    With cmdClearRGB
        .Caption = "Clear RGB"
        .Height = 255
        .Left = 4200
        .Top = 4200
        .Width = 1455
    End With
End Sub
'----------------------End of Setup------------------------------------

Private Sub cmdClearL_Click()
    Text2 = 0
    Picture1.BackColor = &H8000000F
End Sub

Private Sub cmdClearRGB_Click()
    For i = 0 To 2
        Text1(i) = 0
    Next i
    Picture2.BackColor = &H8000000F
End Sub

Private Sub cmdConvert_Click(Index As Integer)
    Select Case Index
        Case 0      'This is the "Long to RGB"
            LONG_COMP
            uEntry = True
        Case 1      'This is the "RGB to Long"
            If uEntry = True Then RGB_COMP
    End Select 'Index
End Sub

Private Sub cmdDisplayL_Click()
    Picture1.BackColor = Text2
End Sub

Private Sub cmdDisplayRGB_Click()
    Select Case uEntry
        Case Is = False
            Picture2.BackColor = RGB(R, G, B)
        Case Is = True
            Picture2.BackColor = RGB(Val(Text1(0)), Val(Text1(1)), Val(Text1(2)))
    End Select 'uEntry
End Sub

Private Sub cmdPickColor_Click()
    CommonDialog1.ShowColor
    Text2 = CommonDialog1.Color
End Sub

Private Sub cmdResetAll_Click()
    Text2 = 0
    For i = 0 To 2
        Text1(i) = 0
    Next i
    Picture1.BackColor = &H8000000F
    Picture2.BackColor = &H8000000F
    R = 0
    G = 0
    B = 0
End Sub

Private Sub Form_Load()
        cmdResetAll.Value = True
End Sub

Private Sub RGB_COMP()
    'Because it's 0 to 255, it can't
    'be blank (less than 0) or above 255
    If Len(Text1(Index).Text) = 0 Then
        Text1(Index).Text = 0
    End If
    If Text1(Index).Text > 255 Then
        Text1(Index).Text = 255
    End If
    'This does the math that Anonymous1 provided us with
    Select Case Index
        Case 0
            If Text1(2).Text <> "" Then
                If Text1(1).Text <> "" Then
                    Text2.Text = Text1(2).Text * 65536 + Text1(1).Text * 256 + Text1(0).Text
                End If
            End If
        Case 1
            If Text1(2).Text <> "" Then
                If Text1(0).Text <> "" Then
                    Text2.Text = Text1(2).Text * 65536 + Text1(1).Text * 256 + Text1(0).Text
                End If
            End If
        Case 2
            If Text1(1).Text <> "" Then
                If Text1(0).Text <> "" Then
                    Text2.Text = Text1(2).Text * 65536 + Text1(1).Text * 256 + Text1(0).Text
                End If
            End If
    End Select
    Select Case uEntry
        Case Is = False
            B = Text2.Text \ 65536
            G = (Text2.Text - B * 65536) \ 256
            R = Text2.Text - B * 65536 - G * 256
            Text1(0).Text = R
            Text1(1).Text = G       'These are the caclulated values for RGB
            Text1(2).Text = B
        Case Is = True
            If Text1(0) = "" Then Text1(0) = 0
            If Text1(1) = "" Then Text1(1) = 0
            If Text1(2) = "" Then Text1(2) = 0
    End Select 'uEntry
End Sub

Private Sub LONG_COMP()
    'Because it's 0 to 16777215, it can't
    'be blank (less than 0) or above 16777215
    If Len(Text2.Text) = 0 Then
        Text2.Text = 0
    End If
    If Text2.Text > 16777215 Then
        Text2.Text = 16777215
    End If
    'This does the math that Anonymous1 provided us with
    Text1(2).Text = Text2.Text \ 65536
    Text1(1).Text = (Text2.Text - Text1(2).Text * 65536) \ 256
    Text1(0).Text = Text2.Text - Text1(2).Text * 65536 - Text1(1).Text * 256
    R = Val(Text1(0))  'Red color
    G = Val(Text1(1))  'Green color - These are replacements for the cmdDisplayRGB
    B = Val(Text1(2))  'Blue color
End Sub

Private Sub Text1_Click(Index As Integer)
    uEntry = True
    Text2 = 0
End Sub

Private Sub Text1_GotFocus(Index As Integer)
    'This will select all text on focus
    Text1(Index).SelStart = 0
    Text1(Index).SelLength = Len(Text1(Index).Text)
End Sub

Private Sub Text2_GotFocus()
    'This will select all text on focus
    Text2.SelStart = 0
    Text2.SelLength = Len(Text2.Text)
End Sub



Download this snippet    Add to My Saved Code

Convert Long Number Colors to RGB or Convert RGB Colors to Long Number Colors Comments

No comments have been posted about Convert Long Number Colors to RGB or Convert RGB Colors to Long Number Colors. Why not be the first to post a comment about Convert Long Number Colors to RGB or Convert RGB Colors to Long Number Colors.

Post your comment

Subject:
Message:
0/1000 characters