Results 1 to 12 of 12

Thread: Getting RGB from palette

  1. #1

    Thread Starter
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282
    If I have a form which has a background colour of &H00C0C0C0 then it's easy to extract the RGB values. OK, but what if the colour of the form is taken from the current palette so the value is &H0800000F. How do I extract the value 15 from this ? Once I've got that I know how to get the RGB from the API but I'm no maths wizz so any help on getting the 15 from the value would be appreciated.
    That's Mr Mullet to you, you mulletless wonder.

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It just basically means using bitmasks and shifts. Since shifts aren't possible in VB, you have to simulate them with division. This code will get you the red, green and blue parts from a 32-bit colour value:

    Code:
    Red = Colour And 255
    Green = Colour \ 256 And 255
    Blue = Colour \ 65536 And 255
    If you wanted the alpha too, it would be (Colour \ 16777216 And 255).

    In case you hadn't noticed:
    256 = 2^8
    65536 = 2^16
    16777216 = 2^24
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282
    Thanks Harry but I've all ready got the code for splitting out the RGB values. To expand upon my explanation - it's not a question of getting the colour values out, it's all about trying to lose the &H08 from the start of the palette colour number. However, VB looks at the number and thinks it's a negative value ! Help.
    That's Mr Mullet to you, you mulletless wonder.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh... hmm well I've never come across this before since I don't use VB for graphics stuff, but perhaps you could convert it to a string, take off the &H using the Right() function, and then convert it back? A bit messy I know but I don't know how VB is treating the number if it's giving you the &H at the beginning.

    I've just realised you said it was sticking &H08 in front of the number... why do you think it shouldn't do that?
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282
    It should do that, it's how it indicates that the colour is from a palette. You should then remove the 08 from the front and then convert the Hex to get the colour number. I'd come up with checking the hex string and using $ functions but there must be a matchematical approach to this. Thanks for looking at it though - it's really starting to p!ss me off.
    That's Mr Mullet to you, you mulletless wonder.

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh, weird.... I guess I'm used to working with C++ with, in this case, more straightforward data. Sorry I couldn't help more Maybe Abs() would remove the negativity? Just a last guess.
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    the thing is that vb uses SIGNED long's that ranges values from -2^31 to 2^31-1, not 0 to 2^32-1. To get around this problem (yeah this been annoying me some time) is to have a currency datatype and add 2^31 if the value is negative. It might also work if you use copymemory with an offset of 2 bytes by copying over the long to a byte array of 8 bytes to 4'th place and then copy it to the currency, then multiply by 10000 to avoid the decimals.
    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.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    just ignore what i said, you don't need the RGBA, but the components,copy them over to a byte array of 4 instead and you have each component in it's own byte, sorry about the confusion
    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.

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    If you're going to start using CopyMemory, you might as well just copy the 8-bit red, green and blue components straight out of the colour value. Or perhaps I'm missing something that makes it more complicated.
    Harry.

    "From one thing, know ten thousand things."

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh, well looks like you got there just before me
    Harry.

    "From one thing, know ten thousand things."

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    a bit slow today? hehe
    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.

  12. #12

    Thread Starter
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282
    Cheers guys. This is what I did in the end :

    Code:
    lTemp = lColor Xor -2147483648# 
    
    If lTemp >= 1 Then
        lColor = GetSysColor(lTemp)
    End If
    and that seems to do the trick. Cheers for the help.
    That's Mr Mullet to you, you mulletless wonder.

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