-
Notifications
You must be signed in to change notification settings - Fork 2
/
ColorSelector.cpp
188 lines (144 loc) · 3.37 KB
/
ColorSelector.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
/*
* Copyright 2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* John Scipione <[email protected]>
*/
#include "ColorSelector.h"
#include <InterfaceDefs.h>
#include <Point.h>
#include <Size.h>
#include <Window.h>
ColorSelector::ColorSelector()
:
BControl("ColorSelector", "", new BMessage(MSG_COLOR_SELECTOR),
B_WILL_DRAW),
fIsHidden(false),
fMouseOver(false)
{
SetExplicitMaxSize(
BSize(COLOR_RECT.right + COLOR_RECT.left, B_SIZE_UNSET));
}
ColorSelector::~ColorSelector()
{
}
void
ColorSelector::Draw(BRect updateRect)
{
if (fIsHidden) {
SetHighColor(ViewColor());
FillRect(updateRect);
return;
}
if (updateRect.Intersects(COLOR_RECT)) {
rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR);
rgb_color shadow = tint_color(background, B_DARKEN_1_TINT);
rgb_color darkShadow = tint_color(background, B_DARKEN_3_TINT);
rgb_color light = tint_color(background, B_LIGHTEN_MAX_TINT);
BRect bounds = COLOR_RECT;
BeginLineArray(4);
AddLine(BPoint(bounds.left, bounds.bottom),
BPoint(bounds.left, bounds.top), shadow);
AddLine(BPoint(bounds.left + 1.0, bounds.top),
BPoint(bounds.right, bounds.top), shadow);
AddLine(BPoint(bounds.right, bounds.top + 1.0),
BPoint(bounds.right, bounds.bottom), light);
AddLine(BPoint(bounds.right - 1.0, bounds.bottom),
BPoint(bounds.left + 1.0, bounds.bottom), light);
EndLineArray();
bounds.InsetBy(1.0, 1.0);
BeginLineArray(4);
AddLine(BPoint(bounds.left, bounds.bottom),
BPoint(bounds.left, bounds.top), darkShadow);
AddLine(BPoint(bounds.left + 1.0, bounds.top),
BPoint(bounds.right, bounds.top), darkShadow);
AddLine(BPoint(bounds.right, bounds.top + 1.0),
BPoint(bounds.right, bounds.bottom), background);
AddLine(BPoint(bounds.right - 1.0, bounds.bottom),
BPoint(bounds.left + 1.0, bounds.bottom), background);
EndLineArray();
bounds.InsetBy(1.0, 1.0);
SetHighColor(fColor);
FillRect(bounds);
}
}
status_t
ColorSelector::Invoke(BMessage *message)
{
if (message == NULL)
message = Message();
message->RemoveName("selected_color");
message->AddData("selected_color", B_RGB_COLOR_TYPE, &fColor,
sizeof(fColor));
return BControl::Invoke(message);
}
void
ColorSelector::MouseDown(BPoint where)
{
if (fIsHidden)
return;
Window()->Activate();
BMessage message;
message.AddData("RGBColor", B_RGB_COLOR_TYPE, &fColor, sizeof(fColor));
Invoke();
BControl::MouseDown(where);
}
void
ColorSelector::MouseMoved(BPoint where, uint32 code, const BMessage *message)
{
if (fIsHidden)
return;
switch (code) {
case B_ENTERED_VIEW:
case B_INSIDE_VIEW:
fMouseOver = true;
break;
case B_EXITED_VIEW:
case B_OUTSIDE_VIEW:
fMouseOver = false;
break;
}
Invalidate(INDICATOR_RECT);
}
void
ColorSelector::Hide()
{
fIsHidden = true;
fMouseOver = false;
// so that indicator won't be colored when shown
Invalidate();
}
void
ColorSelector::Show()
{
fIsHidden = false;
Invalidate();
}
bool
ColorSelector::IsHidden()
{
return fIsHidden;
}
rgb_color
ColorSelector::GetColor() const
{
return fColor;
}
void
ColorSelector::SetColor(long int c)
{
rgb_color color;
color.red = (c >> 16) & 255;
color.green = (c >> 8) & 255;
color.blue = c & 255;
SetColor(color);
}
void
ColorSelector::SetColor(rgb_color color)
{
color.alpha = 255;
fColor = color;
if (Window() && !IsHidden())
Draw(COLOR_RECT);
}