Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

55
LINES

< > BotCompany Repo | #1001881 // Image rendering from random scripts

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1475L/11K/31K).

1  
!752
2  
3  
!include #1000522 // image helper functions
4  
5  
static RGBImage targetImg;
6  
static S bestScript;
7  
static double bestScore;
8  
9  
static ImageSurface imageSurface;
10  
11  
p {
12  
  targetImg = new RGBImage(200, 200, Color.blue);
13  
  
14  
  while true {
15  
    RGBImage img = new RGBImage(200, 200, Color.white);
16  
    S scriptSchema = "fill(<color1>);";
17  
    S script = makeScript(scriptSchema);
18  
    //print ("Script: " + script);
19  
    render(img, script);
20  
    double score = diffToPercent(diff(img, targetImg));
21  
    if (score > bestScore || (score == bestScore && l(script) < l(bestScript))) {
22  
      bestScore = score;
23  
      bestScript = script;
24  
      print("New best score: " + score + ", script: " + script);
25  
      imageSurface = showImage(img, imageSurface);
26  
    }
27  
  }
28  
}
29  
30  
// assumes diff is between 0.0 (full score) and 1.0 (least score)
31  
static double diffToPercent(double diff) {
32  
  ret 100*(1-diff);
33  
}
34  
35  
static S makeScript(S schema) {
36  
  L<S> tok = javaTok(schema);
37  
  int i = findCodeTokens(tok, "<", "color1", ">");
38  
  if (i >= 0) {
39  
    clearAllTokens(tok, i, i+5);
40  
    tok.set(i, quote(randomColor().toString()));
41  
    tok = javaTok(tok);
42  
  }
43  
  ret join(tok);
44  
}
45  
46  
static void render(RGBImage img, S script) {
47  
  L<S> tok = javaTok(script);
48  
  new Matches m;
49  
  for (L<S> s : statements(tok)) {
50  
    if (jmatch("fill(*);", s, m)) {
51  
      fill(img, m.unq(0));
52  
    } else
53  
      fail("unknown statement: " + join(s));
54  
  }
55  
}

Author comment

Began life as a copy of #1001877

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: #1001881
Snippet name: Image rendering from random scripts
Eternal ID of this version: #1001881/1
Text MD5: 2d4b3da37b323364a1fa6637da891883
Transpilation MD5: 5f5f2a00227bb6316ce1432642c06b7c
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:22:30
Source code size: 1446 bytes / 55 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 688 / 696
Referenced in: [show references]