Skip to content

Commit a9985df

Browse files
author
Gary Huang
committed
Add default constructor to color and change color representation
1 parent b55eedd commit a9985df

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

src/Processing/processing.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,25 @@ void noStroke()
236236

237237
float alpha(color rgb)
238238
{
239-
return (rgb.toInt() & 0xFF);
239+
return rgb.data.alpha;
240240
}
241241

242242
float blue(color rgb)
243243
{
244-
return ((rgb.toInt() >> 8) & 0xFF);
244+
return rgb.data.v3;
245245
}
246246

247247
float brightness(color rgb)
248248
{
249-
return ((rgb.toInt() >> 8) & 0xFF);
250-
}
251-
252-
static int color_to_data(int v1, int v2, int v3, int alpha)
253-
{
254-
return ((v1 & 0xFF) << 24) | ((v2 & 0xFF) << 16) | ((v3 & 0xFF) << 8) | (alpha & 0xFF);
249+
return rgb.data.v3;
255250
}
256251

257252
color::color(int v1, int v2, int v3, int alpha)
258253
{
259-
data = color_to_data(v1, v2, v3, alpha);
254+
data.v1 = v1;
255+
data.v2 = v2;
256+
data.v3 = v3;
257+
data.alpha = alpha;
260258
}
261259

262260
static bool is_digit(char c)
@@ -293,39 +291,42 @@ color::color(const char *hex)
293291
if (tmp[i] != '\0')
294292
throw "bad hex notation";
295293

296-
int r, g, b, a = 0xFF;
294+
int v1, v2, v3, alpha = 0xFF;
297295
switch (i)
298296
{
299297
case 3:
300-
r = (d[0] << 4) | d[0];
301-
g = (d[1] << 4) | d[1];
302-
b = (d[2] << 4) | d[2];
298+
v1 = (d[0] << 4) | d[0];
299+
v2 = (d[1] << 4) | d[1];
300+
v3 = (d[2] << 4) | d[2];
303301
break;
304302
case 6:
305-
r = (d[0] << 4) | d[1];
306-
g = (d[2] << 4) | d[3];
307-
b = (d[4] << 4) | d[5];
303+
v1 = (d[0] << 4) | d[1];
304+
v2 = (d[2] << 4) | d[3];
305+
v3 = (d[4] << 4) | d[5];
308306
break;
309307
case 8:
310-
r = (d[0] << 4) | d[1];
311-
g = (d[2] << 4) | d[3];
312-
b = (d[4] << 4) | d[5];
313-
a = (d[6] << 4) | d[7];
308+
v1 = (d[0] << 4) | d[1];
309+
v2 = (d[2] << 4) | d[3];
310+
v3 = (d[4] << 4) | d[5];
311+
alpha = (d[6] << 4) | d[7];
314312
break;
315313
default:
316314
throw "bad hex notation: only support format: #RGB, #RRGGBB, #RRGGBBAA";
317315
}
318-
data = color_to_data(r, g, b, a);
316+
data.v1 = v1;
317+
data.v2 = v2;
318+
data.v3 = v3;
319+
data.alpha = alpha;
319320
}
320321

321322
float green(color rgb)
322323
{
323-
return ((rgb.toInt() >> 16) & 0xFF);
324+
return rgb.data.v2;
324325
}
325326

326327
float hue(color rgb)
327328
{
328-
return ((rgb.toInt() >> 24) & 0xFF);
329+
return rgb.data.v1;
329330
}
330331

331332
//int lerpColor(int c1, int c2, float amt)
@@ -334,12 +335,12 @@ float hue(color rgb)
334335

335336
float red(color rgb)
336337
{
337-
return ((rgb.toInt() >> 24) & 0xFF);
338+
return rgb.data.v1;
338339
}
339340

340341
float saturation(color rgb)
341342
{
342-
return ((rgb.toInt() >> 16) & 0xFF);
343+
return rgb.data.v2;
343344
}
344345

345346
void ellipseMode(DrawMode mode)

src/Processing/processing.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,35 @@ PROCESSING_BEGIN_NAMESPACE
2121

2222
typedef char byte;
2323

24+
typedef struct color_data_t
25+
{
26+
unsigned alpha : 8,
27+
v1 : 8,
28+
v2 : 8,
29+
v3 : 8;
30+
}
31+
color_t;
32+
2433
class color
2534
{
2635
public:
36+
color() : color(0) {}
2737
color(int gray) : color(gray, gray, gray, 0xFF) {}
2838
color(int gray, int alpha) : color(gray, gray, gray, alpha) {}
2939
color(int v1, int v2, int v3) : color(v1, v2, v3, 0xFF) {}
3040
color(int v1, int v2, int v3, int alpha);
3141
color(const char *hex);
32-
int toInt() { return data; }
3342

3443
private:
35-
int data;
44+
friend float red(color);
45+
friend float green(color);
46+
friend float blue(color);
47+
friend float hue(color);
48+
friend float saturation(color);
49+
friend float brightness(color);
50+
friend float alpha(color);
51+
52+
color_t data;
3653
};
3754

3855
class Processing

0 commit comments

Comments
 (0)