
Added by thebman220 on January-29-2009, 3:15 am
<?="<?xml version="1.0" encoding="utf-8"?>n"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tween Test</title>
<style type="text/css">
body {
font-family: monospace;
}
td {
white-space: pre;
padding: 5px;
}
th {
font-size: larger;
}
</style>
</head>
<body>
<?php
define("TWEEN_EVEN",1); // An even spread of all numbers
define("TWEEN_HIGH",2); // Few smaller numbers, many larger
define("TWEEN_LOW",3); // Few larger numbers, many smaller
define("TWEEN_HIGH_LOW",4); // Few smaller and larger numbers, many middle
define("TWEEN_LOW_HIGH",5); // Few middle, many smaller and larger numbers
// returns a number between $start ans $end
// for the percentage $num is between $low and $hight
// for the function defined by $path
function tween($start,$end,$low,$num,$high,$path=TWEEN_EVEN) {
if ($start==$end||$low==$high||$num==$low) {
return $start;
} elseif ($num==$high) {
return $end;
}
$part=($num-$low)/($high-$low);
switch ($path) {
case TWEEN_HIGH:
$amount=pow($part,1/3);
break;
case TWEEN_LOW:
$amount=pow($part,3);
#$amount=pow($part-1,3)+1;
break;
case TWEEN_HIGH_LOW:
$amount=(pow(2*$part-1,3)+1)/2;
break;
case TWEEN_LOW_HIGH:
// PHP doesn't get that (-x)^3=-(x^3):
$temp=2*$part-1;
$coeff=$temp<0?-1:1;
$amount=($coeff*pow($coeff*$temp,1/3)+1)/2;
#$amount=$part>1/2
# ?pow(2*($part-1),3)/2+1
# :(pow(2*$part,3)/2;
break;
case TWEEN_EVEN:
default:
$amount=$part;
break;
}
return round(($end-$start)*$amount)+$start;
}
// works like tween(), but with hexdecimal colors
// returns the color where all rgb channels have been tweened
function tween_color($start,$end,$low,$num,$high,$path=TWEEN_EVEN) {
$color="";
for ($i=0; $i<6; $i+=2) {
$c=tween(
255-base_convert(substr($start,$i,2),16,10),
255-base_convert(substr($end,$i,2),16,10),
$low,
$num,
$high,
$path
);
$c=base_convert(255-$c,10,16);
if (strlen($c)<2) {
$c="0$c";
}
$color.=$c;
}
return $color;
}
////////////////////////////////////////////////////////////////////
function opposite($color) {
$white=0;
for ($i=0; $i<6; $i+=2) {
$white+=base_convert(substr($color,$i,2),16,10)>127?1:0;
}
return $white>=2?"000000":"ffffff";
}
////////////////////////////////////////////////////////////////////
test(false,"dddddd");
echo "<br />n<hr />n<br />n";
$data=array();
for ($i=0; $i<256; $i++) {
$data[]=$i;
}
test($data,"ff0000","00ff00");
function test(
$data=false,
$color_min="ffffff",
$color_max="000000",
$font_min=8,
$font_max=15,
$bold_min=1,
$bold_max=9
) {
if (!is_array($data)) {
$data=array();
for ($i=0; $i<mt_rand(10,100); $i++) {
$data[]=mt_rand(0,255);
}
}
$min_num=min($data);
$max_num=max($data);
echo "<table border='2px'>n";
echo "<tr>n";
for ($path=1; $path<=5; $path++) {
echo "<th>";
switch ($path) {
case 1: echo "Normal"; break;
case 2: echo "High"; break;
case 3: echo "Low"; break;
case 4: echo "High to Low"; break;
case 5: echo "Low to High"; break;
}
echo "</th>n";
}
echo "</tr>n";
foreach ($data as $num) {
echo "<tr>n";
for ($path=1; $path<=5; $path++) {
$font=tween($font_min,$font_max,$min_num,$num,$max_num,$path);
$bold=tween($bold_min,$bold_max,$min_num,$num,$max_num,$path);
$color=tween_color($color_min,$color_max,$min_num,$num,$max_num,$path);
echo "<td style='font-size:{$font}pt;font-weight:{$bold}00;background-color:#$color;color:#".opposite($color)."'>$num {$font}pt {$bold}00 #$color</td>n";
}
echo "</tr>n";
}
echo "</table>n";
}
?>
</body>
</html>
Added by thebman220 on May-2-2009, 4:40 am
Added by thebman220 on April-29-2009, 11:47 pm
Added by thebman220 on April-5-2009, 1:48 pm
Added by thebman220 on January-29-2009, 3:04 am
Added by thebman220 on January-3-2009, 6:25 pm