-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWebSafeSelector.cpp
143 lines (113 loc) · 2.99 KB
/
WebSafeSelector.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
/*
* Copyright 2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* John Scipione <jscipione@gmail.com>
*/
#include "WebSafeSelector.h"
#include <Control.h>
#include <InterfaceDefs.h>
#include <Point.h>
#include <Polygon.h>
#include <Window.h>
rgb_color
round_to_nearest_websafe_color(rgb_color color)
{
color.red = (uint8)(floor((color.red + (0x33 / 2)) / 0x33) * 0x33);
color.green = (uint8)(floor((color.green + (0x33 / 2)) / 0x33) * 0x33);
color.blue = (uint8)(floor((color.blue + (0x33 / 2)) / 0x33) * 0x33);
return color;
}
WebSafeSelector::WebSafeSelector()
:
ColorSelector()
{
Show();
}
WebSafeSelector::~WebSafeSelector()
{
}
void
WebSafeSelector::Draw(BRect updateRect)
{
ColorSelector::Draw(updateRect);
if (fIsHidden) {
SetHighColor(ViewColor());
FillRect(updateRect);
return;
}
if (updateRect.Intersects(INDICATOR_RECT)) {
rgb_color black = (rgb_color){ 0, 0, 0 };
rgb_color light;
rgb_color medium;
rgb_color dark;
if (fMouseOver) {
light = (rgb_color){ 0, 255, 0, 255 };
medium = (rgb_color){ 255, 0, 0, 255 };
dark = (rgb_color){ 0, 0, 255, 255 };
} else {
light = tint_color(black, B_LIGHTEN_MAX_TINT);
medium = tint_color(black, B_LIGHTEN_2_TINT);
dark = tint_color(black, B_LIGHTEN_1_TINT);
}
BRect bounds = INDICATOR_RECT;
BPoint pointList[4];
pointList[0] = bounds.LeftTop() + BPoint(0, 3);
pointList[1] = bounds.LeftTop() + BPoint(0, 9);
pointList[2] = bounds.LeftTop() + BPoint(5, 12);
pointList[3] = bounds.LeftTop() + BPoint(5, 6);
BPolygon* rhombus1 = new BPolygon(pointList, 4);
SetHighColor(dark);
FillPolygon(rhombus1);
SetHighColor(black);
StrokePolygon(rhombus1);
delete rhombus1;
pointList[0] = bounds.LeftTop() + BPoint(5, 6);
pointList[1] = bounds.LeftTop() + BPoint(5, 12);
pointList[2] = bounds.LeftTop() + BPoint(10, 9);
pointList[3] = bounds.LeftTop() + BPoint(10, 3);
BPolygon* rhombus2 = new BPolygon(pointList, 4);
SetHighColor(light);
FillPolygon(rhombus2);
SetHighColor(black);
StrokePolygon(rhombus2);
delete rhombus2;
pointList[0] = bounds.LeftTop() + BPoint(0, 3);
pointList[1] = bounds.LeftTop() + BPoint(5, 6);
pointList[2] = bounds.LeftTop() + BPoint(10, 3);
pointList[3] = bounds.LeftTop() + BPoint(5, 0);
BPolygon* rhombus3 = new BPolygon(pointList, 4);
SetHighColor(medium);
FillPolygon(rhombus3);
SetHighColor(black);
StrokePolygon(rhombus3);
delete rhombus3;
}
}
void
WebSafeSelector::Hide() {
ColorSelector::Hide();
SetToolTip("");
}
void
WebSafeSelector::Show() {
ColorSelector::Show();
SetToolTip("Click to select the nearest web safe color");
}
void
WebSafeSelector::SetColor(rgb_color color)
{
if ((color.red % 0x33) == 0 &&
(color.green % 0x33) == 0 &&
(color.blue % 0x33) == 0) {
if (!IsHidden())
Hide();
} else if (IsHidden())
Show();
color = round_to_nearest_websafe_color(color);
color.alpha = 255;
fColor = color;
if (Window() && !IsHidden())
Draw(COLOR_RECT);
}