Here's something I whipped up. The CSS formatting is horrible but the amount of it in there was annoying me since it's not the point of the code so I packed it all together.
PHP Code:<?php
// declarations
$cols = Array($_POST['colourone'], $_POST['colourtwo'], $_POST['colourthree'], $_POST['colourfour']);
$colours = Array(); $final = Array();
// remove # symbol
foreach ($cols as $colour) array_push($colours, preg_replace('/#/', '', $colour));
// nested for loop to only calculate midpoint between unique unordered pairs
for ($i = 0; $i < count($colours); $i++) {
for ($j = $i+1; $j < count($colours); $j++) {
$rgb_one = getRgb($colours[$i]);
$rgb_two = getRgb($colours[$j]);
$rgb_three = getMidColour($rgb_one, $rgb_two);
$triplet = Array(implode($rgb_one), implode($rgb_three), implode($rgb_two));
array_push($final, $triplet);
}
}
// returns array containing individual RGB components in hex
function getRgb($col) {
return Array(
substr($col, 0, 2),
substr($col, 2, 2),
substr($col, 4, 2)
);
}
// calculates the midpoint between two colours
// double conversion is performed to convert from a string for arithmetic
function getMidColour($rgb_one, $rgb_two) {
$red = dechex((hexdec('0x'. $rgb_one[0]) + hexdec('0x'. $rgb_two[0])) / 2);
$green = dechex((hexdec('0x'. $rgb_one[1]) + hexdec('0x'. $rgb_two[1])) / 2);
$blue = dechex((hexdec('0x'. $rgb_one[2]) + hexdec('0x'. $rgb_two[2])) / 2);
return Array($red, $green, $blue);
}
?>
<html>
<head>
<title>Colour generator</title>
<style>
body { background: #FAFAFF; font-family: Tahoma; margin: 35px; }
td { border: 1px solid black; }
tr.headings>td { border: 0px; font-size: 10px; text-align: center; }
.swatchtable { border: 1px dotted black; padding: 10px; }
tr.swatches>td { width: 70px; height: 50px; }
.formdiv { float: left; border: 1px dotted black; padding: 10px; width: 500px; }
</style>
</head>
<body>
<?php if ($_POST['submit']) { ?>
<div style="float: right;">
<h3>Here is your colour table:</h3>
<table class="swatchtable">
<tr class="headings">
<td>First</td>
<td>Middle</td>
<td>Second</td>
</tr>
<?php foreach ($final as $triplet) { ?>
<tr class="swatches">
<td style="background: #<?= $triplet[0] ?>;"> </td>
<td style="background: #<?= $triplet[1] ?>;"> </td>
<td style="background: #<?= $triplet[2] ?>;"> </td>
</tr>
<?php } ?>
</table>
</div>
<?php } ?>
<br /><br />
<div class="formdiv">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="colours">
<h3>Enter four colours in hex:</h3>
<input type="text" size="7" name="colourone" />
<input type="text" size="7" name="colourtwo" />
<input type="text" size="7" name="colourthree" />
<input type="text" size="7" name="colourfour" />
<input type="submit" name="submit" value="Submit" />
</form>
</div>
</body>
</html>





Reply With Quote