AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

Retrieving the red, green and blue values from a RGB color

I had the problem to split an RGB color to its red, green and blue values. So, I used my Assembler knowledge to do it.! Here's the sample-code (for a command button):

Private Sub Command1_Click()

x& = RGB(212, 15, 21)

'lowbyte = red value
'second byte = green value
'third byte = blue value
'highbyte = always zero

Print "RGB color:"; x&

'first = low byte
Print "red:"; vbTab; x& And &HFF&

'seconde byte, to make it an eight bit value,
'we have to divide it by 256 = &H100&
Print "green:"; vbTab; (x& And &HFF00&) \ &H100&

'third byte, division by 65536 = &H10000 makes
'it eight bit
Print "blue:"; vbTab; (x& And &HFF0000) \ &H10000

'the highbyte is always 0, so we don't have to
'read it!
'
'What does the AND-OPERATOR do?
'
'To get only the lowbyte, for example, you have
'to do an AND-OPERATION with &HFF& = 255.
'&HFF& = 0000000011111111 in bits (here I only
'show it as 16 bit value).
'
'0 and 0 = 0
'0 and 1 = 0
'1 and 0 = 0
'1 and 1 = 1
'
'So, making an AND-OPERATION with &HFF& doesn't
'change the lowbyte, but sets all other bytes
'(bits) to zero!!!

'Using &HFF00& sets all bytes (bits) to zero
'except the second byte! To convert it to an
'eight bit value, simply divide it by 256 = &H100&.
'
'32-bit value: highbyte, third byte, second byte,
'lowbyte
'Other progtammers are using following names:
'H.O. Byte = high order byte
'Byte #2 = third byte
'Byte #1 = second byte
'L.O. Byte = low order byte
'
'e.g.:00010203, 00 = highbyte, 03 = lowbyte

End Sub

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/