Libraryless. Click here for Pure Java version (1533L/11K/32K).
!752 !include #1000522 // image helper functions static RGBImage targetImg; static S bestScript; static double score; static long tries; static ImageSurface imageSurface; //static S strategy = "random;"; static S strategy = "random; vary;"; static boolean warned; p { targetImg = new RGBImage(200, 200, Color.blue); while (score < 100) { for (L<S> s : statements(strategy)) { if (jmatch("random;", s)) { S scriptSchema = "fill(<color1>);"; tryScript(makeScript(scriptSchema)); } else if (jmatch("vary;", s)) { if (bestScript != null) { S variation = varyScript(bestScript); if (variation != null && neq(bestScript, variation)) tryScript(variation); } } else if (!warned) { print("Unknown strategy statement: " + join(s)); warned = true; } } } } static S varyScript(S script) { L<S> tok = javaTok(script); for (int i = 1; i < l(tok); i += 2) if (isQuoted(tok.get(i))) { S s = unquote(tok.get(i)); tok.set(i, quote(varyColor(s))); } ret join(tok); } static S varyColor(S color) { ret varyColor(new RGB(color)).toString(); } static void tryScript(S script) { ++tries; //print ("Script: " + script); RGBImage img = new RGBImage(200, 200, Color.white); render(img, script); double newScore = diffToPercent(diff(img, targetImg)); if (newScore > score /*|| (newScore == score && l(script) < l(bestScript))*/) { score = newScore; bestScript = script; print("Try " + tries + ". 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<S> 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<S> tok = javaTok(script); new Matches m; for (L<S> s : statements(tok)) { if (jmatch("fill(*);", s, m)) { fill(img, m.unq(0)); } else fail("unknown statement: " + join(s)); } }
Began life as a copy of #1001881
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1001882 |
Snippet name: | Image rendering from random scripts v2 |
Eternal ID of this version: | #1001882/1 |
Text MD5: | f7e7196d620b688f7dcc91d13bbb6f0f |
Transpilation MD5: | 93213d3edc3fcf617b288141dad317c1 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-12-01 21:39:17 |
Source code size: | 2392 bytes / 91 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 698 / 715 |
Referenced in: | [show references] |