Libraryless. Click here for Pure Java version (1533L/11K/32K).
1 | !752 |
2 | |
3 | !include #1000522 // image helper functions |
4 | |
5 | static RGBImage targetImg; |
6 | static S bestScript; |
7 | static double score; |
8 | static long tries; |
9 | static ImageSurface imageSurface; |
10 | |
11 | //static S strategy = "random;"; |
12 | static S strategy = "random; vary;"; |
13 | |
14 | static boolean warned; |
15 | |
16 | p { |
17 | targetImg = new RGBImage(200, 200, Color.blue); |
18 | |
19 | while (score < 100) { |
20 | for (L<S> s : statements(strategy)) { |
21 | if (jmatch("random;", s)) { |
22 | S scriptSchema = "fill(<color1>);"; |
23 | tryScript(makeScript(scriptSchema)); |
24 | } else if (jmatch("vary;", s)) { |
25 | if (bestScript != null) { |
26 | S variation = varyScript(bestScript); |
27 | if (variation != null && neq(bestScript, variation)) |
28 | tryScript(variation); |
29 | } |
30 | } else if (!warned) { |
31 | print("Unknown strategy statement: " + join(s)); |
32 | warned = true; |
33 | } |
34 | } |
35 | } |
36 | } |
37 | |
38 | static S varyScript(S script) { |
39 | L<S> tok = javaTok(script); |
40 | for (int i = 1; i < l(tok); i += 2) |
41 | if (isQuoted(tok.get(i))) { |
42 | S s = unquote(tok.get(i)); |
43 | tok.set(i, quote(varyColor(s))); |
44 | } |
45 | ret join(tok); |
46 | } |
47 | |
48 | static S varyColor(S color) { |
49 | ret varyColor(new RGB(color)).toString(); |
50 | } |
51 | |
52 | static void tryScript(S script) { |
53 | ++tries; |
54 | //print ("Script: " + script); |
55 | RGBImage img = new RGBImage(200, 200, Color.white); |
56 | render(img, script); |
57 | double newScore = diffToPercent(diff(img, targetImg)); |
58 | if (newScore > score /*|| (newScore == score && l(script) < l(bestScript))*/) { |
59 | score = newScore; |
60 | bestScript = script; |
61 | print("Try " + tries + ". New best score: " + score + ", script: " + script); |
62 | imageSurface = showImage(img, imageSurface); |
63 | } |
64 | } |
65 | |
66 | // assumes diff is between 0.0 (full score) and 1.0 (least score) |
67 | static double diffToPercent(double diff) { |
68 | ret 100*(1-diff); |
69 | } |
70 | |
71 | static S makeScript(S schema) { |
72 | L<S> tok = javaTok(schema); |
73 | int i = findCodeTokens(tok, "<", "color1", ">"); |
74 | if (i >= 0) { |
75 | clearAllTokens(tok, i, i+5); |
76 | tok.set(i, quote(randomColor().toString())); |
77 | tok = javaTok(tok); |
78 | } |
79 | ret join(tok); |
80 | } |
81 | |
82 | static void render(RGBImage img, S script) { |
83 | L<S> tok = javaTok(script); |
84 | new Matches m; |
85 | for (L<S> s : statements(tok)) { |
86 | if (jmatch("fill(*);", s, m)) { |
87 | fill(img, m.unq(0)); |
88 | } else |
89 | fail("unknown statement: " + join(s)); |
90 | } |
91 | } |
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: | 700 / 720 |
Referenced in: | [show references] |