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
#include "Core.h"

NAMESPACE_UPP

void RGBtoHSV(double r, double g, double b, double& h, double& s, double& v)
{
double delta;
if((v = max(r, max(g, b))) == 0 || (delta = v - min(r, min(g, b))) == 0)
{
h = s = 0;
return;
}
s = delta / v;
delta *= 6;
if(g == v)
h = 1 / 3.0 + (b - r) / delta;
else if(b == v)
h = 2 / 3.0 + (r - g) / delta;
else
if((h = (g - b) / delta) < 0)
h += 1;
}

void HSVtoRGB(double h, double s, double v, double& r, double& g, double& b)
{
if(s == 0)
{
r = g = b = v;
return;
}
double rem = fmod(h *= 6, 1);
double p = v * (1 - s);
double q = v * (1 - s * rem);
double t = v * (1 - s * (1 - rem));
switch(ffloor(h))
{
default: NEVER(); // invalid color!
case 6:
case 0: r = v; g = t; b = p; break;
case 1: r = q; g = v; b = p; break;
case 2: r = p; g = v; b = t; break;
case 3: r = p; g = q; b = v; break;
case 4: r = t; g = p; b = v; break;
case -1:
case 5: r = v; g = p; b = q; break;
}
}

Color HsvColorf(double h, double s, double v)
{
double r, g, b;
HSVtoRGB(h, s, v, r, g, b);
return Color(min(int(r * 255), 255), min(int(g * 255), 255), min(int(b * 255), 255));
}

dword Color::Get() const
{
if(IsNullInstance()) return 0;
dword c = color;
return c & 0xffffff;
}

Color::operator RGBA() const
{
RGBA color;
if(IsNullInstance())
Zero(color);
else {
color.r = GetR();
color.g = GetG();
color.b = GetB();
color.a = 255;
}
return color;
}

Color::Color(RGBA rgba)
{
if(rgba.a == 0)
color = 0xffffffff;
else {
if(rgba.a == 255)
color = RGB(rgba.r, rgba.g, rgba.b);
else {
int alpha = 65536 / rgba.a;
color = RGB((alpha * rgba.r) >> 8, (alpha * rgba.g) >> 8, (alpha * rgba.b) >> 8);
}
}
}

void Color::Jsonize(JsonIO& jio)
{
int r, g, b;
if(IsNullInstance()) {
r = g = b = Null;
}
else {
r = GetR();
g = GetG();
b = GetB();
}
jio("red", r)("green", g)("blue", b);
if(IsNull(r))
*this = Null;
else
*this = Color(r, g, b);
}

void Color::Xmlize(XmlIO& xio)
{
int r, g, b;
if(IsNullInstance()) {
r = g = b = Null;
}
else {
r = GetR();
g = GetG();
b = GetB();
}
xio
.Attr("red", r)
.Attr("green", g)
.Attr("blue", b)
;
if(IsNull(r))
*this = Null;
else
*this = Color(r, g, b);
}

RGBA operator*(int alpha, Color c)
{
RGBA r;
r.a = alpha;
alpha += (alpha >> 7);
r.r = (alpha * c.GetR()) >> 8;
r.g = (alpha * c.GetG()) >> 8;
r.b = (alpha * c.GetB()) >> 8;
return r;
}

template<>
String AsString(const Color& c) {
if(IsNull(c))
return "Color(Null)";
return Format("Color(%d, %d, %d)", c.GetR(), c.GetG(), c.GetB());
}

String ColorToHtml(Color color)
{
return Format("#%02X%02X%02X", color.GetR(), color.GetG(), color.GetB());
}

Color Blend(Color c1, Color c2, int alpha)
{
int a = (alpha >> 7) + alpha;
return Color(min(((a * (c2.GetR() - c1.GetR())) >> 8) + c1.GetR(), 255),
min(((a * (c2.GetG() - c1.GetG())) >> 8) + c1.GetG(), 255),
min(((a * (c2.GetB() - c1.GetB())) >> 8) + c1.GetB(), 255));
}

#ifdef SVO_VALUE
INITBLOCK {
Value::SvoRegister<Color>("Color");
}
#else
INITBLOCK {
RichValue<Color>::Register();
}
#endif

int Grayscale(const Color& c)
{
return (77 * c.GetR() + 151 * c.GetG() + 28 * c.GetB()) >> 8;
}

bool IsDark(Color c)
{
return Grayscale(c) < 80;
}

bool IsLight(Color c)
{
return Grayscale(c) > 255 - 80;
}

END_UPP_NAMESPACE

Change log

r4681 by cxl on Mar 11, 2012   Diff
*Core: SvoValue: fixed a couple of issues
with Null
Go to: 
Project members, sign in to write a code review

Older revisions

r4674 by cxl on Mar 10, 2012   Diff
Core: some Xmlization moved to rich
Value types
r4654 by cxl on Mar 3, 2012   Diff
Core: Jsonize finished
r4651 by cxl on Mar 3, 2012   Diff
Core: Value support for Xmlize,
Jasonize
All revisions of this file

File info

Size: 3356 bytes, 187 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting