Draw a hexagon and fill it with color using Imager

Draw a hexagon and fill it with color using Imager

Source code.

use strict;
use warnings;

use Imager;
use Imager::Color;
use Imager::Fill;

my $xsize = 500;
my $ysize = 250;

my $img = Imager->new(xsize => $xsize, ysize => $ysize, channels => 4);

# Background white
$img->box(color => Imager::Color->new(255, 255, 255), xmin=>0, ymin=>0, xmax=>$xsize, ymax=>$ysize, 
          filled=>1);

# Blue Hexagon
{
  my $blue = Imager::Color->new( 0, 0, 255);
  my $fill = Imager::Fill->new(solid=> $blue);
  
  $img->polygon(points => [[50, 50], [100, 50], [125, 100], [100, 200], [50, 200], [25, 100]], color => $blue, fill => $fill, aa => 1);
}

# Green opacity hatch Hexagon
{
  my $green = Imager::Color->new( 0, 255, 0, 200);
  my $fill = Imager::Fill->new(hatch => "check2x2", fg => $green);
  $img->polygon(points => [[250, 50], [300, 50], [325, 100], [300, 200], [250, 200], [225, 100]], color => $green, fill => $fill, aa => 1);
}

# inverse virtical
$img->flip(dir => 'v');


$img->write( file => 'rokkaku.png')
  or die $img->errstr;

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.