-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_draw_gradient.dart
52 lines (41 loc) · 1.29 KB
/
1_draw_gradient.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'dart:io';
import 'package:image/image.dart';
import 'openfile.dart';
import 'pixel.dart';
/**
* Create and populate an array with gradient of pixels.
* Save it as a PNG.
* Open it via the default Mac app.
*/
void main() {
const width = 1024;
const height = 768;
List<Pixel> pixels = List<Pixel>(width * height);
Image im = Image(width, height);
List imagePixels = im.getBytes();
// Image is prepped - draw!
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
var r = y.toDouble() / width.toDouble();
var g = x.toDouble() / height.toDouble();
var b = 0.0;
pixels[x + (y * width)] = Pixel(r, g, b);
}
}
var pixelpos = 0;
var maxsize = width * height * 4;
for (var pos = 0; pos < maxsize; pos += 4) {
var pix = pixels[pixelpos];
imagePixels[pos] = Pixel.toPixelValue(pix.r);
imagePixels[pos + 1] = Pixel.toPixelValue(pix.g);
imagePixels[pos + 2] = Pixel.toPixelValue(pix.b);
imagePixels[pos + 3] = 255;
pixelpos += 1;
}
Image image = Image.fromBytes(width, height, imagePixels);
// Done Drawing, now save it.
List<int> png = encodePng(image);
new File('output_gradient.png').writeAsBytesSync(png);
// Open the file in the Mac previewer
OpenFile().openFileInPreview('output_gradient.png');
}