!752 !include #1000522 // image helper functions static RGBImage targetImg; static S bestScript; static double bestScore; static ImageSurface imageSurface; p { targetImg = new RGBImage(200, 200, Color.blue); while true { RGBImage img = new RGBImage(200, 200, Color.white); S scriptSchema = "fill();"; S script = makeScript(scriptSchema); //print ("Script: " + script); render(img, script); double score = diffToPercent(diff(img, targetImg)); if (score > bestScore || (score == bestScore && l(script) < l(bestScript))) { bestScore = score; bestScript = script; print("New best score: " + score + ", script: " + script); imageSurface = showImage(img, imageSurface); } } } // assumes diff is between 0.0 (full score) and 1.0 (least score) static double diffToPercent(double diff) { ret 100*(1-diff); } static S makeScript(S schema) { L tok = javaTok(schema); int i = findCodeTokens(tok, "<", "color1", ">"); if (i >= 0) { clearAllTokens(tok, i, i+5); tok.set(i, quote(randomColor().toString())); tok = javaTok(tok); } ret join(tok); } 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)); } }