-
Notifications
You must be signed in to change notification settings - Fork 8
/
DrawCube.cpp
206 lines (173 loc) · 5.49 KB
/
DrawCube.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "stdafx.h"
#include "drawcube.h"
#define COLOR_WHITE RGB(255,255,255)
#define COLOR_BLACK RGB(0,0,0)
#define NCOLORSHADES 128 // this many shades in gradient
static void PaintRect(CDC* pDC, int x, int y, int w, int h, COLORREF color)
{
CBrush brush(color);
CBrush* pOldBrush = pDC->SelectObject(&brush);
pDC->PatBlt(x, y, w, h, PATCOPY);
pDC->SelectObject(pOldBrush);
brush.DeleteObject();
}
void PaintGradiantRect(CDC *pDC, const RECT &rect,COLORREF clrFrom, COLORREF clrTo =RGB(255,255,255),
BOOL hori = true, BOOL ascend = true)
{
int cxCap = rect.right - rect.left;
int cyCap = rect.bottom - rect.top;
// Get the intensity values for the ending color
int r1 = GetRValue(clrTo); // red
int g1 = GetGValue(clrTo); // green
int b1 = GetBValue(clrTo); // blue
// Get the intensity values for the begining color
int r2 = GetRValue(clrFrom); // red
int g2 = GetGValue(clrFrom); // green
int b2 = GetBValue(clrFrom); // blue
int r, g, b;
if(hori) //paint horizontal rect;
{
int x = cxCap;
int w = x; // width of area to shade
int xDelta= max(w/NCOLORSHADES,1); // width of one shade band
while (x >= xDelta) {
x -= xDelta;
if (r1 > r2)
r = r1 - (r1-r2)*(w-x)/w;
else
r = r1 + (r2-r1)*(w-x)/w;
if (g1 > g2)
g = g1 - (g1-g2)*(w-x)/w;
else
g = g1 + (g2-g1)*(w-x)/w;
if (b1 > b2)
b = b1 - (b1-b2)*(w-x)/w;
else
b = b1 + (b2-b1)*(w-x)/w;
if(ascend) // Paint from left to right;
PaintRect(pDC, rect.left+x, rect.top, xDelta, cyCap, RGB(r, g, b));
else // Paint from right to left;
PaintRect(pDC, rect.right-x-xDelta, rect.top, xDelta, cyCap, RGB(r, g, b));
}
}
else //paint vertical rect;
{
int y = cyCap;
int w = y; // height of area to shade
int yDelta= max(w/NCOLORSHADES,1); // height of one shade band
//while (y >= yDelta) {
while (y > 0) {
y -= yDelta;
if (r1 > r2)
r = r1 - (r1-r2)*(w-y)/w;
else
r = r1 + (r2-r1)*(w-y)/w;
if (g1 > g2)
g = g1 - (g1-g2)*(w-y)/w;
else
g = g1 + (g2-g1)*(w-y)/w;
if (b1 > b2)
b = b1 - (b1-b2)*(w-y)/w;
else
b = b1 + (b2-b1)*(w-y)/w;
if(ascend) // Paint from top to bottom;
PaintRect(pDC, rect.left, rect.top+y, cxCap, yDelta, RGB(r, g, b));
else // Paint from bottom to top;
PaintRect(pDC, rect.left, rect.bottom-y-yDelta, cxCap, yDelta, RGB(r, g, b));
}
}
}
void DrawColorCube(CDC* pDC, CRect& rect, COLORREF colorFrom, COLORREF colorTo /*= RGB(255,255,255)*/,
BOOL IsHori /*= true*/)
{
CRect newRect;
COLORREF color;
CPen* oldPen;
//CBrush* oldBr;
/*
* If value as 0, draw a line here;
*/
if(rect.Width()<=0 )
rect.right = rect.left +2;
else if(rect.Height() <=0)
rect.top-=2;
BOOL backHori = IsHori;
IsHori = true;
/*
* draw first 1/3 part;
*/
if(IsHori)
{
newRect.SetRect(rect.left,rect.top,(rect.right - rect.left)/3+rect.left,rect.bottom);
PaintGradiantRect(pDC,newRect,colorFrom, colorTo);
}
else
{
newRect.SetRect(rect.left,rect.bottom - (rect.bottom-rect.top)/3,rect.right,rect.bottom);
PaintGradiantRect(pDC,newRect,colorFrom, colorTo, IsHori, false);
}
/*
* then left 2/3 part;
*/
if(IsHori)
{
newRect.SetRect((rect.right - rect.left)/3+rect.left,rect.top,rect.right,rect.bottom);
PaintGradiantRect(pDC,newRect,colorFrom, colorTo, IsHori, false);
}
else
{
newRect.SetRect(rect.left,rect.top, rect.right, rect.bottom - (rect.bottom-rect.top)/3);
PaintGradiantRect(pDC,newRect,colorFrom, colorTo, IsHori, true);
}
// /*
// * Frame the rect with colorFrom, with look better;
// */
// CPen pen(PS_SOLID,1,colorFrom);
// oldPen = (CPen*)pDC->SelectObject(&pen);
// oldBr = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
// //pDC->Rectangle(&rect);
// pDC->SelectObject(oldPen);
// pDC->SelectObject(oldBr);
//pen.DeleteObject();
/*
* Draw Shadow next;
*/
if(backHori)//hori
{
color = RGB(170,170,170);
CPen pen1(PS_SOLID,2,color);
oldPen = (CPen*)pDC->SelectObject(&pen1);
pDC->MoveTo(rect.left, rect.bottom+1);
pDC->LineTo(rect.right+1, rect.bottom+1);
pDC->LineTo(rect.right+1, rect.top);
color = RGB(190,190,190);
CPen pen2(PS_SOLID,2,color);
pDC->SelectObject(&pen2);
pDC->MoveTo(rect.left+1, rect.bottom+2);
pDC->LineTo(rect.right+2, rect.bottom+2);
pDC->LineTo(rect.right+2, rect.top+1);
pDC->SelectObject(oldPen);
pen1.DeleteObject();
pen2.DeleteObject();
}
else//vert
{
#if 0
color = RGB(170,170,170);
CPen pen1(PS_SOLID,1,color);
oldPen = (CPen*)pDC->SelectObject(&pen1);
pDC->MoveTo(rect.left, rect.top-2);
pDC->LineTo(rect.right+1, rect.top-2);
pDC->LineTo(rect.right+1, rect.bottom);
color = RGB(190,190,190);
CPen pen2(PS_SOLID,1,color);
pDC->SelectObject(&pen2);
pDC->MoveTo(rect.left+1, rect.top-3);
pDC->LineTo(rect.right+1, rect.top-3);
pDC->LineTo(rect.right+1, rect.bottom-1);
pDC->SelectObject(oldPen);
pen1.DeleteObject();
pen2.DeleteObject();
#endif
}
}