This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PixelBuffer.js
137 lines (121 loc) · 3.45 KB
/
PixelBuffer.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
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
var color = require('tinycolor2'),
Pixel = require('./Pixel')
/**
* Representation of a pixel strand in the form of a raw buffer
* @param {number} num_pixels Number of pixels in the strand
*/
function PixelBuffer(num_pixels){
this.buffer = new Buffer(num_pixels * 3) // Internal buffer, each pixel is a group of three values
this.readBuffer = new Buffer(num_pixels * 3)
this.buffer.fill(0)
this.readBuffer.fill(0)
}
/**
* Returns the buffer associated with the object
* @return {Buffer} Buffer representing the pixel strand's raw data
*/
PixelBuffer.prototype.get = function(){
return this.buffer
}
/**
* Get the Pixel object at the specified index
* @param {number} i Index of the Pixel to retrieve
* @return {Pixel} Pixel object at the specified index
*/
PixelBuffer.prototype.getPixel = function(i){
return new Pixel(this.buffer[i * 3],
this.buffer[i * 3 + 1],
this.buffer[i * 3 + 2])
}
/**
* Set the RGB value of the pixel at the specified index
* @param {number} i Index of the Pixel to set
* @param {number} r Red value
* @param {number} g Green value
* @param {number} b Blue value
*/
PixelBuffer.prototype.setRGB = function(i, r, g, b){
this.buffer[i * 3] = r
this.buffer[i * 3 + 1] = g
this.buffer[i * 3 + 2] = b
}
/**
* Set the HSL value of the pixel at the specified index
* @param {number} i Index of the Pixel to set
* @param {number} h Hue value
* @param {number} s Saturation value
* @param {number} l Lightness value
*/
PixelBuffer.prototype.setHSL = function(i, h, s, l){
var c = color({
h: h,
s: s,
l: l
}).toRgb()
this.setRGB(
i,
c.r,
c.g,
c.b
)
}
/**
* Set the RGB values at the specified index through a Pixel object
* @param {number} i Index of the Pixel to set
* @param {Pixel} pixel Pixel to set at the specified index
*/
PixelBuffer.prototype.setPixel = function(i, pixel){
var rgb = pixel.getRGB()
this.buffer[i * 3] = rgb.r
this.buffer[i * 3 + 1] = rgb.g
this.buffer[i * 3 + 2] = rgb.b
}
/**
* Fill the entire pixel strand with a specific RGB color
* @param {number} r Red value
* @param {number} g Green value
* @param {number} b Blue value
*/
PixelBuffer.prototype.fillRGB = function(r, g, b){
this.fillRangeRGB(0, this.buffer.length / 3, r, g, b)
}
/**
* Fill a specific range of pixels with a specific color. The other pixels are
* not modified.
* @param {number} start First pixel to change the color of
* @param {number} end Last pixel to change the color of
* @param {number} r Red value
* @param {number} g Green value
* @param {number} b Blue value
*/
PixelBuffer.prototype.fillRangeRGB = function(start, end, r, g, b){
if(start < 0 || start > this.buffer.length / 3
|| end < 0 || end < start || end > this.buffer.length / 3){
return; // You can't fill the pixels in that range
}
for(var i = start * 3; i < end * 3; i += 3){
this.buffer[i] = r
this.buffer[i + 1] = g
this.buffer[i + 2] = b
}
}
/**
* Multiply the RGB values provided onto the pixel buffer
* @param {number} r Red value
* @param {number} g Green value
* @param {number} b Blue value
*/
PixelBuffer.prototype.multiplyRGB = function(r, g, b){
for(var i = 0; i < this.buffer.length; i += 3){
this.buffer[i] = this.buffer[i] * r / 255
this.buffer[i + 1] = this.buffer[i + 1] * g / 255
this.buffer[i + 2] = this.buffer[i + 2] * b / 255
}
}
/**
* Turn the entire pixel strand off
*/
PixelBuffer.prototype.blank = function(){
this.fillRGB(0, 0, 0)
}
module.exports = PixelBuffer