RosettaCodeData/Task/Bitmap-Write-a-PPM-file/Perl-6/bitmap-write-a-ppm-file.pl6

31 lines
698 B
Raku
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
class Pixel { has uint8 ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @!data;
method fill(Pixel $p) {
@!data = $p.clone xx ($!width*$!height)
}
method pixel(
$i where ^$!width,
$j where ^$!height
--> Pixel
) is rw { @!data[$i*$!height + $j] }
method data { @!data }
}
2015-02-20 00:35:01 -05:00
role PPM {
method P6 returns Blob {
"P6\n{self.width} {self.height}\n255\n".encode('ascii')
~ Blob.new: flat map { .R, .G, .B }, self.data
2013-04-10 22:43:41 -07:00
}
}
2016-12-05 22:15:40 +01:00
my Bitmap $b = Bitmap.new(width => 125, height => 125) but PPM;
for flat ^$b.height X ^$b.width -> $i, $j {
2015-02-20 00:35:01 -05:00
$b.pixel($i, $j) = Pixel.new: :R($i*2), :G($j*2), :B(255-$i*2);
}
$*OUT.write: $b.P6;