| 1 | package Image::ButtonMaker::ColorCanvas; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use utf8; | ||
| 5 | use Image::Magick; | ||
| 6 | |||
| 7 | my @default_publ = ( | ||
| 8 | |||
| 9 | |||
| 10 | background_color => '#000000', | ||
| 11 | border_color => '#000000', | ||
| 12 | |||
| 13 | border_width => 0, | ||
| 14 | ); | ||
| 15 | |||
| 16 | |||
| 17 | our $error; | ||
| 18 | our $errorstr; | ||
| 19 | |||
| 20 | |||
| 21 | #### Public('ish) Methods ############################# | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $nobody = shift; | ||
| 25 | my @param = @_; | ||
| 26 | |||
| 27 | my $data = {@default_publ, @param}; | ||
| 28 | |||
| 29 | bless $data; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | sub render($$) { | ||
| 34 | my $self = shift; | ||
| 35 | my($tot_w, $tot_h) = @_; | ||
| 36 | |||
| 37 | my $bgcolor = $self->{background_color}; | ||
| 38 | my $fgcolor = $self->{border_color}; | ||
| 39 | my $stroke_w = $self->{border_width}; | ||
| 40 | |||
| 41 | my $res = Image::Magick->new(size => $tot_w.'x'.$tot_h, | ||
| 42 | matte => 1, | ||
| 43 | mattecolor => $bgcolor | ||
| 44 | ); | ||
| 45 | $res->Read("xc:$bgcolor"); | ||
| 46 | |||
| 47 | if($stroke_w) { | ||
| 48 | $res->Draw(fill => $bgcolor, | ||
| 49 | stroke => $fgcolor, | ||
| 50 | primitive => 'rectangle', | ||
| 51 | points => '0,0 '.($tot_w-1).','.($tot_h-1), | ||
| 52 | antialias => 0, | ||
| 53 | strokewidth => $stroke_w, | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | |||
| 57 | return $res; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | #### Format a geometry string #### | ||
| 63 | sub fromtop($$) { | ||
| 64 | my $x = shift; | ||
| 65 | my $y = shift; | ||
| 66 | return '+'.$x.'+'.$y; | ||
| 67 | } | ||
| 68 | |||
| 69 | #### Error Handling | ||
| 70 | sub reset_error { | ||
| 71 | $error = 0; | ||
| 72 | $errorstr = ''; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub set_error { | ||
| 76 | $error = shift; | ||
| 77 | $errorstr = shift; | ||
| 78 | return @_; | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; |