Results 1 to 8 of 8

Thread: color long value to seperate R,G,B values

  1. #1

    Thread Starter
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    I need to convert a long value of a color to the relative R G B values mathematically, since I don't know how, I leave it to this board's capible 'hands'.

    Thank you,

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    Private Type ColourRGB
        lRed As Integer
        lGreen As Integer
        lBlue As Integer
    End Type
    Private Sub Form_Load()
        Dim z As ColourRGB
        z = ExtractColour(RGB(&HAA, &HBB, &HCC))
        Debug.Print Hex(z.lRed)
        Debug.Print Hex(z.lGreen)
        Debug.Print Hex(z.lBlue)
    End Sub
    Private Function ExtractColour(lColour As Long) As ColourRGB
        Dim tempC As ColourRGB
        tempC.lBlue = (lColour \ &H10000) And &HFF
        tempC.lGreen = (lColour \ &H100) And &HFF
        tempC.lRed = lColour And &HFF
        ExtractColour = tempC
    End Function
    - depending on where your colour came from, you may need to swap the Red and Blue values around (endian problems).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    while everyone is on the subject of colors, how is it possible to save the custom colors in a choosecolor dialog box when the user creates them even if they dont select the color as they click ok?
    retired member. Thanks for everything

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Use Mod

    I have not checked the following, but it should be close to correct.
    Code:
    Dim Red As Integer
    Dim Green As Integer
    Dim Blue As Integer
    Dim Tlong As Long
    
    Blue = RGBvalue Mod 256 'Blue is remainder after divide by 256
    Tlong = (RGBvalue - Blue)/256 'Tlong now contains Red & Blue values.
    Green = Tlong Mod 256
    Red = (Tlong - Green)/256
    I am not sure what VB does with arithmetic using Integer & Long variables, but I think it does sensible integer arithmetic.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    well markman, depends what choosecolor dialog box you are using?

    There's also a way to get the RGB values as you know the color is stored in a long and each value represent a byte
    Code:
    Type LngColor
        Color As Long
    End Type
    Type RGBColor
        Red As Byte
        Green As Byte
        Blue As Byte
        Dummy As Byte
    End Type
    
    
    Dim a As LngColor, b As RGBColor
        a.Color = 123456
        LSet b = a
        With b
          Debug.Print .Red
          Debug.Print .Green
          Debug.Print .Blue
        End With
    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.

  7. #7
    Guest
    Code:
    lRed = lColor Mod &H100
    lGreen = Int(lColor / &H100) Mod &H100
    lBlue = Int(lColor / &H10000) Mod &H100

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    meg, you don't need to use modulus for lblue
    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