Simple palette swap code I wrote up tonight. GIF file needs to be 4-color, preferable 87a format, or at least that's what works for me. This kind of script has been done before, but I'll share my version to you, my Wallachian brethren. What you do with it is up to you. As for me, I have no flippin' idea where to go from here. Damn attribute tables.
Edit/Update: My GIF files were created in Paintshop Pro, saved in 4-bit color with GIF format 87a. Figured I should throw that out there since some programs save GIF files differently.
/*
This script simply modifies the higher three palette entries of a 4-color GIF.
The script looks for the specified file in the Temp directory and rewrites it.
argument0: GIF file (string, sans extension)
argument1: Color 1 (real)
argument2: Color 2 (real)
argument3: Color3 (real)
*/
var i,chr,col,pal;
chr = file_bin_open(temp_directory+'\'+argument0+".gif",1);
for(i=$10;i<$19;i+=3)
{
col = (i-$10) mod 3;
switch (i-$10)div 3
{
case 0:
pal = argument1;
break;
case 1:
pal = argument2;
break;
case 3:
pal = argument3;
break;
}
file_bin_seek(chr,i);
switch col
{
case 0:
file_bin_write_byte(chr,color_get_red(pal));
break;
case 1:
file_bin_write_byte(chr,color_get_green(pal));
break;
case 2:
file_bin_write_byte(chr,color_get_blue(pal));
break;
}
}
file_bin_close(chr);Edit/Update #2: Here's a sample of the code modifying a GIF. Top half is the original file, bottom half is the file after using this code:
pal_swap("chr73",make_color_rgb(24,88,88),make_color_rgb(32,120,120),make_color_rgb(64,192,192));
Q&AQ: How does it work?
A: GIF files are very simple image formats. The palette is stored near the beginning of the file starting at byte $0D. The first color in the file is $0D-$0F, each subsequent palette entry takes up three bytes in typical RGB order. This code simply opens a GIF file and rewrites those bytes.
Q: Is it only for NES-style quadrachromatic images?
A: No. Although this script was written with quadrachromatic images in mind, the beauty of my script is it should be able to handle 16-bit graphics. The switch statement that sets
pal can easily be expanded to allow fifteen editable colors, more than enough.
Q: What makes your code different from the others?
A: For starters, you don't need to specify the color to change, which most of the scripts I've seen require. With a simple edit, I could make this code usable with a specific palette index whereby you'd just use 0 for all the other corresponding arguments. That's a simple enough edit that anyone could do it.
Q: Why use the temporary directory?
A: You wouldn't want the player to easily mess with those files now, would you? If they know anything about GM, they'll know where to find your graphics, but at least the newbs won't know.
Q: How fast is this code?
A: I haven't tested. Sorry.
Q: How do I use this in my game?
A: That's up to you to figure out. I'm still trying to figure out how I'll incorporate it into my tile maps.