Fastest way to get pixel colour value?

DavidN

Member
Licensed User
I want to selectively get the Red, Green, or Blue value for a pixel. Ideally, I would like 3 functions: GetPixelRed(), GetPixelGreen(), and GetPixelBlue(). Each function would return a value from 0..255. This would better complement the existing SetPixel() function which takes 3 integers as arguments.

The existing GetPixel() function just returns a 32-bit value that I assume/suspect consists of the following byte fields
Byte 3 = 0xFF
Byte 2 = Red Value (0..255)
Byte 1 = Green Value (0..255)
Byte 0 = Blue Value (0..255)

I could extract the particular R, G, or B value from this 32-bit value using some bitwise manipulation, but am concerned that the compiler wouldn't optimize it fast enough for my needs.

Any ideas of what the fastest way to get a Red, Green, or Blue colour value from a pixel would be?

Thanks,
 

Cableguy

Expert
Licensed User
Try the following

ad three varables to the globals:
R,G,B -This will be the color components

The in the whatever sub
try :

RGB(R,G,B)=getpixel(x,y)
msgbox("Your color is mador Red("&R&"), Green("&G&"), and Blue("&B&")")

Please note that this is un-tested code...but i Think it will do what you want...
 

specci48

Well-Known Member
Licensed User
I don't know if this is the fastet way but it works:

B4X:
Sub Globals
   red = 0
   green = 0
   blue = 0   
End Sub

Sub App_Start
   ...
   Color2RGB(bitmap1.GetPixel1(w,h))
   ...      
End Sub

Sub Color2RGB(color)
   colorNum = color + 16777216
   red = Int(colorNum / 65536)      
   green = Int((colorNum - red * 65536) / 256)   
   blue = colorNum - red * 65536 - green * 256
End Sub

specci48
 

hung

Member
Licensed User
I got something similar in one of my program.

vC = form1.GetPixel(x, y) + 16777216
vR = Int(vC / 65536)
vG = Int((vC Mod 65536) / 256)
vB = vC Mod 256

But that assumes the PPC is using 65535 colors, other color pallet will not work.
 

DavidN

Member
Licensed User
Next version will include a GetRGB method that will return the three components of a color.
That's great! Hopefully it'll run faster than the work arounds. When do you think this next release will be?
  1. days?
  2. weeks?
  3. months?
  4. All of the above?
  5. None of the above?

Thanks for your work at improving an impressive product.
 
Top