update page now

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

Gusts Kaksis
18 years ago
This code generate a simple colortable - not a very acurate one (it would be good to define fade of color - more fading)

<?php
set_time_limit(200);
/*
Custommize this to see some nice changes:
*/
$width = 360;   // degrees
$height = 18;   // byte
$offset = -60;    // offset of color hue

/*
Main programm:
Here comes transformations - width to degrees and height to intesity
*/
$w2deg = $width/360;
$h2byte = $height/255;
$byte2deg = 255/360;
$im = imagecreatetruecolor($width,$height);
for ($x = 0; $x < $width; $x ++){
  /*
  Transform X to degrees
  */
  $x_pos = $x/$w2deg;
  /*
  Intensity position (where max intensity is 255) on 360 degree scale.
  0 = red
  1 = green
  2 = blue
  */
  $rgb_pos[0] = sin(deg2rad($x_pos) - deg2rad($offset + 0));
  $rgb_pos[1] = sin(deg2rad($x_pos) - deg2rad($offset + 120));
  $rgb_pos[2] = sin(deg2rad($x_pos) - deg2rad($offset + 240));
  /*
  Calculate intesity at current point 0 - 255
  */
  $rgb_col[0] = 127 + 127 * $rgb_pos[0];
  $rgb_col[1] = 127 + 127 * $rgb_pos[1];
  $rgb_col[2] = 127 + 127 * $rgb_pos[2];

  /*
  White -> color -> Black loop
  */
  for ($y = 0; $y < $height; $y ++){
    /*
    Transform Y to intensity (-255 to 255)
    */
    $y_pos = -255 + ($y/$h2byte) * 2;
    $rgb_out[0] = $rgb_col[0] - $y_pos;
    $rgb_out[1] = $rgb_col[1] - $y_pos;
    $rgb_out[2] = $rgb_col[2] - $y_pos;

    /*
    If we go over 255 or under 0 we normalize it
    */
    foreach($rgb_out as $key => $col){
      if ($col > 255){
        $rgb_out[$key] = 255;
      } else if ($col < 0){
        $rgb_out[$key] = 0;
      }
    }
    /*
    Put a pixel
    */
    $col = imagecolorallocate($im,$rgb_out[0],$rgb_out[1],$rgb_out[2]);
    imagesetpixel($im,$x,$y,$col);
  }
}
/*
Test output
*/
imagejpeg($im,'colortable.jpg');
echo '<img src="colortable.jpg">';
?>

<< Back to user notes page

To Top