Browse Source

using the better webassembly setup

Inderjit Gill 6 months ago
parent
commit
008e01ad3d
20 changed files with 1122 additions and 9259 deletions
  1. 0
    5
      .gitignore
  2. 125
    145
      Cargo.lock
  3. 29
    14
      Cargo.toml
  4. 0
    23
      README.md
  5. 0
    9
      bootstrap.js
  6. 10
    0
      build.sh
  7. 0
    9029
      package-lock.json
  8. 0
    12
      package.json
  9. 810
    0
      src/colour.rs
  10. 39
    0
      src/error.rs
  11. 47
    3
      src/lib.rs
  12. 32
    0
      src/utils.rs
  13. 0
    10
      webpack.config.js
  14. 2
    0
      www/.gitignore
  15. 0
    0
      www/css/base-min.css
  16. 0
    0
      www/css/pure-min.css
  17. 0
    0
      www/css/stylesheet.css
  18. 0
    0
      www/favicon.ico
  19. 17
    1
      www/index.html
  20. 11
    8
      www/index.js

+ 0
- 5
.gitignore View File

@@ -1,6 +1 @@
1 1
 /target
2
-**/*.rs.bk
3
-
4
-/node_modules/
5
-
6
-wasm_themacs*

+ 125
- 145
Cargo.lock View File

@@ -1,71 +1,56 @@
1 1
 [[package]]
2
-name = "backtrace"
3
-version = "0.3.7"
2
+name = "cfg-if"
3
+version = "0.1.6"
4 4
 source = "registry+https://github.com/rust-lang/crates.io-index"
5
-dependencies = [
6
- "backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
7
- "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
8
- "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
9
- "rustc-demangle 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
10
- "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
11
-]
12 5
 
13 6
 [[package]]
14
-name = "backtrace-sys"
15
-version = "0.1.16"
7
+name = "console_error_panic_hook"
8
+version = "0.1.5"
16 9
 source = "registry+https://github.com/rust-lang/crates.io-index"
17 10
 dependencies = [
18
- "cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
19
- "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
11
+ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
12
+ "wasm-bindgen 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
20 13
 ]
21 14
 
22 15
 [[package]]
23
-name = "cc"
24
-version = "1.0.15"
16
+name = "futures"
17
+version = "0.1.25"
25 18
 source = "registry+https://github.com/rust-lang/crates.io-index"
26 19
 
27 20
 [[package]]
28
-name = "cfg-if"
29
-version = "0.1.3"
21
+name = "js-sys"
22
+version = "0.3.6"
30 23
 source = "registry+https://github.com/rust-lang/crates.io-index"
24
+dependencies = [
25
+ "wasm-bindgen 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
26
+]
31 27
 
32 28
 [[package]]
33
-name = "dtoa"
34
-version = "0.4.2"
29
+name = "lazy_static"
30
+version = "1.2.0"
35 31
 source = "registry+https://github.com/rust-lang/crates.io-index"
36 32
 
37 33
 [[package]]
38
-name = "failure"
39
-version = "0.1.1"
34
+name = "libc"
35
+version = "0.2.45"
40 36
 source = "registry+https://github.com/rust-lang/crates.io-index"
41
-dependencies = [
42
- "backtrace 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
43
- "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
44
-]
45 37
 
46 38
 [[package]]
47
-name = "failure_derive"
48
-version = "0.1.1"
39
+name = "log"
40
+version = "0.4.6"
49 41
 source = "registry+https://github.com/rust-lang/crates.io-index"
50 42
 dependencies = [
51
- "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
52
- "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
53
- "synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
43
+ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
54 44
 ]
55 45
 
56 46
 [[package]]
57
-name = "itoa"
58
-version = "0.4.1"
59
-source = "registry+https://github.com/rust-lang/crates.io-index"
60
-
61
-[[package]]
62
-name = "libc"
63
-version = "0.2.40"
47
+name = "memory_units"
48
+version = "0.4.0"
64 49
 source = "registry+https://github.com/rust-lang/crates.io-index"
65 50
 
66 51
 [[package]]
67 52
 name = "proc-macro2"
68
-version = "0.3.8"
53
+version = "0.4.24"
69 54
 source = "registry+https://github.com/rust-lang/crates.io-index"
70 55
 dependencies = [
71 56
  "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -73,154 +58,151 @@ dependencies = [
73 58
 
74 59
 [[package]]
75 60
 name = "quote"
76
-version = "0.3.15"
61
+version = "0.6.10"
77 62
 source = "registry+https://github.com/rust-lang/crates.io-index"
63
+dependencies = [
64
+ "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
65
+]
78 66
 
79 67
 [[package]]
80
-name = "quote"
81
-version = "0.5.2"
68
+name = "scoped-tls"
69
+version = "0.1.2"
82 70
 source = "registry+https://github.com/rust-lang/crates.io-index"
83
-dependencies = [
84
- "proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
85
-]
86 71
 
87 72
 [[package]]
88
-name = "rustc-demangle"
89
-version = "0.1.8"
73
+name = "syn"
74
+version = "0.15.22"
90 75
 source = "registry+https://github.com/rust-lang/crates.io-index"
76
+dependencies = [
77
+ "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
78
+ "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
79
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
80
+]
91 81
 
92 82
 [[package]]
93
-name = "sen-colour"
94
-version = "0.2.0"
83
+name = "themacs"
84
+version = "0.1.0"
95 85
 dependencies = [
96
- "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
86
+ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
87
+ "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
88
+ "wasm-bindgen 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
89
+ "wasm-bindgen-test 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
90
+ "wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
97 91
 ]
98 92
 
99 93
 [[package]]
100
-name = "serde"
101
-version = "1.0.47"
94
+name = "unicode-xid"
95
+version = "0.1.0"
102 96
 source = "registry+https://github.com/rust-lang/crates.io-index"
103 97
 
104 98
 [[package]]
105
-name = "serde_derive"
106
-version = "1.0.47"
99
+name = "unreachable"
100
+version = "1.0.0"
107 101
 source = "registry+https://github.com/rust-lang/crates.io-index"
108 102
 dependencies = [
109
- "proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
110
- "quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
111
- "syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)",
103
+ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
112 104
 ]
113 105
 
114 106
 [[package]]
115
-name = "serde_json"
116
-version = "1.0.17"
107
+name = "void"
108
+version = "1.0.2"
117 109
 source = "registry+https://github.com/rust-lang/crates.io-index"
118
-dependencies = [
119
- "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
120
- "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
121
- "serde 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)",
122
-]
123 110
 
124 111
 [[package]]
125
-name = "syn"
126
-version = "0.11.11"
112
+name = "wasm-bindgen"
113
+version = "0.2.29"
127 114
 source = "registry+https://github.com/rust-lang/crates.io-index"
128 115
 dependencies = [
129
- "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
130
- "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
131
- "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
116
+ "wasm-bindgen-macro 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
132 117
 ]
133 118
 
134 119
 [[package]]
135
-name = "syn"
136
-version = "0.13.7"
120
+name = "wasm-bindgen-backend"
121
+version = "0.2.29"
137 122
 source = "registry+https://github.com/rust-lang/crates.io-index"
138 123
 dependencies = [
139
- "proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
140
- "quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
141
- "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
124
+ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
125
+ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
126
+ "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
127
+ "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
128
+ "syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
129
+ "wasm-bindgen-shared 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
142 130
 ]
143 131
 
144 132
 [[package]]
145
-name = "synom"
146
-version = "0.11.3"
133
+name = "wasm-bindgen-futures"
134
+version = "0.3.6"
147 135
 source = "registry+https://github.com/rust-lang/crates.io-index"
148 136
 dependencies = [
149
- "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
137
+ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
138
+ "js-sys 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
139
+ "wasm-bindgen 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
150 140
 ]
151 141
 
152 142
 [[package]]
153
-name = "synstructure"
154
-version = "0.6.1"
143
+name = "wasm-bindgen-macro"
144
+version = "0.2.29"
155 145
 source = "registry+https://github.com/rust-lang/crates.io-index"
156 146
 dependencies = [
157
- "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
158
- "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
147
+ "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
148
+ "wasm-bindgen-macro-support 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
159 149
 ]
160 150
 
161 151
 [[package]]
162
-name = "unicode-xid"
163
-version = "0.0.4"
164
-source = "registry+https://github.com/rust-lang/crates.io-index"
165
-
166
-[[package]]
167
-name = "unicode-xid"
168
-version = "0.1.0"
169
-source = "registry+https://github.com/rust-lang/crates.io-index"
170
-
171
-[[package]]
172
-name = "wasm-bindgen"
173
-version = "0.2.8"
152
+name = "wasm-bindgen-macro-support"
153
+version = "0.2.29"
174 154
 source = "registry+https://github.com/rust-lang/crates.io-index"
175 155
 dependencies = [
176
- "wasm-bindgen-macro 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
156
+ "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
157
+ "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
158
+ "syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
159
+ "wasm-bindgen-backend 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
160
+ "wasm-bindgen-shared 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
177 161
 ]
178 162
 
179 163
 [[package]]
180
-name = "wasm-bindgen-backend"
181
-version = "0.2.8"
164
+name = "wasm-bindgen-shared"
165
+version = "0.2.29"
182 166
 source = "registry+https://github.com/rust-lang/crates.io-index"
183
-dependencies = [
184
- "proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
185
- "quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
186
- "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
187
- "syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)",
188
- "wasm-bindgen-shared 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
189
-]
190 167
 
191 168
 [[package]]
192
-name = "wasm-bindgen-macro"
193
-version = "0.2.8"
169
+name = "wasm-bindgen-test"
170
+version = "0.2.29"
194 171
 source = "registry+https://github.com/rust-lang/crates.io-index"
195 172
 dependencies = [
196
- "proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
197
- "quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
198
- "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
199
- "syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)",
200
- "wasm-bindgen-backend 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
201
- "wasm-bindgen-shared 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
173
+ "console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
174
+ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
175
+ "js-sys 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
176
+ "scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
177
+ "wasm-bindgen 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
178
+ "wasm-bindgen-futures 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
179
+ "wasm-bindgen-test-macro 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
202 180
 ]
203 181
 
204 182
 [[package]]
205
-name = "wasm-bindgen-shared"
206
-version = "0.2.8"
183
+name = "wasm-bindgen-test-macro"
184
+version = "0.2.29"
207 185
 source = "registry+https://github.com/rust-lang/crates.io-index"
208 186
 dependencies = [
209
- "serde 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)",
210
- "serde_derive 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)",
187
+ "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
188
+ "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
211 189
 ]
212 190
 
213 191
 [[package]]
214
-name = "wasm_themacs"
215
-version = "0.1.0"
192
+name = "wee_alloc"
193
+version = "0.4.2"
194
+source = "registry+https://github.com/rust-lang/crates.io-index"
216 195
 dependencies = [
217
- "sen-colour 0.2.0",
218
- "wasm-bindgen 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
196
+ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
197
+ "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
198
+ "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
199
+ "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
200
+ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
219 201
 ]
220 202
 
221 203
 [[package]]
222 204
 name = "winapi"
223
-version = "0.3.4"
205
+version = "0.3.6"
224 206
 source = "registry+https://github.com/rust-lang/crates.io-index"
225 207
 dependencies = [
226 208
  "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -238,32 +220,30 @@ version = "0.4.0"
238 220
 source = "registry+https://github.com/rust-lang/crates.io-index"
239 221
 
240 222
 [metadata]
241
-"checksum backtrace 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea58cd16fd6c9d120b5bcb01d63883ae4cc7ba2aed35c1841b862a3c7ef6639"
242
-"checksum backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "44585761d6161b0f57afc49482ab6bd067e4edef48c12a152c237eb0203f7661"
243
-"checksum cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "0ebb87d1116151416c0cf66a0e3fb6430cccd120fd6300794b4dfaa050ac40ba"
244
-"checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18"
245
-"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
246
-"checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82"
247
-"checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b"
248
-"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682"
249
-"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
250
-"checksum proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1b06e2f335f48d24442b35a19df506a835fb3547bc3c06ef27340da9acf5cae7"
251
-"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
252
-"checksum quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8"
253
-"checksum rustc-demangle 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "76d7ba1feafada44f2d38eed812bd2489a03c0f5abb975799251518b68848649"
254
-"checksum serde 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)" = "c9bc5ad5d354bc43e8ba78885f81650e6d4467a55716271b49d7ceeba739e21e"
255
-"checksum serde_derive 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)" = "a0b9c70a763b79f976e7147322d6c523da0705b3d20e57fd764172d0741c50f2"
256
-"checksum serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "f3ad6d546e765177cf3dded3c2e424a8040f870083a0e64064746b958ece9cb1"
257
-"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
258
-"checksum syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)" = "61b8f1b737f929c6516ba46a3133fd6d5215ad8a62f66760f851f7048aebedfb"
259
-"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
260
-"checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd"
261
-"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
223
+"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
224
+"checksum console_error_panic_hook 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6c5dd2c094474ec60a6acaf31780af270275e3153bafff2db5995b715295762e"
225
+"checksum futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)" = "49e7653e374fe0d0c12de4250f0bdb60680b8c80eed558c5c7538eec9c89e21b"
226
+"checksum js-sys 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58cfec35fd4a94f3cf357d5cb7da71c71cd52720c2f2a7320090a8db5f06f655"
227
+"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
228
+"checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
229
+"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
230
+"checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
231
+"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
232
+"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
233
+"checksum scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "332ffa32bf586782a3efaeb58f127980944bbc8c4d6913a86107ac2a5ab24b28"
234
+"checksum syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)" = "ae8b29eb5210bc5cf63ed6149cbf9adfc82ac0be023d8735c176ee74a2db4da7"
262 235
 "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
263
-"checksum wasm-bindgen 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b0271974d38e09a1e102d95a34a6b27a104ee77541c0314be7e91e026bff9e89"
264
-"checksum wasm-bindgen-backend 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8e9a4a55017ec67b697893292efd05eb5f7e0ae26ddb4d9879eee6d60e66eb2b"
265
-"checksum wasm-bindgen-macro 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "87e37cc5c09b0b58897ea69be6f81d1effb7ab22b34eb48626267471d8c4ec9e"
266
-"checksum wasm-bindgen-shared 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "a629526dde66833247cfeb5fa2d5ea88f116cb05146f5039f6085784fdea679b"
267
-"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
236
+"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
237
+"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
238
+"checksum wasm-bindgen 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "91f95b8f30407b9ca0c2de157281d3828bbed1fc1f55bea6eb54f40c52ec75ec"
239
+"checksum wasm-bindgen-backend 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "ab7c242ebcb45bae45340986c48d1853eb2c1c52ff551f7724951b62a2c51429"
240
+"checksum wasm-bindgen-futures 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d1784e7401a90119b2a4e8ec9c8d37c3594c3e3bb9ba24533ee1969eebaf0485"
241
+"checksum wasm-bindgen-macro 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "6e353f83716dec9a3597b5719ef88cb6c9e461ec16528f38aa023d3224b4e569"
242
+"checksum wasm-bindgen-macro-support 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "3cc90b65fe69c3dd5a09684517dc79f42b847baa2d479c234d125e0a629d9b0a"
243
+"checksum wasm-bindgen-shared 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "a71a37df4f5845025f96f279d20bbe5b19cbcb77f5410a3a90c6c544d889a162"
244
+"checksum wasm-bindgen-test 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "a98dfd06998415cef588205b1462a03f01447c10870231145b603f801f770bac"
245
+"checksum wasm-bindgen-test-macro 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "538a21fed1ca83ef51d333ce668d925b9ab017818f6c92a54900a3c03dab7ca8"
246
+"checksum wee_alloc 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27875be1daf838fa18f3e94fd19fd12638e34615b42f56da2610c8f46be80cc6"
247
+"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
268 248
 "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
269 249
 "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 29
- 14
Cargo.toml View File

@@ -1,29 +1,44 @@
1 1
 [package]
2
-name = "wasm_themacs"
2
+name = "themacs"
3 3
 version = "0.1.0"
4
-authors = ["Inderjit Gill <inderjit.gill@gmail.com>"]
4
+authors = ["Inderjit Gill <email@indy.io>"]
5
+edition = "2018"
5 6
 
6 7
 [lib]
7
-crate-type = ["cdylib"]
8
+crate-type = ["cdylib", "rlib"]
9
+
10
+[features]
11
+default = ["console_error_panic_hook"]
8 12
 
9 13
 [dependencies]
10
-wasm-bindgen = "0.2.8"
11
-sen-colour = { path = "../sen-colour" }
14
+cfg-if = "0.1.2"
15
+wasm-bindgen = "0.2"
16
+
17
+# The `console_error_panic_hook` crate provides better debugging of panics by
18
+# logging them with `console.error`. This is great for development, but requires
19
+# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
20
+# code size when deploying.
21
+console_error_panic_hook = { version = "0.1.1", optional = true }
22
+
23
+# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
24
+# compared to the default allocator's ~10K. It is slower than the default
25
+# allocator, however.
26
+#
27
+# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
28
+wee_alloc = { version = "0.4.2", optional = true }
12 29
 
30
+[dev-dependencies]
31
+wasm-bindgen-test = "0.2"
13 32
 
14 33
 [profile.release]
15 34
 # Include function names in the `.wasm` for better debugging and
16 35
 # profiling. Comment this out if you're trying to create the smallest `.wasm`
17 36
 # binaries you can.
18
-debug = true
37
+debug = false
19 38
 
20 39
 # # Uncomment these lines to create smaller `.wasm` binaries, at the cost of
21 40
 # # longer compile times.
22
-# codegen-units = 1
23
-# incremental = false
24
-# lto = true
25
-# opt-level = "z"
26
-
27
-
28
-# # option to optimise for size
29
-# opt-level = "s"
41
+codegen-units = 1
42
+incremental = false
43
+lto = true
44
+opt-level = "z"

+ 0
- 23
README.md View File

@@ -1,23 +0,0 @@
1
-Themacs
2
-======
3
-
4
-## Overview
5
-
6
-
7
-
8
-## ensure that the correct toolchain is installed
9
-```sh
10
-$ rustup update
11
-$ rustup install nightly
12
-$ rustup target add wasm32-unknown-unknown --toolchain nightly
13
-$ cargo install wasm-bindgen-cli
14
-```
15
-
16
-## npm bollocks
17
-```sh
18
-$ npm install
19
-$ npm run build-debug
20
-$ npm run serve
21
-```
22
-
23
-can now visit http://localhost:8080

+ 0
- 9
bootstrap.js View File

@@ -1,9 +0,0 @@
1
-const index = import("./index");
2
-index.then(() => {
3
-  console.log("Loaded...");
4
-});
5
-
6
-
7
-document.addEventListener('DOMContentLoaded', () => {
8
-  console.log('DOMContentLoaded');
9
-}, false);

+ 10
- 0
build.sh View File

@@ -0,0 +1,10 @@
1
+#!/bin/sh
2
+
3
+if [ "$1" = "release" ]
4
+then
5
+    cargo +nightly build --release --target wasm32-unknown-unknown
6
+    wasm-bindgen target/wasm32-unknown-unknown/release/themacs.wasm --out-dir www --no-typescript --no-modules
7
+else
8
+    cargo +nightly build --target wasm32-unknown-unknown
9
+    wasm-bindgen target/wasm32-unknown-unknown/debug/themacs.wasm --out-dir www --no-typescript --no-modules
10
+fi

+ 0
- 9029
package-lock.json
File diff suppressed because it is too large
View File


+ 0
- 12
package.json View File

@@ -1,12 +0,0 @@
1
-{
2
-  "scripts": {
3
-    "serve": "webpack-dev-server",
4
-    "build-debug": "cargo +nightly build --target wasm32-unknown-unknown && wasm-bindgen target/wasm32-unknown-unknown/debug/wasm_themacs.wasm --out-dir .",
5
-    "build-release": "cargo +nightly build --release --target wasm32-unknown-unknown && wasm-bindgen target/wasm32-unknown-unknown/release/wasm_themacs.wasm --out-dir ."
6
-  },
7
-  "devDependencies": {
8
-    "webpack": "^4.0.1",
9
-    "webpack-cli": "^2.0.10",
10
-    "webpack-dev-server": "^3.1.0"
11
-  }
12
-}

+ 810
- 0
src/colour.rs View File

@@ -0,0 +1,810 @@
1
+// Copyright (C) 2018 Inderjit Gill
2
+
3
+// This program is free software: you can redistribute it and/or modify
4
+// it under the terms of the GNU General Public License as published by
5
+// the Free Software Foundation, either version 3 of the License, or
6
+// (at your option) any later version.
7
+
8
+// This program is distributed in the hope that it will be useful,
9
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+// GNU General Public License for more details.
12
+
13
+// You should have received a copy of the GNU General Public License
14
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
+
16
+// |--------+-----------+-------------+-------------|
17
+// | format | element 0 | element 1   | element 2   |
18
+// |--------+-----------+-------------+-------------|
19
+// | RGB    | R 0..1    | G 0..1      | B 0..1      |
20
+// | HSL    | H 0..360  | S 0..1      | L 0..1      |
21
+// | HSLuv  | H 0..360  | S 0..100    | L 0..100    |
22
+// | LAB    | L 0..100  | A -128..128 | B -128..128 |
23
+// |--------+-----------+-------------+-------------|
24
+
25
+
26
+use crate::error::{SenError, SenResult};
27
+use std;
28
+
29
+const REF_U: f64 = 0.197_830_006_642_836_807_64;
30
+const REF_V: f64 = 0.468_319_994_938_791_003_70;
31
+
32
+//  http://www.brucelindbloom.com/index.html?Equations.html
33
+//  http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
34
+
35
+// we're using an sRGB working space with a D65 reference white
36
+
37
+// https://uk.mathworks.com/help/images/ref/whitepoint.html
38
+// the D65 whitepoint
39
+const WHITEPOINT_0: f64 = 0.9504;
40
+const WHITEPOINT_1: f64 = 1.0;
41
+const WHITEPOINT_2: f64 = 1.0888;
42
+
43
+const CIE_EPSILON: f64 = 0.008_856;
44
+const CIE_KAPPA: f64 = 903.3;
45
+
46
+// intent from the CIE
47
+//
48
+// #define CIE_EPSILON (216.0f / 24389.0f)
49
+// #define CIE_KAPPA (24389.0f / 27.0f)
50
+
51
+// RGB to XYZ (M)
52
+// 0.4124564  0.3575761  0.1804375
53
+// 0.2126729  0.7151522  0.0721750
54
+// 0.0193339  0.1191920  0.9503041
55
+
56
+// XYZ to RBG (M)^-1
57
+//  3.2404542 -1.5371385 -0.4985314
58
+// -0.9692660  1.8760108  0.0415560
59
+//  0.0556434 -0.2040259  1.0572252
60
+
61
+#[derive(Debug, PartialEq, Clone, Copy)]
62
+pub enum Format {
63
+    RGB,
64
+    HSLuv,
65
+    HSL,
66
+    LAB,
67
+    HSV,
68
+}
69
+
70
+#[derive(Debug, Clone, Copy)]
71
+pub enum Colour {
72
+    RGB(f64, f64, f64, f64),
73
+    HSLuv(f64, f64, f64, f64),
74
+    HSL(f64, f64, f64, f64),
75
+    LAB(f64, f64, f64, f64),
76
+    HSV(f64, f64, f64, f64),
77
+    XYZ(f64, f64, f64, f64),
78
+    LUV(f64, f64, f64, f64),
79
+    LCH(f64, f64, f64, f64),
80
+}
81
+
82
+impl Colour {
83
+    pub fn is_format(&self, format: Format) -> bool {
84
+        match format {
85
+            Format::RGB => match *self {
86
+                Colour::RGB(_, _, _, _) => true,
87
+                _ => false,
88
+            },
89
+            Format::HSLuv => match *self {
90
+                Colour::HSLuv(_, _, _, _) => true,
91
+                _ => false,
92
+            },
93
+            Format::HSL => match *self {
94
+                Colour::HSL(_, _, _, _) => true,
95
+                _ => false,
96
+            },
97
+            Format::LAB => match *self {
98
+                Colour::LAB(_, _, _, _) => true,
99
+                _ => false,
100
+            },
101
+            Format::HSV => match *self {
102
+                Colour::HSV(_, _, _, _) => true,
103
+                _ => false,
104
+            },
105
+        }
106
+    }
107
+
108
+    pub fn clone_as(&self, format: Format) -> SenResult<Colour> {
109
+        match *self {
110
+            Colour::HSL(h, s, l, alpha) => match format {
111
+                Format::HSL => Ok(Colour::HSL(h, s, l, alpha)),
112
+                Format::HSLuv => hsluv_from_xyz(xyz_from_rgb(rgb_from_hsl(*self)?)?),
113
+                Format::HSV => hsv_from_rgb(rgb_from_hsl(*self)?),
114
+                Format::LAB => lab_from_xyz(xyz_from_rgb(rgb_from_hsl(*self)?)?),
115
+                Format::RGB => rgb_from_hsl(*self),
116
+            },
117
+            Colour::HSLuv(h, s, l, alpha) => match format {
118
+                Format::HSL => hsl_from_rgb(rgb_from_xyz(xyz_from_hsluv(*self)?)?),
119
+                Format::HSLuv => Ok(Colour::HSLuv(h, s, l, alpha)),
120
+                Format::HSV => hsv_from_rgb(rgb_from_xyz(xyz_from_hsluv(*self)?)?),
121
+                Format::LAB => lab_from_xyz(xyz_from_hsluv(*self)?),
122
+                Format::RGB => rgb_from_xyz(xyz_from_hsluv(*self)?),
123
+            },
124
+            Colour::HSV(h, s, v, alpha) => match format {
125
+                Format::HSL => hsl_from_rgb(rgb_from_hsv(*self)?),
126
+                Format::HSLuv => hsluv_from_xyz(xyz_from_rgb(rgb_from_hsv(*self)?)?),
127
+                Format::HSV => Ok(Colour::HSV(h, s, v, alpha)),
128
+                Format::LAB => lab_from_xyz(xyz_from_rgb(rgb_from_hsv(*self)?)?),
129
+                Format::RGB => rgb_from_hsv(*self),
130
+            },
131
+            Colour::LAB(l, a, b, alpha) => match format {
132
+                Format::HSL => hsl_from_rgb(rgb_from_xyz(xyz_from_lab(*self)?)?),
133
+                Format::HSLuv => hsluv_from_xyz(xyz_from_lab(*self)?),
134
+                Format::HSV => hsv_from_rgb(rgb_from_xyz(xyz_from_lab(*self)?)?),
135
+                Format::LAB => Ok(Colour::LAB(l, a, b, alpha)),
136
+                Format::RGB => rgb_from_xyz(xyz_from_lab(*self)?),
137
+            },
138
+            Colour::RGB(r, g, b, alpha) => match format {
139
+                Format::HSL => hsl_from_rgb(*self),
140
+                Format::HSLuv => hsluv_from_xyz(xyz_from_rgb(*self)?),
141
+                Format::HSV => hsv_from_rgb(*self),
142
+                Format::LAB => lab_from_xyz(xyz_from_rgb(*self)?),
143
+                Format::RGB => Ok(Colour::RGB(r, g, b, alpha)),
144
+            },
145
+            _ => Err(SenError::IncorrectColourFormat),
146
+        }
147
+    }
148
+}
149
+
150
+fn colour_to_axis(component: f64) -> f64 {
151
+    if component > 0.04045 {
152
+        ((component + 0.055) / 1.055).powf(2.4)
153
+    } else {
154
+        (component / 12.92)
155
+    }
156
+}
157
+
158
+fn axis_to_colour(a: f64) -> f64 {
159
+    if a > 0.003_130_8 {
160
+        (1.055 * a.powf(1.0 / 2.4)) - 0.055
161
+    } else {
162
+        a * 12.92
163
+    }
164
+}
165
+
166
+fn xyz_from_rgb(rgb: Colour) -> SenResult<Colour> {
167
+    match rgb {
168
+        Colour::RGB(r, g, b, alpha) => {
169
+            let rr = colour_to_axis(r);
170
+            let gg = colour_to_axis(g);
171
+            let bb = colour_to_axis(b);
172
+
173
+            // multiply by matrix
174
+            // see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
175
+            // sRGB colour space with D65 reference white
176
+            //
177
+
178
+            let x = (rr * 0.412_390_799_265_959_5)
179
+                + (gg * 0.357_584_339_383_877_96)
180
+                + (bb * 0.180_480_788_401_834_3);
181
+            let y = (rr * 0.212_639_005_871_510_36)
182
+                + (gg * 0.715_168_678_767_755_927_46)
183
+                + (bb * 0.072_192_315_360_733_715_00);
184
+            let z = (rr * 0.019_330_818_715_591_850_69)
185
+                + (gg * 0.119_194_779_794_625_987_91)
186
+                + (bb * 0.950_532_152_249_660_580_86);
187
+
188
+            Ok(Colour::XYZ(x, y, z, alpha))
189
+        }
190
+        _ => Err(SenError::IncorrectColourFormat),
191
+    }
192
+}
193
+
194
+fn rgb_from_xyz(xyz: Colour) -> SenResult<Colour> {
195
+    match xyz {
196
+        Colour::XYZ(x, y, z, alpha) => {
197
+            let r = (x * 3.240_969_941_904_521_343_77)
198
+                + (y * -1.537_383_177_570_093_457_94)
199
+                + (z * -0.498_610_760_293_003_283_66);
200
+            let g = (x * -0.969_243_636_280_879_826_13)
201
+                + (y * 1.875_967_501_507_720_667_72)
202
+                + (z * 0.041_555_057_407_175_612_47);
203
+            let b = (x * 0.055_630_079_696_993_608_46)
204
+                + (y * -0.203_976_958_888_976_564_35)
205
+                + (z * 1.056_971_514_242_878_560_72);
206
+
207
+            let rr = axis_to_colour(r);
208
+            let gg = axis_to_colour(g);
209
+            let bb = axis_to_colour(b);
210
+
211
+            Ok(Colour::RGB(rr, gg, bb, alpha))
212
+        }
213
+        _ => Err(SenError::IncorrectColourFormat),
214
+    }
215
+}
216
+
217
+fn axis_to_lab_component(a: f64) -> f64 {
218
+    if a > CIE_EPSILON {
219
+        a.cbrt()
220
+    } else {
221
+        ((CIE_KAPPA * a) + 16.0) / 116.0
222
+    }
223
+}
224
+
225
+fn lab_from_xyz(xyz: Colour) -> SenResult<Colour> {
226
+    match xyz {
227
+        Colour::XYZ(x, y, z, alpha) => {
228
+            let xr = x / WHITEPOINT_0;
229
+            let yr = y / WHITEPOINT_1;
230
+            let zr = z / WHITEPOINT_2;
231
+
232
+            let fx = axis_to_lab_component(xr);
233
+            let fy = axis_to_lab_component(yr);
234
+            let fz = axis_to_lab_component(zr);
235
+
236
+            let l = (116.0 * fy) - 16.0;
237
+            let a = 500.0 * (fx - fy);
238
+            let b = 200.0 * (fy - fz);
239
+
240
+            Ok(Colour::LAB(l, a, b, alpha))
241
+        }
242
+        _ => Err(SenError::IncorrectColourFormat),
243
+    }
244
+}
245
+
246
+fn max_channel(r: f64, g: f64, b: f64) -> i32 {
247
+    let hi = if r > g { 0 } else { 1 };
248
+    let hival = if r > g { r } else { g };
249
+
250
+    if b > hival {
251
+        2
252
+    } else {
253
+        hi
254
+    }
255
+}
256
+
257
+// TODO: implement a better fmod, this one is not exact
258
+fn fmod(a: f64, b: f64) -> f64 {
259
+    a - b * (a / b).floor()
260
+}
261
+
262
+// http://www.rapidtables.com/convert/color/rgb-to-hsl.htm
263
+fn hue(colour: Colour, max_chan: i32, chroma: f64) -> SenResult<f64> {
264
+    if chroma == 0.0 {
265
+        // return Err(SenError::InvalidColourHue)
266
+        return Ok(0.0);
267
+    }
268
+
269
+    let mut angle: f64;
270
+
271
+    match colour {
272
+        Colour::RGB(r, g, b, _) => {
273
+            angle = match max_chan {
274
+                0 => fmod((g - b) / chroma, 6.0),
275
+                1 => ((b - r) / chroma) + 2.0,
276
+                2 => ((r - g) / chroma) + 4.0,
277
+                _ => return Err(SenError::InvalidColourChannel),
278
+            }
279
+        }
280
+        _ => return Err(SenError::IncorrectColourFormat),
281
+    }
282
+
283
+    angle *= 60.0;
284
+
285
+    while angle < 0.0 {
286
+        angle += 360.0;
287
+    }
288
+
289
+    Ok(angle)
290
+}
291
+
292
+// http://www.rapidtables.com/convert/color/rgb-to-hsl.htm
293
+fn hsl_from_rgb(colour: Colour) -> SenResult<Colour> {
294
+    match colour {
295
+        Colour::RGB(r, g, b, alpha) => {
296
+            let min_val = r.min(g).min(b);
297
+            let max_val = r.max(g).max(b);
298
+            let max_ch = max_channel(r, g, b);
299
+
300
+            let delta = max_val - min_val;
301
+
302
+            let h = hue(colour, max_ch, delta)?;
303
+            let lightness = 0.5 * (min_val + max_val);
304
+            let saturation: f64 = if delta == 0.0 {
305
+                0.0
306
+            } else {
307
+                delta / (1.0 - ((2.0 * lightness) - 1.0).abs())
308
+            };
309
+
310
+            Ok(Colour::HSL(h, saturation, lightness, alpha))
311
+        }
312
+        _ => Err(SenError::IncorrectColourFormat),
313
+    }
314
+}
315
+
316
+fn hsv_from_rgb(colour: Colour) -> SenResult<Colour> {
317
+    match colour {
318
+        Colour::RGB(r, g, b, alpha) => {
319
+            let min_val = r.min(g).min(b);
320
+            let max_val = r.max(g).max(b);
321
+            let max_ch = max_channel(r, g, b);
322
+
323
+            let chroma = max_val - min_val;
324
+            let h = hue(colour, max_ch, chroma)?;
325
+
326
+            // valid_hue: bool = chroma != 0.0;
327
+
328
+            let saturation: f64 = if chroma == 0.0 { 0.0 } else { chroma / max_val };
329
+
330
+            // TODO: set valid_hue
331
+            // return col.set('valid_hue', valid_hue);
332
+
333
+            Ok(Colour::HSV(h, saturation, max_val, alpha))
334
+        }
335
+        _ => Err(SenError::IncorrectColourFormat),
336
+    }
337
+}
338
+
339
+fn rgb_from_chm(chroma: f64, h: f64, m: f64, alpha: f64) -> Colour {
340
+    // todo: validhue test
341
+    //
342
+    // if (c.get('validHue') === undefined) {
343
+    // return construct(Format.RGB, [m, m, m, element(c, ALPHA)]);
344
+    //}
345
+
346
+    let hprime = h / 60.0;
347
+    let x = chroma * (1.0 - (fmod(hprime, 2.0) - 1.0).abs());
348
+    let mut r = 0.0;
349
+    let mut g = 0.0;
350
+    let mut b = 0.0;
351
+
352
+    if hprime < 1.0 {
353
+        r = chroma;
354
+        g = x;
355
+        b = 0.0;
356
+    } else if hprime < 2.0 {
357
+        r = x;
358
+        g = chroma;
359
+        b = 0.0;
360
+    } else if hprime < 3.0 {
361
+        r = 0.0;
362
+        g = chroma;
363
+        b = x;
364
+    } else if hprime < 4.0 {
365
+        r = 0.0;
366
+        g = x;
367
+        b = chroma;
368
+    } else if hprime < 5.0 {
369
+        r = x;
370
+        g = 0.0;
371
+        b = chroma;
372
+    } else if hprime < 6.0 {
373
+        r = chroma;
374
+        g = 0.0;
375
+        b = x;
376
+    }
377
+
378
+    Colour::RGB(r + m, g + m, b + m, alpha)
379
+}
380
+
381
+fn rgb_from_hsl(hsl: Colour) -> SenResult<Colour> {
382
+    match hsl {
383
+        Colour::HSL(h, s, l, alpha) => {
384
+            let chroma = (1.0 - ((2.0 * l) - 1.0).abs()) * s;
385
+            let m = l - (0.5 * chroma);
386
+
387
+            // todo: set validhue
388
+            // f64 col = c.set('validHue', true);
389
+
390
+            Ok(rgb_from_chm(chroma, h, m, alpha))
391
+        }
392
+        _ => Err(SenError::IncorrectColourFormat),
393
+    }
394
+}
395
+
396
+fn lab_component_to_axis(l: f64) -> f64 {
397
+    if l.powf(3.0) > CIE_EPSILON {
398
+        l.powf(3.0)
399
+    } else {
400
+        ((116.0 * l) - 16.0) / CIE_KAPPA
401
+    }
402
+}
403
+
404
+fn xyz_from_lab(lab: Colour) -> SenResult<Colour> {
405
+    match lab {
406
+        Colour::LAB(l, a, b, alpha) => {
407
+            let fy = (l + 16.0) / 116.0;
408
+            let fz = fy - (b / 200.0);
409
+            let fx = (a / 500.0) + fy;
410
+
411
+            let xr = lab_component_to_axis(fx);
412
+            let mut yr;
413
+            if l > (CIE_EPSILON * CIE_KAPPA) {
414
+                yr = (l + 16.0) / 116.0;
415
+                yr = yr * yr * yr;
416
+            } else {
417
+                yr = l / CIE_KAPPA;
418
+            }
419
+            let zr = lab_component_to_axis(fz);
420
+
421
+            Ok(Colour::XYZ(
422
+                WHITEPOINT_0 * xr,
423
+                WHITEPOINT_1 * yr,
424
+                WHITEPOINT_2 * zr,
425
+                alpha,
426
+            ))
427
+        }
428
+        _ => Err(SenError::IncorrectColourFormat),
429
+    }
430
+}
431
+
432
+fn rgb_from_hsv(hsv: Colour) -> SenResult<Colour> {
433
+    match hsv {
434
+        Colour::HSV(h, s, v, alpha) => {
435
+            let chroma = v * s;
436
+            let m = v - chroma;
437
+
438
+            Ok(rgb_from_chm(chroma, h, m, alpha))
439
+        }
440
+        _ => Err(SenError::IncorrectColourFormat),
441
+    }
442
+}
443
+
444
+// the luv and hsluv code is based on https://github.com/hsluv/hsluv-c
445
+// which uses the MIT License:
446
+
447
+// # The MIT License (MIT)
448
+
449
+// Copyright © 2015 Alexei Boronine (original idea, JavaScript implementation)
450
+// Copyright © 2015 Roger Tallada (Obj-C implementation)
451
+// Copyright © 2017 Martin Mitáš (C implementation, based on Obj-C
452
+// implementation)
453
+
454
+// Permission is hereby granted, free of charge, to any person obtaining a
455
+// copy of this software and associated documentation files (the “Software”),
456
+// to deal in the Software without restriction, including without limitation
457
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
458
+// and/or sell copies of the Software, and to permit persons to whom the
459
+// Software is furnished to do so, subject to the following conditions:
460
+
461
+// The above copyright notice and this permission notice shall be included
462
+// in all copies or substantial portions of the Software.
463
+
464
+// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
465
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
466
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
467
+// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
468
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
469
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
470
+// IN THE SOFTWARE.
471
+
472
+#[derive(Debug, Clone, Copy)]
473
+struct Bounds {
474
+    a: f64,
475
+    b: f64,
476
+}
477
+
478
+fn get_bounds(l: f64, bounds: &mut [Bounds]) {
479
+    let tl = l + 16.0;
480
+    let sub1 = (tl * tl * tl) / 1_560_896.0;
481
+    let sub2 = if sub1 > CIE_EPSILON {
482
+        sub1
483
+    } else {
484
+        l / CIE_KAPPA
485
+    };
486
+
487
+    let mut m = [[0f64; 3]; 3];
488
+    m[0][0] = 3.240_969_941_904_521_343_77;
489
+    m[0][1] = -1.537_383_177_570_093_457_94;
490
+    m[0][2] = -0.498_610_760_293_003_283_66;
491
+    m[1][0] = -0.969_243_636_280_879_826_13;
492
+    m[1][1] = 1.875_967_501_507_720_667_72;
493
+    m[1][2] = 0.041_555_057_407_175_612_47;
494
+    m[2][0] = 0.055_630_079_696_993_608_46;
495
+    m[2][1] = -0.203_976_958_888_976_564_35;
496
+    m[2][2] = 1.056_971_514_242_878_560_72;
497
+
498
+    for channel in 0..3 {
499
+        let m1 = m[channel][0];
500
+        let m2 = m[channel][1];
501
+        let m3 = m[channel][2];
502
+
503
+        for t in 0..2 {
504
+            let top1 = (284_517.0 * m1 - 94_839.0 * m3) * sub2;
505
+            let top2 = (838_422.0 * m3 + 769_860.0 * m2 + 731_718.0 * m1) * l * sub2
506
+                - 769_860.0 * (t as f64) * l;
507
+            let bottom = (632_260.0 * m3 - 126_452.0 * m2) * sub2 + 126_452.0 * (t as f64);
508
+
509
+            bounds[channel * 2 + t].a = top1 / bottom;
510
+            bounds[channel * 2 + t].b = top2 / bottom;
511
+        }
512
+    }
513
+}
514
+
515
+fn ray_length_until_intersect(theta: f64, line: &Bounds) -> f64 {
516
+    line.b / (theta.sin() - line.a * theta.cos())
517
+}
518
+
519
+fn max_chroma_for_lh(l: f64, h: f64) -> f64 {
520
+    let mut min_len = std::f64::MAX;
521
+    let hrad = h * 0.017_453_292_519_943_295_77; /* (2 * pi / 260) */
522
+    let mut bounds = [Bounds { a: 0.0, b: 0.0 }; 6];
523
+
524
+    get_bounds(l, &mut bounds);
525
+
526
+    for b in &bounds {
527
+        let l2 = ray_length_until_intersect(hrad, &b);
528
+
529
+        if l2 >= 0.0 && l2 < min_len {
530
+            min_len = l2;
531
+        }
532
+    }
533
+
534
+    min_len
535
+}
536
+
537
+/* http://en.wikipedia.org/wiki/CIELUV
538
+ * In these formulas, Yn refers to the reference white point. We are using
539
+ * illuminant D65, so Yn (see refY in Maxima file) equals 1. The formula is
540
+ * simplified accordingly.
541
+ */
542
+fn y2l(y: f64) -> f64 {
543
+    if y <= CIE_EPSILON {
544
+        y * CIE_KAPPA
545
+    } else {
546
+        116.0 * y.cbrt() - 16.0
547
+    }
548
+}
549
+
550
+fn l2y(l: f64) -> f64 {
551
+    if l <= 8.0 {
552
+        l / CIE_KAPPA
553
+    } else {
554
+        let x = (l + 16.0) / 116.0;
555
+        x * x * x
556
+    }
557
+}
558
+
559
+fn luv_from_xyz(xyz: Colour) -> SenResult<Colour> {
560
+    match xyz {
561
+        Colour::XYZ(x, y, z, alpha) => {
562
+            let var_u = (4.0 * x) / (x + (15.0 * y) + (3.0 * z));
563
+            let var_v = (9.0 * y) / (x + (15.0 * y) + (3.0 * z));
564
+            let l = y2l(y);
565
+            let u = 13.0 * l * (var_u - REF_U);
566
+            let v = 13.0 * l * (var_v - REF_V);
567
+
568
+            if l < 0.000_000_01 {
569
+                Ok(Colour::LUV(l, 0.0, 0.0, alpha))
570
+            } else {
571
+                Ok(Colour::LUV(l, u, v, alpha))
572
+            }
573
+        }
574
+        _ => Err(SenError::IncorrectColourFormat),
575
+    }
576
+}
577
+
578
+fn xyz_from_luv(luv: Colour) -> SenResult<Colour> {
579
+    match luv {
580
+        Colour::LUV(l, u, v, alpha) => {
581
+            if l <= 0.000_000_01 {
582
+                return Ok(Colour::XYZ(0.0, 0.0, 0.0, alpha));
583
+            }
584
+
585
+            let var_u = u / (13.0 * l) + REF_U;
586
+            let var_v = v / (13.0 * l) + REF_V;
587
+            let y = l2y(l);
588
+            let x = -(9.0 * y * var_u) / ((var_u - 4.0) * var_v - var_u * var_v);
589
+            let z = (9.0 * y - (15.0 * var_v * y) - (var_v * x)) / (3.0 * var_v);
590
+
591
+            Ok(Colour::XYZ(x, y, z, alpha))
592
+        }
593
+        _ => Err(SenError::IncorrectColourFormat),
594
+    }
595
+}
596
+
597
+fn lch_from_luv(luv: Colour) -> SenResult<Colour> {
598
+    match luv {
599
+        Colour::LUV(l, u, v, alpha) => {
600
+            let mut h: f64;
601
+            let c = (u * u + v * v).sqrt();
602
+
603
+            if c < 0.000_000_01 {
604
+                h = 0.0;
605
+            } else {
606
+                h = v.atan2(u) * 57.295_779_513_082_320_876_80; /* (180 / pi) */
607
+                if h < 0.0 {
608
+                    h += 360.0;
609
+                }
610
+            }
611
+
612
+            Ok(Colour::LCH(l, c, h, alpha))
613
+        }
614
+        _ => Err(SenError::IncorrectColourFormat),
615
+    }
616
+}
617
+
618
+fn luv_from_lch(lch: Colour) -> SenResult<Colour> {
619
+    match lch {
620
+        Colour::LCH(l, c, h, alpha) => {
621
+            let hrad = h * 0.017_453_292_519_943_295_77; /* (pi / 180.0) */
622
+            let u = hrad.cos() * c;
623
+            let v = hrad.sin() * c;
624
+
625
+            Ok(Colour::LUV(l, u, v, alpha))
626
+        }
627
+        _ => Err(SenError::IncorrectColourFormat),
628
+    }
629
+}
630
+
631
+fn lch_from_hsluv(hsluv: Colour) -> SenResult<Colour> {
632
+    match hsluv {
633
+        Colour::HSLuv(h, s, l, alpha) => {
634
+            let c = if l > 99.999_999_9 || l < 0.000_000_01 {
635
+                0.0
636
+            } else {
637
+                max_chroma_for_lh(l, h) / 100.0 * s
638
+            };
639
+
640
+            if s < 0.000_000_01 {
641
+                Ok(Colour::LCH(l, c, 0.0, alpha))
642
+            } else {
643
+                Ok(Colour::LCH(l, c, h, alpha))
644
+            }
645
+        }
646
+        _ => Err(SenError::IncorrectColourFormat),
647
+    }
648
+}
649
+
650
+fn hsluv_from_lch(lch: Colour) -> SenResult<Colour> {
651
+    match lch {
652
+        Colour::LCH(l, c, h, alpha) => {
653
+            let s = if l > 99.999_999_9 || l < 0.000_000_01 {
654
+                0.0
655
+            } else {
656
+                c / max_chroma_for_lh(l, h) * 100.0
657
+            };
658
+
659
+            if c < 0.000_000_01 {
660
+                Ok(Colour::HSLuv(0.0, s, l, alpha))
661
+            } else {
662
+                Ok(Colour::HSLuv(h, s, l, alpha))
663
+            }
664
+        }
665
+        _ => Err(SenError::IncorrectColourFormat),
666
+    }
667
+}
668
+
669
+fn xyz_from_hsluv(hsluv: Colour) -> SenResult<Colour> {
670
+    xyz_from_luv(luv_from_lch(lch_from_hsluv(hsluv)?)?)
671
+}
672
+
673
+fn hsluv_from_xyz(xyz: Colour) -> SenResult<Colour> {
674
+    hsluv_from_lch(lch_from_luv(luv_from_xyz(xyz)?)?)
675
+}
676
+
677
+#[cfg(test)]
678
+mod tests {
679
+
680
+    use super::*;
681
+
682
+    const TOLERANCE: f64 = 0.02;
683
+
684
+    fn f64_within(tolerance: f64, a: f64, b: f64, msg: &'static str) {
685
+        assert!(
686
+            (a - b).abs() < tolerance,
687
+            format!("{} expected: {}, actual: {}", msg, b, a)
688
+        )
689
+    }
690
+
691
+    fn is_format(expected: Format, actual: Format) {
692
+        assert!(
693
+            expected == actual,
694
+            format!("expected: {:?}, actual: {:?}", expected, actual)
695
+        )
696
+    }
697
+
698
+    fn assert_col(col: Colour, format: Format, c0: f64, c1: f64, c2: f64, c3: f64) {
699
+        match col {
700
+            Colour::HSL(h, s, l, alpha) => {
701
+                is_format(format, Format::HSL);
702
+                f64_within(TOLERANCE, h, c0, "HSL H");
703
+                f64_within(TOLERANCE, s, c1, "HSL_S");
704
+                f64_within(TOLERANCE, l, c2, "HSL_L");
705
+                f64_within(TOLERANCE, alpha, c3, "HSL_alpha");
706
+            }
707
+            Colour::HSLuv(h, s, l, alpha) => {
708
+                is_format(format, Format::HSLuv);
709
+                f64_within(TOLERANCE, h, c0, "HSLuv H");
710
+                f64_within(TOLERANCE, s, c1, "HSLuv_S");
711
+                f64_within(TOLERANCE, l, c2, "HSLuv_L");
712
+                f64_within(TOLERANCE, alpha, c3, "HSLuv_alpha");
713
+            }
714
+            Colour::HSV(h, s, v, alpha) => {
715
+                is_format(format, Format::HSV);
716
+                f64_within(TOLERANCE, h, c0, "HSV H");
717
+                f64_within(TOLERANCE, s, c1, "HSV_S");
718
+                f64_within(TOLERANCE, v, c2, "HSV_V");
719
+                f64_within(TOLERANCE, alpha, c3, "HSV_alpha");
720
+            }
721
+            Colour::LAB(l, a, b, alpha) => {
722
+                is_format(format, Format::LAB);
723
+                f64_within(TOLERANCE, l, c0, "LAB_L");
724
+                f64_within(TOLERANCE, a, c1, "LAB_A");
725
+                f64_within(TOLERANCE, b, c2, "LAB_B");
726
+                f64_within(TOLERANCE, alpha, c3, "LAB_alpha");
727
+            }
728
+            Colour::RGB(r, g, b, alpha) => {
729
+                is_format(format, Format::RGB);
730
+                f64_within(TOLERANCE, r, c0, "RGB R");
731
+                f64_within(TOLERANCE, g, c1, "RGB_G");
732
+                f64_within(TOLERANCE, b, c2, "RGB_B");
733
+                f64_within(TOLERANCE, alpha, c3, "RGB_alpha");
734
+            }
735
+            _ => assert_eq!(true, false),
736
+        }
737
+    }
738
+
739
+    fn assert_colour_match(expected: Colour, col: Colour) {
740
+        match expected {
741
+            Colour::HSL(h, s, l, alpha) => assert_col(col, Format::HSL, h, s, l, alpha),
742
+            Colour::HSLuv(h, s, l, alpha) => assert_col(col, Format::HSLuv, h, s, l, alpha),
743
+            Colour::HSV(h, s, v, alpha) => assert_col(col, Format::HSV, h, s, v, alpha),
744
+            Colour::LAB(l, a, b, alpha) => assert_col(col, Format::LAB, l, a, b, alpha),
745
+            Colour::RGB(r, g, b, alpha) => assert_col(col, Format::RGB, r, g, b, alpha),
746
+            _ => assert_eq!(true, false),
747
+        }
748
+    }
749
+
750
+    fn assert_colour_rgb_hsl_match(r: f64, g: f64, b: f64, h: f64, s: f64, l: f64) {
751
+        let rgb = Colour::RGB(r, g, b, 1.0);
752
+        let hsl = Colour::HSL(h, s, l, 1.0);
753
+
754
+        assert_colour_match(rgb, hsl.clone_as(Format::RGB).unwrap());
755
+        assert_colour_match(hsl, rgb.clone_as(Format::HSL).unwrap());
756
+    }
757
+
758
+    #[test]
759
+    fn test_colour() {
760
+        let rgb = Colour::RGB(0.2, 0.09803921568627451, 0.49019607843137253, 1.0);
761
+        let hsl = Colour::HSL(255.6, 0.6666, 0.294, 1.0);
762
+        let lab = Colour::LAB(
763
+            19.555676428108306,
764
+            39.130689315704764,
765
+            -51.76254071703564,
766
+            1.0,
767
+        );
768
+
769
+        assert_colour_match(rgb, rgb.clone_as(Format::RGB).unwrap());
770
+        assert_colour_match(rgb, hsl.clone_as(Format::RGB).unwrap());
771
+        assert_colour_match(rgb, lab.clone_as(Format::RGB).unwrap());
772
+
773
+        assert_colour_match(hsl, rgb.clone_as(Format::HSL).unwrap());
774
+        assert_colour_match(hsl, hsl.clone_as(Format::HSL).unwrap());
775
+        assert_colour_match(hsl, lab.clone_as(Format::HSL).unwrap());
776
+
777
+        assert_colour_match(lab, rgb.clone_as(Format::LAB).unwrap());
778
+        assert_colour_match(lab, hsl.clone_as(Format::LAB).unwrap());
779
+        assert_colour_match(lab, lab.clone_as(Format::LAB).unwrap());
780
+    }
781
+
782
+    #[test]
783
+    fn test_colour_2() {
784
+        let rgb = Colour::RGB(0.066666, 0.8, 0.86666666, 1.0);
785
+        let hsluv = Colour::HSLuv(205.7022764106217, 98.91247496876854, 75.15356872935901, 1.0);
786
+
787
+        assert_colour_match(rgb, hsluv.clone_as(Format::RGB).unwrap());
788
+        assert_colour_match(hsluv, rgb.clone_as(Format::HSLuv).unwrap());
789
+    }
790
+
791
+    #[test]
792
+    fn test_colour_3() {
793
+        assert_colour_rgb_hsl_match(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
794
+        assert_colour_rgb_hsl_match(1.0, 1.0, 1.0, 0.0, 0.0, 1.0);
795
+        assert_colour_rgb_hsl_match(1.0, 0.0, 0.0, 0.0, 1.0, 0.5);
796
+        assert_colour_rgb_hsl_match(0.0, 1.0, 0.0, 120.0, 1.0, 0.5);
797
+        assert_colour_rgb_hsl_match(0.0, 0.0, 1.0, 240.0, 1.0, 0.5);
798
+        assert_colour_rgb_hsl_match(1.0, 1.0, 0.0, 60.0, 1.0, 0.5);
799
+        assert_colour_rgb_hsl_match(0.0, 1.0, 1.0, 180.0, 1.0, 0.5);
800
+        assert_colour_rgb_hsl_match(1.0, 0.0, 1.0, 300.0, 1.0, 0.5);
801
+        assert_colour_rgb_hsl_match(0.7529, 0.7529, 0.7529, 0.0, 0.0, 0.75);
802
+        assert_colour_rgb_hsl_match(0.5, 0.5, 0.5, 0.0, 0.0, 0.5);
803
+        assert_colour_rgb_hsl_match(0.5, 0.0, 0.0, 0.0, 1.0, 0.25);
804
+        assert_colour_rgb_hsl_match(0.5, 0.5, 0.0, 60.0, 1.0, 0.25);
805
+        assert_colour_rgb_hsl_match(0.0, 0.5, 0.0, 120.0, 1.0, 0.25);
806
+        assert_colour_rgb_hsl_match(0.5, 0.0, 0.5, 300.0, 1.0, 0.25);
807
+        assert_colour_rgb_hsl_match(0.0, 0.5, 0.5, 180.0, 1.0, 0.25);
808
+        assert_colour_rgb_hsl_match(0.0, 0.0, 0.5, 240.0, 1.0, 0.25);
809
+    }
810
+}

+ 39
- 0
src/error.rs View File

@@ -0,0 +1,39 @@
1
+// Copyright (C) 2018 Inderjit Gill
2
+
3
+// This program is free software: you can redistribute it and/or modify
4
+// it under the terms of the GNU General Public License as published by
5
+// the Free Software Foundation, either version 3 of the License, or
6
+// (at your option) any later version.
7
+
8
+// This program is distributed in the hope that it will be useful,
9
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+// GNU General Public License for more details.
12
+
13
+// You should have received a copy of the GNU General Public License
14
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
+
16
+pub type SenResult<T> = ::std::result::Result<T, SenError>;
17
+
18
+#[derive(Debug)]
19
+pub enum SenError {
20
+    GeneralError,
21
+
22
+    // colour
23
+    //
24
+    IncorrectColourFormat,
25
+    InvalidColourHue,
26
+    InvalidColourChannel,
27
+
28
+    // parser
29
+    ParserInvalidChar(char),
30
+    ParserInvalidLiteral,
31
+    ParserUnableToParseFloat(String),
32
+    ParserHandledToken,
33
+
34
+
35
+    // MemorySegmentType
36
+    MemorySegmentTypeUnmappableBytecodeArg,
37
+    MemorySegmentTypeUnmappableI32,
38
+
39
+}

+ 47
- 3
src/lib.rs View File

@@ -1,10 +1,54 @@
1
-#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
1
+// Copyright (C) 2018 Inderjit Gill
2 2
 
3
-extern crate sen_colour;
3
+// This program is free software: you can redistribute it and/or modify
4
+// it under the terms of the GNU General Public License as published by
5
+// the Free Software Foundation, either version 3 of the License, or
6
+// (at your option) any later version.
7
+
8
+// This program is distributed in the hope that it will be useful,
9
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+// GNU General Public License for more details.
12
+
13
+// You should have received a copy of the GNU General Public License
14
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
+#![allow(dead_code)]
16
+#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names, too_many_arguments))]
17
+
18
+extern crate cfg_if;
4 19
 extern crate wasm_bindgen;
5 20
 
6
-use sen_colour::{Colour, Format};
21
+mod error;
22
+mod utils;
23
+mod colour;
24
+
25
+use cfg_if::cfg_if;
7 26
 use wasm_bindgen::prelude::*;
27
+use crate::colour::{Colour, Format};
28
+
29
+cfg_if! {
30
+    // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
31
+    // allocator.
32
+    if #[cfg(feature = "wee_alloc")] {
33
+        extern crate wee_alloc;
34
+        #[global_allocator]
35
+        static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
36
+    }
37
+}
38
+
39
+#[wasm_bindgen]
40
+extern "C" {
41
+    #[wasm_bindgen(js_namespace = console)]
42
+    fn log(s: &str);
43
+}
44
+
45
+#[wasm_bindgen]
46
+pub fn say_hi() {
47
+    log("hello from wasm world!!!");
48
+}
49
+
50
+
51
+
8 52
 
9 53
 #[wasm_bindgen]
10 54
 pub struct Transport3C {

+ 32
- 0
src/utils.rs View File

@@ -0,0 +1,32 @@
1
+// Copyright (C) 2018 Inderjit Gill
2
+
3
+// This program is free software: you can redistribute it and/or modify
4
+// it under the terms of the GNU General Public License as published by
5
+// the Free Software Foundation, either version 3 of the License, or
6
+// (at your option) any later version.
7
+
8
+// This program is distributed in the hope that it will be useful,
9
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+// GNU General Public License for more details.
12
+
13
+// You should have received a copy of the GNU General Public License
14
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
+
16
+use cfg_if::cfg_if;
17
+
18
+cfg_if! {
19
+    // When the `console_error_panic_hook` feature is enabled, we can call the
20
+    // `set_panic_hook` function at least once during initialization, and then
21
+    // we will get better error messages if our code ever panics.
22
+    //
23
+    // For more details see
24
+    // https://github.com/rustwasm/console_error_panic_hook#readme
25
+    if #[cfg(feature = "console_error_panic_hook")] {
26
+        extern crate console_error_panic_hook;
27
+        pub use self::console_error_panic_hook::set_once as set_panic_hook;
28
+    } else {
29
+        #[inline]
30
+        pub fn set_panic_hook() {}
31
+    }
32
+}

+ 0
- 10
webpack.config.js View File

@@ -1,10 +0,0 @@
1
-const path = require("path");
2
-
3
-module.exports = {
4
-  entry: "./bootstrap.js",
5
-  output: {
6
-    path: path.resolve(__dirname, "dist"),
7
-    filename: "bootstrap.js",
8
-  },
9
-  mode: "development"
10
-};

+ 2
- 0
www/.gitignore View File

@@ -0,0 +1,2 @@
1
+themacs.js
2
+themacs_bg.wasm

css/base-min.css → www/css/base-min.css View File


css/pure-min.css → www/css/pure-min.css View File


css/stylesheet.css → www/css/stylesheet.css View File


favicon.ico → www/favicon.ico View File


index.html → www/index.html View File

@@ -856,6 +856,22 @@
856 856
       </div>
857 857
     </div>
858 858
 
859
-    <script src='./bootstrap.js'></script>
859
+    <script>
860
+      // The `--no-modules`-generated JS from `wasm-bindgen` attempts to use
861
+      // `WebAssembly.instantiateStreaming` to instantiate the wasm module,
862
+      // but this doesn't work with `file://` urls. This example is frequently
863
+      // viewed by simply opening `index.html` in a browser (with a `file://`
864
+      // url), so it would fail if we were to call this function!
865
+      //
866
+      // Work around this for now by deleting the function to ensure that the
867
+      // `no_modules.js` script doesn't have access to it. You won't need this
868
+      // hack when deploying over HTTP.
869
+      delete WebAssembly.instantiateStreaming;
870
+    </script>
871
+
872
+    <!-- this is the JS generated by the `wasm-bindgen` CLI tool -->
873
+    <script src='./themacs.js'></script>
874
+
875
+    <script src='index.js'></script>
860 876
     </body>
861 877
 </html>

index.js → www/index.js View File

@@ -5,11 +5,9 @@
5 5
 // programming language saturation (i.e. builtin saturation)
6 6
 // clock12 hue
7 7
 // clock12 saturation
8
-import { Transport3C,
9
-         hsl_from_rgb,
10
-         rgb_from_hsl
11
-       } from "./wasm_themacs";
12
-
8
+const { Transport3C,
9
+        hsl_from_rgb,
10
+        rgb_from_hsl } = wasm_bindgen;
13 11
 
14 12
 let colourTable = undefined;
15 13
 
@@ -868,7 +866,6 @@ function pointerToFloat32Array(mem, ptr, length) {
868 866
 }
869 867
 
870 868
 function init() {
871
-  console.log('init');
872 869
   colourTable = document.querySelector("#colour-table");
873 870
 
874 871
   initModel(model);
@@ -876,7 +873,7 @@ function init() {
876 873
   colourTable.onchange = changeEventHandler;
877 874
 
878 875
   // request MIDI access
879
-  if (navigator.requestMIDIAccess) {
876
+  if (false && navigator.requestMIDIAccess) {
880 877
     navigator.requestMIDIAccess({
881 878
       sysex: false
882 879
     }).then(onMIDISuccess(ui), onMIDIFailure);
@@ -885,4 +882,10 @@ function init() {
885 882
   }
886 883
 }
887 884
 
888
-init();
885
+document.addEventListener('DOMContentLoaded', () => {
886
+  wasm_bindgen('./themacs_bg.wasm')
887
+    .then(() => {
888
+      init();
889
+    })
890
+  .catch(console.error);
891
+});

Loading…
Cancel
Save