-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
422 lines (362 loc) · 11.7 KB
/
Main.elm
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
module Benoit exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import Task exposing (perform)
import Array exposing (Array, fromList, toList, length, filter)
import Window exposing (Size, resizes, size)
import Maybe exposing (withDefault)
import Random exposing (..)
import Ports exposing (..)
import Json.Encode as EJson
import ParseInt exposing (toHex, parseIntHex)
import Time exposing (..)
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subs
}
-- MODEL
type alias CN =
{ r : Float
, i : Float
}
type alias C =
{ r : Float
, g : Float
, b : Float
}
type alias Cband =
{ c : C
, v : Float
}
type alias Model =
{ cdepth : Int
, cres : Int
, res : Int
, p : CN
, u : Float
, v : Float
, cosampl : Int
, grad : Array Cband
, comp : List (List Int)
, gamma : { act : Bool, g : Float}
, renderPar : {comp : Int, paint: Int, proc: Int}
}
init : (Model, Cmd Msg)
init =
Model 50 400 10 (CN -2.5 2) 4.0 -4.0 1 (Array.fromList [(Cband (C 1 1 1) 0), (Cband (C 0 0 1) 0.25), (Cband (C 1 0 0) 0.5), (Cband (C 1 1 0) 0.75), (Cband (C 0 0 0) 1)]) [[0]] { act = True, g = 2.2 } { comp = -1, paint = -1, proc = -1 }
! [ Task.perform Res Window.size ]
-- UPDATE
type Msg
= Compute
| Paint
| Res Window.Size
| Rect (Int, Int, Int)
| ChangeDepth String
| ChangeOsampl String
| ChangeRes String
| ChangeGrad String Int String
| SwitchGamma Bool
| ChangeGamma String
| Render Time
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Compute ->
compute model
! [ Task.perform identity (Task.succeed Paint) ]
Paint ->
model
! [ paint model ]
Render _ ->
let
rp = model.renderPar
in
if rp.comp >= 0 || rp.paint >= 0 then
if rp.comp >= 3 then
{ model | renderPar = { comp = -1, paint = -1, proc = 1 } }
! [ Task.perform identity (Task.succeed Compute) ]
else if rp.paint >= 3 then
{ model | renderPar = { rp | paint = -1 } }
! [ Task.perform identity (Task.succeed Paint) ]
else
{ model | renderPar = { comp = if rp.comp >= 0 then rp.comp + 1 else rp.comp, paint = if rp.paint >= 0 then rp.paint + 1 else rp.paint, proc = -1 } }
! []
else
{ model | renderPar = { rp | proc = -1 } }
! []
Res s ->
{ model | res = s.height }
! [ Task.perform identity (Task.succeed Compute) ]
Rect (ax,ay, bx) ->
let
ores = 1.0 / toFloat (if model.cres == -1 then model.res else model.cres)
p_ar = ores * toFloat ax
p_ai = ores * toFloat ay
uv_a = ores * (toFloat bx)
np = CN (model.p.r + p_ar*model.u) (model.p.i + p_ai*model.v)
nu = uv_a * model.u
nv = uv_a * model.v
in
{ model | p = np, u = nu, v = nv }
! [ Task.perform identity (Task.succeed Compute) ]
ChangeDepth d ->
let
nd = Result.withDefault 50 (String.toInt d)
rp = model.renderPar
in
{ model | cdepth = nd, renderPar = { rp | comp = 0} }
! []
ChangeRes d ->
let
nd = Result.withDefault -1 (String.toInt d)
rp = model.renderPar
in
{ model | cres = nd, renderPar = { rp | comp = 0} }
! []
ChangeOsampl o ->
let
no = Result.withDefault 1 (String.toInt o)
rp = model.renderPar
in
{ model | cosampl = if no > 0 then no else 1, renderPar = { rp | comp = 0} }
! []
ChangeGrad t i val ->
case Array.get i model.grad of
Nothing ->
model ! []
Just v ->
case t of
"c" ->
{ model | grad = Array.set i { v | c = hexToCol val } model.grad }
! [ Task.perform identity (Task.succeed Paint) ]
"v" ->
case String.toFloat val of
Err _ ->
model ! []
Ok a ->
let
g = Array.set i { v | v = a } model.grad
rp = model.renderPar
in
{ model | grad = g, renderPar = { rp | paint = 0 } }
! []
"d" ->
let
rp = model.renderPar
a = Array.slice 0 i model.grad
b = Array.slice (i + 1) (Array.length model.grad) model.grad
c = Array.append a b
in
{ model | grad = c, renderPar = { rp | paint = 0 } }
! []
"a" ->
let
rp = model.renderPar
c = Array.push (Cband (C 0 0 0) 1) model.grad
in
{ model | grad = c, renderPar = { rp | paint = 0 } }
! []
_ ->
model ! []
SwitchGamma g ->
let
og = model.gamma
rp = model.renderPar
in
{ model | gamma = { og | act = g }, renderPar = { rp | paint = 0 } }
! []
ChangeGamma g ->
let
no = Result.withDefault 2.2 (String.toFloat g)
og = model.gamma
rp = model.renderPar
in
{ model | gamma = { og | g = no }, renderPar = { rp | paint = 0} }
! []
-- SUBSCRIPTIONS
subs : Model -> Sub Msg
subs model =
Sub.batch [ Window.resizes Res, getRect Rect, every (250*millisecond) Render ]
-- VIEW
view : Model -> Html Msg
view model =
section []
[ p []
[ label [ for "res" ] [text "resolution"]
, input
[ id "res"
, type_ "number"
, property "min" (EJson.int 1)
, property "step" (EJson.int 100)
, placeholder <| "default: " ++ (if model.cres == -1 then "automatic" else toString model.cres)
, onInput ChangeRes
] []
, label [ for "depth" ] [text "maximum number of iterations"]
, input
[ id "depth"
, type_ "number"
, property "min" (EJson.int 1)
, property "step" (EJson.int 10)
, placeholder <| "default: " ++ toString model.cdepth
, onInput ChangeDepth
] []
, label [ for "osampl" ] [text "antialiasing"]
, input
[ id "osampl"
, type_ "number"
, property "min" (EJson.int 1)
, property "step" (EJson.int 1)
, placeholder <| "default: " ++ toString model.cosampl
, onInput ChangeOsampl
] []
]
, grad model
, p []
[ input
[ type_ "checkbox"
, title "turn gamma on/off"
, checked (if model.gamma.act then True else False)
, Html.Events.onCheck SwitchGamma
] []
, label [ for "gamma" ] [text "gamma correction"]
, input
[ id "gamma"
, type_ "number"
, property "min" (EJson.float 0)
, property "step" (EJson.float 0.1)
, placeholder <| "default: " ++ toString model.gamma.g
, onInput ChangeGamma
] []
]
]
grad : Model -> Html Msg
grad model =
p []
[ label [] [ text "gradient" ]
, section []
(toList <| Array.indexedMap (\i gr ->
section []
[ input
[ type_ "number"
, property "min" (EJson.float 0)
, property "max" (EJson.float 1)
, property "step" (EJson.float 0.01)
, value <| toString gr.v
, onInput (ChangeGrad "v" i)
] []
, input
[ type_ "color"
, value <| colToHex gr.c
, onInput (ChangeGrad "c" i)
] []
, button [ title "remove color", Html.Events.onClick (ChangeGrad "d" i "") ] [ text "x"]
]
) model.grad)
, button [ title "add color", Html.Events.onClick (ChangeGrad "a" 0 "") ] [ text "+"]
]
--
mult : CN -> CN -> CN
mult a b =
CN (a.r*b.r - a.i*b.i) (a.r*b.i + a.i*b.r)
add : CN -> CN -> CN
add a b =
CN (a.r + b.r) (a.i + b.i)
eval : CN -> CN -> Int -> Int -> (CN, Int)
eval z c d i =
if z.r < -2.0 || z.r > 2.0 || z.i < -2.0 || z.i > 2.0 then
(z, i)
else if i >= d then
(z, i)
else
eval (add (mult z z) c) c d (i + 1)
compute : Model -> Model
compute m =
let
res = (if m.cres == -1 then m.res else m.cres)
t = 1.0 / toFloat res
cc = List.concat <| List.indexedMap (\y a ->
List.indexedMap (\x b ->
List.map (\rnd ->
let
(ru, r0) = Random.step (Random.float 0 1) (Random.initialSeed (x*y*rnd))
(rv, _) = Random.step (Random.float 0 1) r0
du = t * m.u * ru
dv = t * m.v * rv
p = CN (m.p.r + t * (toFloat x) * m.u + du) (m.p.i + t * (toFloat y) * m.v + dv)
(_, i) = eval (CN 0.0 0.0) p m.cdepth 0
in
i
) b
) a
) (List.repeat res (List.repeat res (List.range 1 m.cosampl)))
in
{ m | comp = cc }
gamma : Model -> Float -> Int
gamma m c =
let
nc = if m.gamma.act then
c ^ (1.0/m.gamma.g)
else
c
in
clamp 0 255 <| round (255 * nc )
paint : Model -> Cmd Msg
paint m =
let
grad = Array.fromList <| List.sortBy .v (Array.toList m.grad)
data =
List.map (\b ->
let
c = List.foldr (\i sp ->
let
sf = 1.0 / toFloat m.cosampl
col =
if i >= m.cdepth then
(Maybe.withDefault (Cband (C 0 0 0) 0) (Array.get ((Array.length grad)-1) grad)).c
else
let
t = logBase 2 ( (toFloat i) / (toFloat m.cdepth) +1)
a = Array.filter (\g -> g.v <= t) grad
b = Array.filter (\g -> g.v >= t) grad
va = Maybe.withDefault (Cband (C 0 0 0) 0) (Array.get ((Array.length a)-1) a)
vb = Maybe.withDefault (Cband (C 0 0 0) 0) (Array.get 0 b)
ut = (t - va.v) / (vb.v - va.v)
u = if isNaN ut then 1 else ut
ur = (1 - u) * va.c.r + vb.c.r * u
ug = (1 - u) * va.c.g + vb.c.g * u
ub = (1 - u) * va.c.b + vb.c.b * u
in
C ur ug ub
in
C (sp.r + sf * col.r) (sp.g + sf * col.g) (sp.b + sf * col.b)
) (C 0 0 0) b
in
C c.r c.g c.b
) m.comp
x = List.concat <| List.map (\c ->
[ gamma m c.r, gamma m c.g, gamma m c.b, 255]
) data
in
Ports.sendData ((if m.cres == -1 then m.res else m.cres), (if m.cres == -1 then m.res else m.cres), x)
hexToCol : String -> C
hexToCol s =
let
rh = Result.withDefault 0 (parseIntHex <| String.slice 1 3 s)
gh = Result.withDefault 0 (parseIntHex <| String.slice 3 5 s)
bh = Result.withDefault 0 (parseIntHex <| String.slice 5 7 s)
in
C ((toFloat rh)/255.0) ((toFloat gh)/255.0) ((toFloat bh)/255.0)
colToHex : C -> String
colToHex c =
"#" ++ (padToHex (round (255*c.r))) ++ (padToHex (round (255*c.g))) ++ (padToHex (round (255*c.b)))
padToHex : Int -> String
padToHex i =
let
h = toHex i
in
if String.length h > 1 then h else "0" ++ h