Surprise! We've been running on hardware provided by BuyVM for a few months and wanted to show them a little appreciation.
Running a paste site comes with unique challenges, ones that aren't always obvious and hard to control. As such, BuyVM offered us a home where we could worry less about the hosting side of things and focus on maintaining a clean and useful service! Go check them out and show them some love!
Description: .pal to table
Submitted on July 12, 2016 at 09:47 AM

Section 1 (PHP)

<?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
	}