[Solved] Color Pick on Image

BlitzMax Forums/BlitzMax Beginners Area/[Solved] Color Pick on Image

RustyKristi(Posted 2016) [#1]
Hi, Is there a way to color pick (RGB) an image once it is loaded (not displayed)?


Yan(Posted 2016) [#2]
Yes!


Derron(Posted 2016) [#3]
color = ReadPixel from a given Pixmap

Pixmap = LockImage(img)



bye
Ron


markcw(Posted 2016) [#4]
The basics of accessing pixels.
	Local map:TPixmap=LoadPixmap(path$)
	If map.format <> PF_RGBA8888 map=map.Convert(PF_RGBA8888)	
	For Local iy%=0 To PixmapWidth(map)-1
		For Local ix%=0 To PixmapHeight(map)-1
			Local r%=ReadPixel(map,ix,iy) & $FF
			Local g%=ReadPixel(map,ix,iy) & $00FF
			Local b%=ReadPixel(map,ix,iy) & $0000FF
			Local alp%=ReadPixel(map,ix,iy) & $000000FF
			WritePixel map,ix,iy,r | (g Shl 8) | (b Shl 16) | (alp Shl 24)
		Next
	Next



RustyKristi(Posted 2016) [#5]
Thanks, I got it now :)


TomToad(Posted 2016) [#6]
			Local r%=ReadPixel(map,ix,iy) & $FF
			Local g%=ReadPixel(map,ix,iy) & $00FF
			Local b%=ReadPixel(map,ix,iy) & $0000FF
			Local alp%=ReadPixel(map,ix,iy) & $000000FF

That will actually return the exact same value for all components. I think you meant
			Local r%=ReadPixel(map,ix,iy) & $FF
			Local g%=ReadPixel(map,ix,iy) & $FF00
			Local b%=ReadPixel(map,ix,iy) & $FF0000
			Local alp%=ReadPixel(map,ix,iy) & $FF000000

Although even that would be wrong. Regardless of the pixmap format, readpixel will always convert to ARGB. Also, Readpixel is a bit slow, might be a good idea to read once and store in a separate variable.
			Local Color% = ReadPixel(map,ix,iy)
			Local r%=Color & $FF0000 Shr 16
			Local g%=Color & $ff00 Shr 8
			Local b%=Color & $FF
			Local alp%=Color & $FF000000 Shr 24



markcw(Posted 2016) [#7]
yes quite correct Tom, that was some WIP code. Here's a more finished example, this allows you to choose any color channel as the alpha channel, it's a workaround for texture blending with alpha in Openb3d.
	Function LoadTextureAlpha:TTexture( file:String,flags:Int=11,alphamask%=$0000FF00 )
	
		Local map:TPixmap=LoadPixmap(file)
		If map.format<>PF_RGBA8888 Then map=map.Convert(PF_RGBA8888)
		If flags & 2 And alphamask ' only if alpha flag and mask
			For Local iy%=0 To PixmapWidth(map)-1
				For Local ix%=0 To PixmapHeight(map)-1
					Local rgba%=ReadPixel(map,ix,iy)
					Local alp%=rgba & alphamask
					If alp & $FF000000 ' alpha
						alp=alp Shr 24 ' convert to byte
					ElseIf alp & $00FF0000 ' blue
						alp=alp Shr 16
					ElseIf alp & $0000FF00 ' green
						alp=alp Shr 8
					EndIf
					alp=alp*(Float(alp)/255.0) ' makes darker colors less visible
					WritePixel map,ix,iy,(rgba & $00FFFFFF)|(alp Shl 24)
				Next
			Next
		EndIf
		Local tex:TTexture=CreateTexture(PixmapWidth(map),PixmapHeight(map),flags)
		BufferToTex tex,PixmapPixelPtr(map,0,0)
		Return tex
		
	End Function