### Author Topic: Colour extraction  (Read 3139 times)

#### matty47

• Mr. Drawsprite
• Posts: 71
##### Colour extraction
« on: 2008-Feb-28 »
A few functions to extract the component values of a 32 colour and an example of how to use them. I have adapted the example code from a DarkbasicPro tutorial so if you feel this infringes any copyright please remove. Hopefully the code is commented enough to work out. You will need a 200 x 200 pixel image with a spectrum of colours for the sprite.
Code: (glbasic) [Select]
`// --------------------------------- //// Project: RGB_Tute2// Start: Thursday, February 28, 2008// IDE Version: 5.179//load our imageLOADSPRITE "rgb.png",1//drawing to 2d screenX_MAKE2D//draw our base pictureDRAWSPRITE 1,0,0LOCAL x=190LOCAL y=190LOCAL col,tx,ty,r,g,b,timer,TimeTakenGLOBAL blue,green,red,alphtimer=GETTIMERALL()FOR ty=0 TO y FOR tx=0 TO x col=GETPIXEL(tx,ty) //get the colour at tx,ty //now split the colour into its components r=rgbr(col) g=rgbg(col) b=rgbb(col) //now use these to draw 3 images SETPIXEL tx+200,ty,RGB(r,0,0) SETPIXEL tx+400,ty,RGB(0,g,0) SETPIXEL tx+600,ty,RGB(0,0,b) //now draw another 3 SETPIXEL tx+200,ty+200,RGB(r,g,0) SETPIXEL tx+400,ty+200,RGB(r,0,b) SETPIXEL tx+600,ty+200,RGB(0,g,b) //and finally another three SETPIXEL tx+200,ty+400,RGB(r*2,g*2,b*2) SETPIXEL tx+400,ty+400,RGB(r-tx,tx,b-tx) SETPIXEL tx+600,ty+400,RGB(150,g/2,b/2) NEXT NEXT TimeTaken=GETTIMERALL()-timerPRINT "Time Taken = "+TimeTaken,10,550SHOWSCREENKEYWAITEND//*************************************//Function rgbr(col) - return the red component of a 32 bit colour//*************************************FUNCTION rgbr: col red=bAND(col,0xff) RETURN redENDFUNCTION//*************************************//Function rgbg(col) - return the green component of a  32 bit colour//*************************************FUNCTION rgbg: col green=bAND(col/0x100,0xff) RETURN greenENDFUNCTION//*************************************//Function rgbb(col) - return the blue component of a  32 bit colour//*************************************FUNCTION rgbb: col blue=bAND(col/0x10000,0xff) RETURN blueENDFUNCTION//*************************************//Function rgba(col) - return the alpha component of a  32 bit colour//*************************************FUNCTION rgba: col alph=bAND(col/0x1000000,0xff) RETURN alphENDFUNCTION`The image I used had that transparent colour (pink) in it so the effect was a bit marred. (Is there a way to turn off the pink transparency??) You need to set the resolutuin to at least 800x600
I tested the speed in both DarkBasic Pro and GLBasic. - DBP 11.44secs, GLB - 1.84secs.
Hope this may be useful
M

#### PeeJay

• Mr. Polyvector
• Posts: 244
##### Colour extraction
« Reply #1 on: 2008-Feb-28 »
Quote from: matty47
I have adapted the example code from a DarkbasicPro tutorial so if you feel this infringes any copyright please remove
I reckon you're safe - after all, I'm sure the DB Pro demo code states you can use any of it in your own projects
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity