Libraryless. Click here for Pure Java version (1404L/10K/29K).
1 | !752 |
2 | |
3 | !include #1000522 // image helper functions |
4 | |
5 | p { |
6 | RGBImage targetImg = new RGBImage(200, 200, Color.blue); |
7 | |
8 | RGBImage img = new RGBImage(200, 200, Color.white); |
9 | render(img, "fill(\"808000\");"); |
10 | print("Diff: " + diff(img, targetImg)); |
11 | showImage(img); |
12 | } |
13 | |
14 | static void render(RGBImage img, S script) { |
15 | L<S> tok = javaTok(script); |
16 | new Matches m; |
17 | for (L<S> s : statements(tok)) { |
18 | if (jmatch("fill(*);", s, m)) { |
19 | fill(img, m.unq(0)); |
20 | } else |
21 | fail("unknown statement: " + join(s)); |
22 | } |
23 | } |
24 | |
25 | static void fill(RGBImage img, S col) { |
26 | RGB rgb = new RGB(col); |
27 | int w = img.getWidth(), h = img.getHeight(); |
28 | for (int y = 0; y < h; y++) |
29 | for (int x = 0; x < w; x++) |
30 | img.setPixel(x, y, rgb); |
31 | } |
32 | |
33 | // assumes that it's a list of statements; return them one by one |
34 | static L<L<S>> statements(L<S> tok) { |
35 | new L<L<S>> l; |
36 | int i = 0; |
37 | while (i < l(tok)) { |
38 | int j = indexOf(tok, ";", i); |
39 | if (j < 0) { |
40 | if (l(tok)-i >= 3) |
41 | l.add(tok.subList(i, l(tok))); |
42 | break; |
43 | } else { |
44 | l.add(tok.subList(i, j+2)); // actual CNC - we may return some comments twice, but we don't look at them anyways. |
45 | i = j+1; // go to non-code token |
46 | } |
47 | } |
48 | ret l; |
49 | } |
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: | #1001877 |
Snippet name: | Image rendering from script, with comparison to target image |
Eternal ID of this version: | #1001877/1 |
Text MD5: | 89b08b6f26bb98bd943ebf0a5a5cae0d |
Transpilation MD5: | be968a40173092f2046728f7fcdbe469 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-12-01 17:36:08 |
Source code size: | 1257 bytes / 49 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 728 / 783 |
Referenced in: | [show references] |