-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·74 lines (68 loc) · 1.83 KB
/
Copy pathindex.js
File metadata and controls
executable file
·74 lines (68 loc) · 1.83 KB
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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var objectAssign = require('object-assign');
var rework = require('rework');
var reworkFunction = require('rework-plugin-function');
module.exports = function (options) {
options = options || {};
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-hsv2hex', 'Streaming not supported'));
return;
}
options = objectAssign({}, options, {source: file.path});
try {
file.contents = new Buffer(
rework(file.contents.toString())
.use(reworkFunction({
hsv: function(h, s, v) {
s = s.replace('%', '');
v = v.replace('%', '');
if (s > 1) { s /= 100; }
if (v > 1) { v /= 100; }
var rgb, i, data = [];
if (s === 0) {
rgb = [v,v,v];
} else {
h = h / 60;
i = Math.floor(h);
data = [v*(1-s), v*(1-s*(h-i)), v*(1-s*(1-(h-i)))];
switch(i) {
case 0:
rgb = [v, data[2], data[0]];
break;
case 1:
rgb = [data[1], v, data[0]];
break;
case 2:
rgb = [data[0], v, data[2]];
break;
case 3:
rgb = [data[0], data[1], v];
break;
case 4:
rgb = [data[2], data[0], v];
break;
default:
rgb = [v, data[0], data[1]];
break;
}
}
return '#' + rgb.map(function(x){
return ("0" + Math.round(x*255).toString(16)).slice(-2);
}).join('');
}
}))
.toString()
);
cb(null, file);
} catch (err) {
cb(new gutil.PluginError('gulp-hsv2hex', err, {fileName: file.path}));
}
});
};