-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColour.cpp
70 lines (58 loc) · 1.25 KB
/
Colour.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
/*
* Colour.cpp
*
* Created on: Nov 26, 2012
* Author: joost
*/
#include <Arduino.h>
#include "Colour.h"
Colour::Colour() {
setRGB(0, 0, 0);
}
Colour::Colour(uint8_t r, uint8_t g, uint8_t b) {
setRGB(r, g, b);
}
void Colour::setRGB(uint8_t r, uint8_t g, uint8_t b) {
red = r;
green = g;
blue = b;
}
uint8_t Colour::brightness() {
return (red + green + blue) / 3;
}
void Colour::approach(Colour c) {
if (abs(c.red - red) <= 2 && abs(c.green - green) <= 2 && abs(c.blue - blue) <= 2) {
red = c.red;
green = c.green;
blue = c.blue;
return;
}
if (c.brightness() > brightness()) {
red = (c.red + red * 2) / 3;
green = (c.green + green * 2) / 3;
blue = (c.blue + blue * 2) / 3;
} else {
red = (c.red + red * 2) / 3;
green = (c.green + green * 2) / 3;
blue = (c.blue + blue * 2) / 3;
}
}
void Colour::quickApproach(Colour c) {
for (uint8_t i = 0; i < 3; i++) {
approach(c);
}
}
void Colour::average(Colour c) {
red = (red + c.red) / 2;
green = (green + c.green) / 2;
blue = (blue + c.blue) / 2;
}
void Colour::multiply(float factor) {
uint16_t r, g, b;
r = (float) red * factor;
g = (float) green * factor;
b = (float) blue * factor;
red = r > 255 ? 255 : r;
green = g > 255 ? 255 : g;
blue = b > 255 ? 255 : b;
}