8000 使用 rgba 代替十六进制 · HashCoding/boxbot@eece8d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit eece8d1

Browse files
committed
使用 rgba 代替十六进制
1 parent b77f8bd commit eece8d1

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

boxbot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Boxbot.prototype.commands = [
5353
}
5454
},
5555
{
56-
pattern: /^bru\s+(#\w+)$/i,
56+
pattern: /^bru\s+(.*)$/i,
5757
handler: function (color) {
5858
return this.run(this.setColor, [color])
5959
}

image-reader.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ var ImageReader = function () {
1111
/**
1212
* 读取图片数据
1313
*
14-
* @param {HTMLInputElement} file
14+
* @param {Blob} file
1515
* @param {int} width
1616
* @param {int} height
1717
* @returns {Promise}
1818
*/
1919
ImageReader.prototype.read = function (file, width, height) {
20-
this.width = width
21-
this.height = height
22-
this.reader.readAsDataURL(file.files[0])
2320
return new Promise((function (resolve) {
21+
this.width = width
22+
this.height = height
23+
this.reader.readAsDataURL(file)
2424
this.resolve = resolve
2525
}).bind(this))
2626
}
@@ -32,29 +32,18 @@ ImageReader.prototype.load = function () {
3232
for (var y = 0; y < this.width; y += 1) {
3333
data.push([])
3434
for (var x = 0; x < this.height; x += 1) {
35-
data[y].push(this.toRGB(this.canvas.getImageData(x, y, 1, 1).data))
35+
data[y].push(this.toRGBA(this.canvas.getImageData(x, y, 1, 1).data))
3636
}
3737
}
3838
this.resolve(data)
3939
}
4040

41-
/**
42-
* 十进制转十六进制,如果不足两位则加前导零
43-
*
44-
* @param {int} dec
45-
* @returns {string}
46-
*/
47-
ImageReader.prototype.toHex = function (dec) {
48-
var hex = dec.toString(16)
49-
return hex.length == 2 ? hex : '0' + hex
50-
}
51-
5241
/**
5342
* RGBA 数组转十六进制
5443
*
5544
* @param {CanvasPixelArray} pixel
5645
* @returns {string}
5746
*/
58-
ImageReader.prototype.toRGB = function (pixel) {
59-
return '#' + this.toHex(pixel[0]) + this.toHex(pixel[1]) + this.toHex(pixel[2])
47+
ImageReader.prototype.toRGBA = function (pixel) {
48+
return 'rgba(' + pixel[0] + ',' + pixel[1] + ',' + pixel[2] + ',' + pixel[2] + ')'
6049
}

0 commit comments

Comments
 (0)
0