-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBCell.cpp
163 lines (144 loc) · 3.98 KB
/
RGBCell.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "RGBCell.h"
#include <ofMain.h>
RGBCell::RGBCell() {}
RGBCell::RGBCell(float x, float y, float d)
{
this->position = ofVec2f(x * d, y * d);
this->coordinates = ofVec2f(x, y);
this->width = d / 3;
this->height = d;
this->r = this->lr = this->tr = 0;
this->g = this->lg = this->tg = 0;
this->b = this->lb = this->tb = 0;
this->isChanging = false;
this->endTime = this->startTime = this->currentTime = 0;
this->state = 0;
this->delay = 0;
}
void RGBCell::SetColors(float r, float g, float b)
{
this->r = r;
this->g = g;
this->b = b;
}
void RGBCell::ChangeTo(float rr, float gg, float bb, int durationInSecs, int delayInSecs)
{
this->isChanging = true;
this->tr = rr;
this->tg = gg;
this->tb = bb;
this->delay = delay;
this->startTime = ofGetFrameNum();
this->endTime = ofGetFrameNum() + 150;
this->currentTime = 0;
}
void RGBCell::Update(int state)
{
if (state == 0)
{
this->UpdateNoise(1, 1, 1);
}
else if (state == 1)
{
this->UpdateImage();
}
else if (state == 2)
{
this->UpdateImage();
}
else if (state == 3)
{
this->UpdateImage();
}
else if (state == 4)
{
this->UpdateImage();
}
}
void RGBCell::UpdateImage()
{
if (!this->isChanging) return;
int f = ofGetFrameNum();
if (f > this->endTime)
{
this->isChanging = false;
return;
}
if (f >= this->startTime)
{
this->lr = this->r;
this->lg = this->g;
this->lb = this->b;
this->r = this->GetAnimationValue(this->r, this->tr, this->startTime, this->endTime, f);
this->g = this->GetAnimationValue(this->g, this->tg, this->startTime, this->endTime, f);
this->b = this->GetAnimationValue(this->b, this->tb, this->startTime, this->endTime, f);
}
}
float RGBCell::GetAnimationValue(float start, float end, int startTime, int endTime, int currentTime)
{
float diff = end - start;
float t = (currentTime - startTime) / (float)((endTime - startTime));
float res = start + this->Calculate(t) * diff;
return res;
}
float RGBCell::Calculate(float t)
{
return 1 - (1 - (t * t)) * (1 - (t * t));
}
void RGBCell::UpdateNoise(float rr, float gg, float bb)
{
float f = (ofGetElapsedTimeMillis() * 0.00075);
float x = this->position.x / (ofGetHeight() / 4);
float y = this->position.y / (ofGetWidth() / 4);
this->r = (ofSignedNoise(x, y, 0, 0 + f) / 2 + .5);this->r *= this->r * rr;
this->g = (ofSignedNoise(x, y, 3, 3 + f) / 2 + .5);this->g *= this->g * gg;
this->b = (ofSignedNoise(x, y, 6, 6 + f) / 2 + .5);this->b *= this->b * bb;
}
void RGBCell::Draw()
{
ofSetColor(ofColor::red, this->r * 255);
ofDrawRectangle(
this->position.x + (this->width * (1 - this->r)),
this->position.y + (this->height * (1 - this->r)),
this->width * this->r,
this->height * this->r
);
ofSetColor(ofColor::green, this->g * 255);
ofDrawRectangle(
this->position.x + this->width + (this->width * (1 - this->g)),
this->position.y + (this->height * (1 - this->g)),
this->width * this->g,
this->height * this->g
);
ofSetColor(ofColor::blue, this->b * 255);
ofDrawRectangle(
this->position.x + this->width * 2 + (this->width * (1 - this->b)),
this->position.y + (this->height * (1 - this->b)),
this->width * this->b,
this->height * this->b
);
}
void RGBCell::Draw2()
{
ofSetColor(ofColor::red, this->r * 255);
ofDrawRectangle(
this->position.x + (this->width * (1 - this->r)),
this->position.y + (this->height * (1 - this->r)),
this->width * this->r,
this->height * this->r
);
ofSetColor(ofColor::green, this->g * 255);
ofDrawRectangle(
this->position.x + this->width + (this->width * (1 - this->g)),
this->position.y + (this->height * (1 - this->g)),
this->width * this->g,
this->height * this->g
);
ofSetColor(ofColor::blue, this->b * 255);
ofDrawRectangle(
this->position.x + this->width * 2 + (this->width * (1 - this->b)),
this->position.y + (this->height * (1 - this->b)),
this->width * this->b,
this->height * this->b
);
}