-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_box.cpp
131 lines (101 loc) · 3.62 KB
/
color_box.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
#include "app.h"
#include "color_box.h"
#include <cstdio>
ColorBox::ColorBox(int x,int y,int w, int h, const char *tip) : Image(x, y, w, h, tip) {
satRows = 1;
satCols = 2;
hueCols = app->windowWidth - 16;
valRows = app->windowHeight;
}
ColorBox::~ColorBox() {
}
void ColorBox::setColorToChange(TCODColor *col) {
// colorToChange is the color that gets set when the user picks his color
colorToChange = col;
}
void ColorBox::drawColorGrid(TCODConsole *con, float saturation) {
int x;
int y;
TCODColor col;
// Draw a grid of colors as follows:
// A table of saturation groups with satRows rows and satCols columns
// In the columns of each cell should hueCols columns of hues
// In the rows of each cell should valRows rows of values(brightness)
for(int sr = 0; sr < satRows; sr++) {
for(int sc = 0; sc < satCols; sc++) {
for(int hc = 0; hc < hueCols; hc++) {
for(int vr = 0; vr < valRows; vr++) {
x = (sc * hueCols) + hc;
y = (sr * valRows) + vr;
float hue = (float)hc / hueCols * 360.0;
//float sat = 1 - (float)(sr * satCols + sc) / (satRows * satCols);
float sat = saturation;
float val = 1 - (float)vr / (valRows - 1); // the valRows-1 ensures we get 0,0,0 in the grid
// Make sure the last saturation cell is grayscale
if(sr == satRows - 1 && sc == satCols - 1)
sat = 0;
col.setHSV(hue, sat, val);
if(x < app->windowWidth && y < app->windowHeight)
con->setCharBackground(x, y, col);
}
}
}
}
}
void ColorBox::onButtonClick() {
int windowWidth = app->windowWidth;
int windowHeight = app->windowHeight;
TCODConsole *con = new TCODConsole(windowWidth, windowHeight);
con->clear();
drawColorGrid(con, 1);
con->setBackgroundFlag(TCOD_BKGND_SET);
con->setAlignment(TCOD_CENTER);
con->print(app->windowWidth - 8, app->windowHeight - 1, "Saturation");
con->flush();
TCOD_key_t key;
TCOD_mouse_t mouse;
int infoX = 0;
int infoY = 0;
int infoW = 15;
int infoH = 10;
TCODConsole *con2 = new TCODConsole(infoW, infoH);
do {
TCODSystem::checkForEvent(TCOD_EVENT_MOUSE,&key,&mouse);
// If the mouse is on the gray zone on the right
if(mouse.cx > windowWidth - 16 && mouse.rbutton) {
// Change the saturation of the colors
drawColorGrid(con, 1 - ((float)mouse.cy)/windowHeight);
}
TCODColor col = con->getCharBackground(mouse.cx, mouse.cy);
// Draw the info box
con2->setDefaultBackground(TCODColor::black);
con2->printFrame(0, 0, infoW, infoH, true, TCOD_BKGND_NONE, "Info");
con2->setAlignment(TCOD_LEFT);
con2->setBackgroundFlag(TCOD_BKGND_NONE);
con2->print(1,2,"r: %i\ng: %i\nb: %i",col.r, col.g, col.b);
float h, s, v;
col.getHSV(&h, &s, &v);
con2->print(1, 6, "h: %.0f\ns: %.0f\nv: %.0f\n", h/360*255, s * 255, v * 255);
con2->setDefaultBackground(col);
con2->rect(7, 2, 7, 7, true);
con2->flush();
// Make sure that the info window does not go out of the screen
if(mouse.cx >= windowWidth - infoW - 2)
infoX = mouse.cx - infoW - 2;
else
infoX = mouse.cx + 2;
if(mouse.cy >= windowHeight - infoH)
infoY = windowHeight - infoH;
else
infoY = mouse.cy + 2;
TCODConsole::blit(con, 0, 0, windowWidth, windowHeight, TCODConsole::root, 0, 0);
TCODConsole::blit(con2, 0, 0, infoW, infoH, TCODConsole::root, infoX, infoY);
// Pressing left button chooses the color
if(mouse.lbutton_pressed) {
*colorToChange = con->getCharBackground(mouse.cx, mouse.cy);
break;
}
} while(key.vk != TCODK_ESCAPE);
delete con;
delete con2;
}