-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer.cpp
72 lines (60 loc) · 1.87 KB
/
buffer.cpp
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
#include <string.h> // memcpy
#include "buffer.h"
#include "hexxx.h"
Buffer::Buffer() {
fill( 0 );
}
void Buffer::fill( ws2811_led_t c ) {
for( int i = 0; i < HEXAGON_LED_COUNT; i++ ) {
setPixel( i, c );
}
}
void Buffer::fill( ws2811_led_t c, double alpha ) {
for( int i = 0; i < HEXAGON_LED_COUNT; i++ ) {
setPixel( i, c, alpha );
}
}
void Buffer::setPixel( int led, ws2811_led_t c ) {
buffer[ led ] = c;
}
ws2811_led_t Buffer::getPixel( int led ) {
return buffer[ led ];
}
void Buffer::setPixel( int led, ws2811_led_t c, double alpha ) {
ws2811_led_t previouscolor = getPixel( led );
int r = getRed( previouscolor ) * ( 1.0 - alpha ) + getRed( c ) * alpha;
int g = getGreen( previouscolor ) * ( 1.0 - alpha ) + getGreen( c ) * alpha;
int b = getBlue( previouscolor ) * ( 1.0 - alpha ) + getBlue( c ) * alpha;
setPixel( led, color( r, g, b ) );
}
void Buffer::render() {
for( int i = 0; i < HEXAGON_LED_COUNT; i++ ) {
setColor( i, applyGammaCorrection( buffer[ i ] ) );
}
}
void Buffer::rotate(double angle, Buffer* rotated) {
// determine for each (rotated) pixel its original color (before rotation)
for( int rled = 0; rled < HEXAGON_LED_COUNT; rled++ ) {
float rx = led2unitx( rled ),
ry = led2unity( rled ),
x = rx * cos(-angle) - ry * sin(-angle),
y = ry * cos(-angle) + rx * sin(-angle);
int led = unit2led(x, y);
rotated->setPixel(rled, this->getPixel(led));
}
}
void Buffer::rotate(double angle) {
Buffer rotated;
this->rotate(angle, &rotated);
memcpy(this->buffer, rotated.buffer, sizeof(this->buffer));
}
void Buffer::darken( double factor ) {
for( int i = 0; i < HEXAGON_LED_COUNT; i++ ) {
setPixel( i, darkenColor( getPixel( i ), factor ) );
}
}
void Buffer::screenshot() {
for( int i = 0; i < HEXAGON_LED_COUNT; i++ ) {
setPixel( i, applyGammaError( getColor( i ) ) );
}
}