My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "CtrlLib.h"

NAMESPACE_UPP

static struct {
const char *name;
ColorF color;
}
s_colors[] = {
{ "Black", &Black },
{ "Red", &Red },
{ "Green", &Green },
{ "Brown", &Brown },
{ "Blue", &Blue },
{ "Magenta", &Magenta },
{ "Cyan", &Cyan },
{ "Gray", &Gray },
{ "LtGray", &LtGray },
{ "LtRed", &LtRed },
{ "LtGreen", &LtGreen },
{ "LtYellow", &LtYellow },
{ "LtBlue", &LtBlue },
{ "LtMagenta", &LtMagenta },
{ "LtCyan", &LtCyan },
{ "Yellow", &Yellow },
{ "WhiteGray", &WhiteGray },
{ "White", &White },

//deprecated: (TODO)
{ "SBlack", &Black },
{ "SRed", &Red },
{ "SGreen", &Green },
{ "SBrown", &Brown },
{ "SBlue", &Blue },
{ "SMagenta", &Magenta },
{ "SCyan", &Cyan },
{ "SGray", &Gray },
{ "SLtGray", &LtGray },
{ "SLtRed", &LtRed },
{ "SLtGreen", &LtGreen },
{ "SLtYellow", &LtYellow },
{ "SLtBlue", &LtBlue },
{ "SLtMagenta", &LtMagenta },
{ "SLtCyan", &LtCyan },
{ "SYellow", &Yellow },
{ "SWhiteGray", &WhiteGray },
{ "SWhite", &White },
};

Color ColorPopUp::hint[18];

void ColorPopUp_InitHint()
{
for(int i = 0; i < 18; i++)
ColorPopUp::hint[i] = LtGray;
}

INITBLOCK {
ColorPopUp_InitHint();
}

void ColorPopUp::Hint(Color c)
{
for(int i = 0; i < 17; i++)
if(hint[i] == c) {
memmove(&hint[i], &hint[i + 1], (17 - i) * sizeof(Color));
hint[17] = LtGray;
}
memmove(&hint[1], &hint[0], 17 * sizeof(Color));
hint[0] = c;
}

String FormatColor(Color c)
{
if(IsNull(c))
return "Null";
for(int i = 0; i < __countof(s_colors); i++)
if((*s_colors[i].color)() == c)
return s_colors[i].name;
return Format("Color(%d, %d, %d)", c.GetR(), c.GetG(), c.GetB());
}

Color ReadColor(CParser& p)
{
for(int i = 0; i < __countof(s_colors); i++)
if(p.Id(s_colors[i].name))
return (*s_colors[i].color)();
p.PassId("Color");
p.PassChar('(');
int r = p.ReadInt();
p.PassChar(',');
int g = p.ReadInt();
p.PassChar(',');
int b = p.ReadInt();
p.PassChar(')');
return Color(minmax(r, 0, 255), minmax(g, 0, 255), minmax(b, 0, 255));
}


static int sCharFilterNoDigit(int c)
{
return IsDigit(c) ? 0 : c;
}

static int sCharFilterHex(int c)
{
return c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' || IsDigit(c) ? c : 0;
}

Color ColorFromText(const char *s)
{
Vector<String> h = Split(s, sCharFilterNoDigit);
if(h.GetCount() == 3 && (strchr(s, ',') || strchr(s, ';') || strchr(s, '.'))) {
int r = atoi(h[0]);
int g = atoi(h[1]);
int b = atoi(h[2]);
if(r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255)
return Color(r, g, b);
}
String hex = Filter(s, sCharFilterHex);
if(hex.GetCount() == 6 || hex.GetCount() == 8) {
dword w = (dword)ScanInt64(~hex, NULL, 16);
return Color(byte(w >> 16), byte(w >> 8), byte(w));
}
return Null;
}

ColorPopUp::~ColorPopUp() {}

int ColorPopUp::GetColorCount() const
{
return 18 + scolors * 18 + 2 * 18 + hints * 18 + 216;
}

Color ColorPopUp::GetColor(int i) const
{
if(!scolors)
i += 18;
if(i < 36)
return *s_colors[i].color;
i -= 36;
if(i < 18)
return GrayColor(255 * (i + 1) / 20);
if(hints) {
i -= 18;
if(i < 18)
return hint[i];
}
i -= 18;
int q = i % 18;
i /= 18;
return Color(255 * (q < 9 ? q + 1 : 18 - q) / 9,
255 * (i < 6 ? i + 1 : 12 - i) / 6,
q < 9 ? i < 6 ? 0 : 200 : i < 6 ? 150 : 255);
}

int ColorPopUp::GetCy()
{
return ((GetColorCount() + 17) / 18) * 16 +
(norampwheel ? 0 : 2) +
(notnull ? 0 : StdFont().Info().GetHeight() + 3 + 2);
}

void ColorPopUp::DrawFilledFrame(Draw &w, int x, int y, int cx, int cy, Color fcol, Color bcol)
{
DrawFrame(w, x, y, cx, cy, fcol);
w.DrawRect(x + 1, y + 1, cx - 2, cy - 2, bcol);
}

void ColorPopUp::DrawFilledFrame(Draw &w, Rect &r, Color fcol, Color bcol)
{
DrawFrame(w, r.left, r.top, r.Width(), r.Height(), fcol);
w.DrawRect(r.left + 1, r.top + 1, r.Width() - 2, r.Height() - 2, bcol);
}

void ColorPopUp::Paint(Draw& w)
{
Size sz = GetSize();
int cy = GetCy();

w.DrawRect(sz, SColorMenu);

int y = 1;

if(!notnull) {
Size fsz = GetTextSize(nulltext, StdFont());
Rect r(1, y, sz.cx - 1, fsz.cy + y + 2);
DrawFrame(w, r, SColorText);
w.DrawText((sz.cx - fsz.cx) / 2, y, nulltext, StdFont(), SColorText());
y = r.bottom + 3;
if(colori == 998)
{
r.Inflate(1);
if(GetMouseLeft())
DrawFrame(w, r, SColorShadow, SColorLight);
else
DrawFrame(w, r, GUI_GlobalStyle() >= GUISTYLE_XP ? SColorText : SColorHighlight);
}
}

int i = 0;
for(;;) {
for(int x = 0; x < 18 * 16; x += 16) {
if(i >= GetColorCount()) {
if(!norampwheel) {
Rect r(8 * 16 + 1, cy + 4, 10 * 16 - 1, sz.cy - 4);
DrawFilledFrame(w, r, SColorText, color);

r.Inflate(1);
if(colori == 999)
if(GetMouseLeft())
DrawFrame(w, r, SColorShadow, SColorLight);
else
DrawFrame(w, r, GUI_GlobalStyle() >= GUISTYLE_XP ? SColorText : SColorHighlight);
}
return;
}

DrawFilledFrame(w, x + 1, y, 14, 14, SColorText, GetColor(i));
if(i < 18 && scolors)
DrawFrame(w, x + 2, y + 1, 12, 12, Blend(SColorLight, SColorHighlight));

if(i == colori)
if(GetMouseLeft())
DrawFrame(w, x, y - 1, 16, 16, SColorShadow, SColorLight);
else
DrawFrame(w, x, y - 1, 16, 16, GUI_GlobalStyle() >= GUISTYLE_XP ? SColorText : SColorHighlight);
i++;
}
y += 16;
}
}

int ColorPopUp::Get(Point p)
{
if(p.y >= GetCy())
return 999;
if(!notnull) {
int y0 = StdFont().Info().GetHeight() + 4;
if(p.y < y0)
return 998;
p.y -= y0;
}
Size sz = GetSize();
if(p.x >= 0 && p.x < sz.cx && p.y >= 0)
return p.x / 16 + p.y / 16 * 18;
return -1;
}

void ColorPopUp::MouseMove(Point p, dword)
{
int ci = Get(p);
if(ci != colori) {
colori = ci;
Refresh();
WhenAction();
}
}

void ColorPopUp::MouseLeave()
{
colori = -1;
Refresh();
}

void ColorPopUp::Finish()
{
popup.Clear();
if(colori >= 0)
WhenSelect();
else
WhenCancel();
}

void ColorPopUp::LeftDown(Point p, dword)
{
Refresh();
}

void ColorPopUp::LeftUp(Point p, dword)
{
Finish();
}

bool ColorPopUp::Key(dword key, int count)
{
if(key == K_ESCAPE) {
Close();
WhenCancel();
}
return true;
}

void ColorPopUp::Ramp()
{
color = wheel <<= ~ramp;
WhenAction();
Refresh();
}

void ColorPopUp::Wheel()
{
color = ramp <<= ~wheel;
WhenAction();
Refresh();
}

Color ColorPopUp::Get() const
{
if(colori >= 0 && colori < GetColorCount())
return GetColor(colori);
else
if(colori == 999)
return color;
else
return Null;
}

void ColorPopUp::PopupDeactivate() {
if(popup && popup->IsOpen() && !animating && open) {
popup.Clear();
IgnoreMouseClick();
WhenCancel();
}
}

void ColorPopUp::PopUp(Ctrl *owner, Color c)
{
int cy = norampwheel ? 0 : 110;
Size sz = AddFrameSize(18 * 16, GetCy() + cy);
Rect wr = GetWorkArea();
Rect r = owner->GetScreenRect();
int x = r.left;
int y = r.bottom;
BottomPos(0, sz.cy).RightPos(0, sz.cx);
Point start(x, y);
if(x + sz.cx >= wr.right) {
x = r.right - sz.cx;
start.x = r.right;
LeftPos(0, sz.cx);
}
if(y + sz.cy >= wr.bottom) {
y = r.top - sz.cy;
start.y = r.top;
TopPos(0, sz.cy);
}

Rect rt = RectC(x, y, sz.cx, sz.cy);

open = false;
popup.Create();
popup->color = this;
popup->Add(*this);
popup->SetRect(RectC(start.x, start.y, 3, 3));

if(!norampwheel) {
ramp.LeftPos(0, 18*7).VSizePos(GetCy(), 0);
wheel.LeftPos(18*9 - 1, 18*7).VSizePos(GetCy(), 0);
}
ramp <<= c;
wheel <<= c;
color = c;
colori = -1;

if(GUI_PopUpEffect()) {
animating = true;
popup->PopUp(owner, true, true, GUI_GlobalStyle() >= GUISTYLE_XP);
SetFocus();
Ctrl::ProcessEvents();
Animate(*popup, rt, GUIEFFECT_SLIDE);
animating = false;
}

popup->SetRect(rt);
if(!popup->IsOpen())
popup->PopUp(owner, true, true, true);
SetFocus();
open = true;
}

void ColorPopUp::Select()
{
colori = 999;
Finish();
}

ColorPopUp::ColorPopUp()
{
norampwheel = false;
notnull = false;
scolors = false;
animating = false;
hints = false;
open = false;
SetFrame(MenuFrame());
Add(ramp);
Add(wheel);
ramp <<= THISBACK(Ramp);
wheel <<= THISBACK(Wheel);
ramp.WhenLeftDouble = wheel.WhenLeftDouble = THISBACK(Select);
BackPaint();
nulltext = t_("(transparent)");
}

END_UPP_NAMESPACE

Change log

r4307 by cxl on Dec 16, 2011   Diff
*CtrlLib: Fixed ColorPopUp issue in X11
Go to: 
Project members, sign in to write a code review

Older revisions

r4273 by cxl on Dec 8, 2011   Diff
CtrlLib: Improved heurestics in
ColorPopup reading colors from text
clipboard
r3974 by cxl on Oct 8, 2011   Diff
.CtrlLib: ColorPopup minor issue
r3971 by cxl on Oct 8, 2011   Diff
CtrlLib: ColorPusher now supports hint
colors, based on colors previous set
to all instances of ColorPusher, also
if clipboard contains text with color
specification (either 3 decimal
...
All revisions of this file

File info

Size: 8171 bytes, 405 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting