<?php

use codeagent\treemap\Treemap;
use codeagent\treemap\presenter\ImagePresenter;
use codeagent\treemap\presenter\NodeInfo;
use codeagent\treemap\Gradient;

function rgb2hex($rgb)
{
  return "#0203FF";
}

class topisto_treemap_fuzzy
{
  public static function DrawBlock($the_block, $vImage, $x, $y, $width, $height, $mode, $type=1)
  {
    $min  =-1;
    $max  = 0;
    $data = blockchain::getTransactionData($the_block, $type);

    $vBgColor = imagecolorallocate($vImage, 10, 10, 10);
    imagefilledrectangle($vImage, $x, $y, $x+$width, $y+$height, $vBgColor);

    // Calcul des min max
    foreach($data as $v)
    {
      if ($v['value'] > $max) $max = $v['value'];
      if (($v['value'] < $min)||($min == -1)) $min = $v['value'];
    }
    if ($min == $max) $max = $min + 1;

    $tDrawColor  = array();
    $tDrawColor[0] = imagecolorallocatealpha($vImage, 140, 246, 138, 125);
    $tDrawColor[1] = imagecolorallocatealpha($vImage, 200, 200, 200, 125);

    $treemap = new Treemap($data, $width, $height);
    $map = $treemap->getMap();
    $mm = count($map);
    $mmax = $mm - 30;
    foreach($map as $tx)
    {
      $x1 = $x + $tx['_rectangle']->left;
      $y1 = $y + $tx['_rectangle']->top;

      if (($tx['_rectangle']->height < 2)&&($tx['_rectangle']->width < 2))
      {
      	imagesetpixel($vImage, $x1, $y1, $tDrawColor[1]);
      } else {
      	$x2 = $x1;
      	$y2 = $y1 + $tx['_rectangle']->height;
      	// if ($y2 >= ($height-$bandeau)) $y2 = ($height - $bandeau) - 1;

      	$x3 = $x1 + $tx['_rectangle']->width;
      	$y3 = $y2;

      	$x4 = $x3;
      	$y4 = $y1;

      	// ----
      	$w = floor($tx['_rectangle']->width / 4);
      	$h = floor($tx['_rectangle']->height / 4);

      	$ww = floor(200*($mm/$mmax));
        $ww = $h * $w;
      	for($i=0;$i<$ww;$i++)
      	{
      	  $vDrawColor2 = $tDrawColor[$i%2];

      	  $x1_1 = $x1 + floor($w*( rand(0, 100) / 100));
      	  $y1_1 = $y1 + floor($h*( rand(0, 100) / 100));

          $x2_1 = $x2 + floor($w*( rand(0, 100) / 100));
          $y2_1 = $y2 - floor($h*( rand(0, 100) / 100));

          $x3_1 = $x3 - floor($w*( rand(0, 100) / 100));
          $y3_1 = $y3 - floor($h*( rand(0, 100) / 100));

          $x4_1 = $x4 - floor($w*( rand(0, 100) / 100));
          $y4_1 = $y4 + floor($h*( rand(0, 100) / 100));

      	  imageline($vImage, $x1_1, $y1_1, $x2_1, $y2_1, $vDrawColor2);
      	  imageline($vImage, $x2_1, $y2_1, $x3_1, $y3_1, $vDrawColor2);
      	  imageline($vImage, $x3_1, $y3_1, $x4_1, $y4_1, $vDrawColor2);
      	  imageline($vImage, $x4_1, $y4_1, $x1_1, $y1_1, $vDrawColor2);
      	}
      }
      $mm -= 1;
    }
  }
}

?>
