Author Topic: Colour extraction  (Read 2491 times)

Offline matty47

  • Mr. Drawsprite
  • **
  • Posts: 69
    • View Profile
    • Matt and Marg's Homesite
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 image
LOADSPRITE "rgb.png",1
//drawing to 2d screen
X_MAKE2D
//draw our base picture
DRAWSPRITE 1,0,0
LOCAL x=190
LOCAL y=190
LOCAL col,tx,ty,r,g,b,timer,TimeTaken
GLOBAL blue,green,red,alph
timer=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()-timer
PRINT "Time Taken = "+TimeTaken,10,550
SHOWSCREEN
KEYWAIT
END


//*************************************
//Function rgbr(col) - return the red component of a 32 bit colour
//*************************************
FUNCTION rgbr: col
red=bAND(col,0xff)
RETURN red
ENDFUNCTION
//*************************************
//Function rgbg(col) - return the green component of a  32 bit colour
//*************************************
FUNCTION rgbg: col
green=bAND(col/0x100,0xff)
RETURN green
ENDFUNCTION
//*************************************
//Function rgbb(col) - return the blue component of a  32 bit colour
//*************************************
FUNCTION rgbb: col
blue=bAND(col/0x10000,0xff)
RETURN blue
ENDFUNCTION
//*************************************
//Function rgba(col) - return the alpha component of a  32 bit colour
//*************************************
FUNCTION rgba: col
alph=bAND(col/0x1000000,0xff)
RETURN alph
ENDFUNCTION
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

Offline PeeJay

  • Mr. Polyvector
  • ***
  • Posts: 244
    • View Profile
    • PeeJays Remakes
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 :D
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity