-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
187 lines (164 loc) · 4.57 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <string>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int clampColor(int color);
void setColor(string device, char hexCol[8]);
int convertQuality(char axis, int simpleQuality);
int main()
{
// user editable
int quality = 5;
float brightness = 1.5;
int refreshDelay = 10;
string Device1 = "0";
string Device2 = "1";
const unsigned short screenHeight = 1080;
const unsigned short screenWidth = 1920;
int yQuality = convertQuality('y', quality);
int xQuality = convertQuality('x', quality);
// 0: Red, 1: Green, 2: Blue
int colors[3][yQuality][xQuality];
Display *d = XOpenDisplay((char *) NULL);
XImage *image;
// main loop
while (true) {
// get colors from screen
int y = 0;
while (y < screenHeight) {
int x = 0;
while (x < screenWidth) {
XColor c;
image = XGetImage (d, XRootWindow (d, XDefaultScreen (d)), x, y, 1, 1, AllPlanes, XYPixmap);
c.pixel = XGetPixel (image, 0, 0);
XFree (image);
XQueryColor (d, XDefaultColormap(d, XDefaultScreen (d)), &c);
colors[0][y / (screenHeight/yQuality)][x / (screenWidth/xQuality)] = c.red/256;
colors[1][y / (screenHeight/yQuality)][x / (screenWidth/xQuality)] = c.green/256;
colors[2][y / (screenHeight/yQuality)][x / (screenWidth/xQuality)] = c.blue/256;
x += screenWidth/xQuality;
}
cout << y << endl;
y += screenHeight/yQuality;
}
// add colors and get average
int rTotal = 0;
int gTotal = 0;
int bTotal = 0;
for (int y = 0; y < yQuality; y++) {
for (int x = 0; x < xQuality; x++){
rTotal += colors[0][y][x];
gTotal += colors[1][y][x];
bTotal += colors[2][y][x];
}
}
int rAvg = rTotal / (xQuality * yQuality);
int gAvg = gTotal / (xQuality * yQuality);
int bAvg = bTotal / (xQuality * yQuality);
// add brightness an limit to 255
rAvg = clampColor(rAvg * brightness);
gAvg = clampColor(gAvg * brightness);
bAvg = clampColor(bAvg * brightness);
// convert RGB color to hex
char hexAvg[8];
std::snprintf(hexAvg, sizeof hexAvg, "%02x%02x%02x", rAvg, gAvg, bAvg);
setColor(Device1, hexAvg);
setColor(Device2, hexAvg);
sleep(refreshDelay);
}
return 0;
}
int clampColor(int color) {
if (color > 255) {
return 255;
} else if (color < 0) {
return 0;
} else {
return color;
}
}
void setColor(string device, char hexCol[8]) {
string cmd("openrgb -d ");
cmd += device;
cmd += " -c ";
cmd += hexCol;
cout << cmd << endl;
system(cmd.c_str());
}
int convertQuality(char axis, int simpleQuality) {
int finalQuality;
if (axis == 'x') {
switch (simpleQuality) {
case 10:
finalQuality = 960;
break;
case 9:
finalQuality = 480;
break;
case 8:
finalQuality = 320;
break;
case 7:
finalQuality = 160;
break;
case 6:
finalQuality = 120;
break;
case 5:
finalQuality = 80;
break;
case 4:
finalQuality = 40;
break;
case 3:
finalQuality = 20;
break;
case 2:
finalQuality = 10;
break;
case 1:
finalQuality = 1;
break;
}
} else if (axis == 'y') {
switch (simpleQuality) {
case 10:
finalQuality = 540;
break;
case 9:
finalQuality = 270;
break;
case 8:
finalQuality = 180;
break;
case 7:
finalQuality = 120;
break;
case 6:
finalQuality = 90;
break;
case 5:
finalQuality = 60;
break;
case 4:
finalQuality = 45;
break;
case 3:
finalQuality = 24;
break;
case 2:
finalQuality = 10;
break;
case 1:
finalQuality = 1;
break;
}
} else {
cout << "wrong axis in convertQuality" << endl;
}
return finalQuality;
}