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).

!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(<color1>);";
    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<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));
  }
}

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: 685 / 690
Referenced in: [show references]