Replacing a color?

BlitzMax Forums/BlitzMax Beginners Area/Replacing a color?

jhans0n(Posted 2006) [#1]
I saw that in the code archives, there's sample code for replacing a color in an image, but they're all for BlitzPlus or Blitz3d. I was wondering if there is a BMax equivalent?

Basically, what I am hoping to do is be able to draw a sprite of a guy on the screen with a solid colored shirt, and then change that shirt to whatever color I want. I know you can kind of do that with SetColor, but it would tint all of the colors in that sprite, not just the shirt.


FlameDuck(Posted 2006) [#2]
Build your sprite from seperate (greyscale) images. That way you can change the colors quickly in realtime using the 3D hardware (SetColor), rather than slowly (ReadPixel / WritePixel) using the CPU.


ImaginaryHuman(Posted 2006) [#3]
ie have a separate `shirt` image from the rest of the character


SillyPutty(Posted 2006) [#4]
what they said :)


Mordax_Praetorian(Posted 2006) [#5]
Its simple, set the colour you dont want to be transparent, draw the sprite on a background of the colour you do want, and then grab the sprite again from where you drew it

I use this sort of thing alot in my own game engine


tonyg(Posted 2006) [#6]
Tricky to do that in Bmax as you can't easily change the mask colour for an image.


Mordax_Praetorian(Posted 2006) [#7]
you cant for an image, but you can for a pixmap, since pixmaps support loading of pngs I dont see any dissadvantage so far to using them, but I havnt played with pixmaps yet so I dont know for sure

Edit: Aww heck, theres no command to grab pixmaps, there goes that plan

Edit2: Wait, yes there is, why isnt that in the pixmaps section?


tonyg(Posted 2006) [#8]
That is true but how does that work for changing more than a single colour?
The colourize demo on BC uses writepixel which is very fast in Bmax so a straight conversion would work well.


Mordax_Praetorian(Posted 2006) [#9]
very simple

Mask the pixmap to the next colour you dont want, and lay it on the next colour you do want, rinse and repeat

My games engine as it currently stands (in B+) has a function for doing just this, so I simply have to give it the image I want changing, the old RGB Values, and the new RGB Values

A character sprite under my engine has a grand total of 14 colours that all change depending on the character's hair/skin/eye/shirt/trouser colours, and armour material, if B+ can do it all before the user notices, I'm sure BMax can as well


tonyg(Posted 2006) [#10]
Can you give an example? I tried to do this before but pixmap masks are destructive so I never got it to work.


Mordax_Praetorian(Posted 2006) [#11]
I can give an example in B+ if you like, but I'm trying to get it working in B-Max now and dont even know enough to get as far as the function

The B+ code does use Images, its only for the BMax that I'm trying to use Pixmaps instead


tonyg(Posted 2006) [#12]
I would prefer a Bmax example as I had something similar to what you suggest working in B2D/B3D but pixmap/maskpixmap seems to works differently from image/maskimage
In fact, as I never got it to work I used the greyscale solution the others posted.
If you can post your Bmax solution when you get it I would appreciate it.


Mordax_Praetorian(Posted 2006) [#13]
The greyscale solution is fine when your the one doing all the work, and only want very basic sprites with large blocks of colour, but if you look at any professional sprite you will usualy see no more than 2 pixels of the same colour in any one place

This makes splitting the sprites up into multiple images VERY impractical indeed, you'd end up either handling a huge ammount of images, or pissing off your pixel artist by giving them the constraint of using large blocks of colour, and sticking to greyscale

I'm going to need a little help to get as far as being able to try anything with my solution.

I'm trying to load a pixmap from a png file using LoadPixmapPNG, but when I compile the code it tells me its expecting a type name

I've tried putting the variable I want it in before (Variable =) the command, and after the command (,Variable and :Variable), and I've tried having the variable within a Type Instance, but the only one that does anything different is putting the :Variable syntax which tells me it wants ")" instead of ":"

I'm baffled


tonyg(Posted 2006) [#14]
local mypixmap:TPixmap=loadpixmap("my.png")
P.S. What version of Bmax are you using?


Mordax_Praetorian(Posted 2006) [#15]
whatever version the trial download is

Graphics 800,600
Local Cheese:PF_RGBA8888 = LoadPixmapPNG("Images/Cursor.png")

Those are the first 2 lines, when I try to compile I get an error saying:

"expecting type name"

and the cursor set to that second line

I have the program saved in the correct directory, and I'm sure the file exists


tonyg(Posted 2006) [#16]
Loadpixmappng returns a TPixmap type and not a PF_RGBA8888 type.
Anyway...
local mypixmap:TPixmap=loadpixmap("mypic.png")


Mordax_Praetorian(Posted 2006) [#17]
With much help from the guys in the irc channel I got that while the site was down

Encountering a new error now, wanna try fixing it myself, ty for the help