12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
<?php
// http://media.smwcentral.net/Ersanio/SNESstuff/Colors.html
function PCRGBToSNESColor($red, $green, $blue)
{
return ((($red + ($red <= 248 ? 4 : 0)) >> 3) |
((($green + ($green <= 248 ? 4 : 0)) >> 3) << 5) |
((($blue + ($blue <= 248 ? 4 : 0)) >> 3) << 10)) & 0x7FFF;
}
function arrayToTable($arr, $chunkSize = 8)
{
$table = '';
$command = (strlen($arr[0]) === 2) ? 'db' : 'dw';
$chunks = array_chunk($arr, $chunkSize);
foreach ($chunks as $chunk)
{
$table .= $command . ' $' . join(',$', $chunk) . "\n";
}
return $table;
}
if (isset($_POST['submit']))
{
if ($_FILES['pal']['error'] !== 0)
{
die('something went wrong.');
}
if (pathinfo($_FILES['pal']['name'])['extension'] !== 'pal')
{
die('wrong file extension.');
}
if ($_FILES['pal']['size'] !== 768)
{
die('wrong file size.');
}
$palFile = fopen($_FILES['pal']['tmp_name'], 'rb');
$indexes = [];
$highBytes = [];
$lowBytes = [];
for ($i = 0; $i < 256; $i++)
{
$red = hexdec(bin2hex(fread($palFile, 1)));
$green = hexdec(bin2hex(fread($palFile, 1)));
$blue = hexdec(bin2hex(fread($palFile, 1)));
$color = dechex(PCRGBToSNESColor($red, $green, $blue));
$color = strtoupper($color);
$color = str_pad($color, 4, '0', STR_PAD_LEFT);
$indexes[] = str_pad(strtoupper(dechex($i)), 2, '0', STR_PAD_LEFT);
$highBytes[] = substr($color, 0, 2);
$lowBytes[] = substr($color, 2, 2);
}
fclose($palFile);
echo '<pre>';
echo 'Colors:' . "\n" . arrayToTable($indexes, 16) . "\n";
echo 'LowBytes:' . "\n" . arrayToTable($lowBytes) . "\n";
echo 'HighBytes:' . "\n" . arrayToTable($highBytes);
echo '</pre>';
}
else
{
?>
<h1>.pal to table</h1>
<form action="<?php echo basename($_SERVER['SCRIPT_NAME']); ?>" method="post" enctype="multipart/form-data">
<label for="pal">.pal file: </label><input type="file" name="pal" id="pal" />
<input type="submit" name="submit" value="Go" />
</form>
<?php
}