!752 !include #1000522 // image helper functions p { RGBImage targetImg = new RGBImage(200, 200, Color.blue); RGBImage img = new RGBImage(200, 200, Color.white); render(img, "fill(\"808000\");"); print("Diff: " + diff(img, targetImg)); showImage(img); } static void render(RGBImage img, S script) { L tok = javaTok(script); new Matches m; for (L s : statements(tok)) { if (jmatch("fill(*);", s, m)) { fill(img, m.unq(0)); } else fail("unknown statement: " + join(s)); } } static void fill(RGBImage img, S col) { RGB rgb = new RGB(col); int w = img.getWidth(), h = img.getHeight(); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) img.setPixel(x, y, rgb); } // assumes that it's a list of statements; return them one by one static L> statements(L tok) { new L> l; int i = 0; while (i < l(tok)) { int j = indexOf(tok, ";", i); if (j < 0) { if (l(tok)-i >= 3) l.add(tok.subList(i, l(tok))); break; } else { l.add(tok.subList(i, j+2)); // actual CNC - we may return some comments twice, but we don't look at them anyways. i = j+1; // go to non-code token } } ret l; }