-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
36 lines (30 loc) · 984 Bytes
/
index.js
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
var regexLong = /#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})?/gi
var regexShort = /#([a-f0-9])([a-f0-9])([a-f0-9])([a-f0-9])?(...)?/gi
module.exports = transform
module.exports.sync = transform
function transform(filename, src, opts, done) {
src = src.replace(regexLong, function(whole, r, g, b, a) {
return makeVec(r, g, b, a)
}).replace(regexShort, function(whole, r, g, b, a, remaining) {
var str = makeVec(r + r, g + g, b + b, a + a)
if (remaining === 'ine') return whole
if (remaining) str += remaining
return str
})
if (typeof done === 'function') done(null, src)
return src
}
function makeVec(r, g, b, a) {
r = parseInt(r, 16) / 255
g = parseInt(g, 16) / 255
b = parseInt(b, 16) / 255
a = parseInt(a, 16) / 255
return isNaN(a)
? 'vec3('+[r,g,b].map(makeFloat).join(',')+')'
: 'vec4('+[r,g,b,a].map(makeFloat).join(',')+')'
}
function makeFloat(n) {
return String(n).indexOf('.') === -1
? n + '.'
: n
}