Results 1 to 6 of 6

Thread: How do I convert Hex colours into RGB

  1. #1
    Guest

    Question

    How would I convert something like &HFFFFFF into RGB(255, 255, 255)?

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Smile Use Cdec function

    Dreamlax, may be you can use the CDec function as below:

    Code:
    Private Sub Form_Load()
    Dim R%, G%, B%
    Dim HexColorCode$
    HexColorCode = "&HFFFFFF"
    'Assume Hex Color Code = &HFFFFFF
    R = CDec("&H" & Mid(HexColorCode, 3, 2))
    G = CDec("&H" & Mid(HexColorCode, 5, 2))
    B = CDec("&H" & Mid(HexColorCode, 7, 2))
    MsgBox "&HFFFFFF = RGB(" & R & ", " & G & ", " & B & ")"
    End Sub

  3. #3
    Guest
    That won't work if the colour is black because black is &H0 not &H000000.

  4. #4
    Guest
    it would be pretty complicated...
    but the concept of hex is easy

    I am only posting for the blue, because it takes along time to type it up, and takes up too much space, but you can gain the general concept by this table....

    http://denniswrenn.virtualave.net/RGBHEX.htm

    [Edited by denniswrenn on 05-27-2000 at 11:13 PM]

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    New GetRGB Function

    Hi Dreamlax, This more flexible & resolve your problem,

    Code:
    Option Explicit
    Private Sub Form_Load()
    Dim R%, G%, B%
    Dim HexColorCode$
    'Assume Hex Color Code = &HFFFFFF
    HexColorCode = "&H0"
    R = GetRGB(CDec(HexColorCode), 1)
    G = GetRGB(CDec(HexColorCode), 2)
    B = GetRGB(CDec(HexColorCode), 3)
    MsgBox HexColorCode & " = RGB(" & R & ", " & G & ", " & B & ")"
    End Sub
    Function GetRGB(RGBval As Long, Num As Integer) As Integer
        ' RGB color value as Long, and component number as integer
        ' that represents the component color to return (1=red,
        ' 2=green, 3=blue).
              
        ' Check if Num, RGBval are valid.
        If Num > 0 And Num < 4 And RGBval > -1 And RGBval < 16777216 Then
            GetRGB = RGBval \ 256 ^ (Num - 1) And 255
        Else
        ' Return True (-1) if Num or RGBval are invalid.
            GetRGB = 0
        End If
    End Function

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Just don't get upset but making all the fancy rgb functions
    will just drain your performance. The fastest way is always
    is the straightest
    Code:
    R=color mod 256
    G=int(color/256) mod 256
    B=int(color/&H10000)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width