-
Notifications
You must be signed in to change notification settings - Fork 6
/
lang.rkt
115 lines (96 loc) · 3.02 KB
/
lang.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#lang racket/base
(require (for-syntax racket/base)
stamps
racket/draw
racket/class
racket/math
stamps/private/color-utils)
(provide (all-from-out racket/math)
(except-out (all-from-out stamps)
render-shape)
(except-out (all-from-out racket/base)
#%module-begin)
(rename-out [module-begin #%module-begin])
background
width
height
filename
start-shape
quality
translate-x
translate-y
scale-x
scale-y
t
r
s
h
sat
b
a
sx
sy
x
y
ri
rr)
; Parameters
(define background (make-parameter '(0 0 1)))
(define width (make-parameter 1024))
(define height (make-parameter 768))
(define filename (make-parameter "output.png"))
(define filetype (make-parameter 'png))
(define start-shape (make-parameter #f))
(define quality (make-parameter 100))
; Utilities, aliases and syntax
(define-syntax-rule (translate-x x)
(translate x 0))
(define-syntax-rule (translate-y y)
(translate 0 y))
(define-syntax-rule (scale-x x)
(scale x 0))
(define-syntax-rule (scale-y y)
(scale 0 y))
(define-syntax t (make-rename-transformer #'translate))
(define-syntax r (make-rename-transformer #'rotate))
(define-syntax s (make-rename-transformer #'scale))
(define-syntax h (make-rename-transformer #'hue))
(define-syntax sat (make-rename-transformer #'saturation))
(define-syntax b (make-rename-transformer #'brightness))
(define-syntax a (make-rename-transformer #'alpha))
(define-syntax x (make-rename-transformer #'translate-x))
(define-syntax y (make-rename-transformer #'translate-y))
(define-syntax sx (make-rename-transformer #'scale-x))
(define-syntax sy (make-rename-transformer #'scale-y))
(define-syntax ri (make-rename-transformer #'random-integer))
(define-syntax rr (make-rename-transformer #'random-real))
; Module wrapper
(define-syntax-rule (module-begin expr ...)
(#%module-begin
expr ...
(when (not (start-shape))
(fprintf (current-error-port)
"start shape not specified, please set the \"start-shape\" parameter\n")
(exit 1))
; create bitmap and rendering context
(define bmp (make-bitmap (width) (height)))
(define dc (send bmp make-dc))
; set background
(define-values (r g b) (apply hsb->rgb (background)))
(define bg-color (make-object color% r g b 1.0))
(send dc set-background bg-color)
(send dc clear)
; render
(printf "rendering...")
(flush-output)
(define-values (res cpu real gc)
(time-apply render-shape
(list ((start-shape)) dc)))
(printf " ~a shapes, ~a ms\n" (car res) real)
; save
(when (filename)
(printf "saving to ~ax~a image ~a (~a)..." (width) (height) (filename) (filetype))
(flush-output)
(send bmp save-file (filename) (filetype) (quality))
(printf "done\n."))
bmp))