Horizon88 wrote:That's really cool, NogDog. 😃 Would you mind posting the code for that?
I think this was something I did when I was first trying to get a handle on PHP classes and objects, so don't laugh too much:
<?php
class Colors
{
var $color = NULL; // RGB in hex
var $rx; // red in hex
var $gx; // green in hex
var $bx; // blue in hex
var $tint; // RGB tint in hex
var $shade; // RGB shade in hex
var $tone; // RGB tone in hex
// constructor
function Colors($color = 'FFFFFF')
{
$this->setColor($color);
$this->setTint();
$this->setShade();
$this->setTone();
}
function setColor($color)
{
if(!preg_match('/^[0-9a-f]{6,6}$/i', $color))
{
user_error("Colors::setColor(): invalid color '$color'", E_USER_WARNING);
return(FALSE);
}
$this->color = $color;
list($this->rx,$this->gx,$this->bx) =
explode(',', chunk_split($color, 2, ','));
return(TRUE);
}
function setTint()
{
$this->tint = sprintf('%02X%02X%02X', (hexdec($this->rx) + 255) / 2,
(hexdec($this->gx) + 255) / 2, (hexdec($this->bx) + 255) / 2);
}
function setShade()
{
$this->shade = sprintf('%02X%02X%02X', hexdec($this->rx) / 2,
hexdec($this->gx) / 2, hexdec($this->bx) / 2);
}
function setTone()
{
$this->tone = sprintf('%02X%02X%02X', (hexdec($this->rx) + 127) / 2,
(hexdec($this->gx) + 127) / 2, (hexdec($this->bx) + 127) / 2);
}
function hex2dec($color)
{
if(preg_match('/^[0-9a-f]{6,6}$/i', $color))
{
list($r,$g,$b) = explode(',', chunk_split($color,2,','));
return sprintf("%d,%d,%d", hexdec($r), hexdec($g), hexdec($b));
}
user_error("Colors::hex2dec(): Invalid color '$color'", E_USER_WARNING);
return(FALSE);
}
function hex2pct($color)
{
if(preg_match('/^[0-9a-f]{6,6}$/i', $color))
{
list($r,$g,$b) = explode(',', chunk_split($color,2,','));
return sprintf("%d%%,%d%%,%d%%", round(hexdec($r)/2.55),
round(hexdec($g)/2.55), round(hexdec($b)/2.55));
}
user_error("Colors::hex2dec(): Invalid color '$color'", E_USER_WARNING);
return(FALSE);
}
}
$color = (isset($_GET['color'])) ? strtoupper($_GET['color']) : '999999';
$rgb = new Colors($color);
$rgbTint = new Colors($rgb->tint);
$rgbShade = new Colors($rgb->shade);
$rgbTone = new Colors($rgb->tone);
function printCell($color)
{
echo "<td style='background-color: #$color')<span class='black'>#$color</span>\n";
echo "<br><span class='white'>#$color</span></td>\n";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<title>Colors: Tints, Shades, and Tones</title>
<!-- link rel='stylesheet' href='style.css' type='text/css' -->
<style type="text/css">
<!--
body {
margin: 0;
padding: 1em;
font: 90% arial, helvetica, sans-serif;
}
table {
border-collapse: separate;
border-spacing: 3px;
border: none;
}
td {
padding: 20px 10px;
width: 8em;
text-align: center;
font-weight: bold;
}
.white {
color: white;
}
.black {
color: black;
}
.clear {
clear: both;
}
-->
</style>
</head>
<body>
<h2>Color Tints, Shades, and Tones</h2>
<table summary="color grid">
<tr>
<?php
printCell($rgb->color);
printCell($rgb->tint);
printCell($rgbTint->tint);
?>
</tr>
<tr>
<?php
printCell($rgb->shade);
printCell($rgb->tone);
printCell($rgbTint->tone);
?>
</tr>
<tr>
<?php
printCell($rgbShade->shade);
printCell($rgbShade->tone);
printCell($rgbTone->tone);
?>
</tr>
</table>
<h3>Specify a Color</h3>
<form action="<?php echo $_SERVER['php_self'] ?>" method="get">
<p>
<label for="color">Color (6-digit hexadecimal, no "#"):</label>
<input type="text" name="color" id="color" size="6" maxlength="6"
value="<?php echo $color; ?>">
<input type="submit" value="Show Colors">
</p>
</form>
</body>
</html>