-
Notifications
You must be signed in to change notification settings - Fork 3
/
UIColorPalette.cpp
409 lines (340 loc) · 11.2 KB
/
UIColorPalette.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "StdAfx.h"
#include <math.h>
namespace DuiLib {
#define HSLMAX 255 /* H,L, and S vary over 0-HSLMAX */
#define RGBMAX 255 /* R,G, and B vary over 0-RGBMAX */
#define HSLUNDEFINED (HSLMAX*2/3)
/*
* Convert hue value to RGB
*/
static float HueToRGB(float v1, float v2, float vH)
{
if (vH < 0.0f) vH += 1.0f;
if (vH > 1.0f) vH -= 1.0f;
if ((6.0f * vH) < 1.0f) return (v1 + (v2 - v1) * 6.0f * vH);
if ((2.0f * vH) < 1.0f) return (v2);
if ((3.0f * vH) < 2.0f) return (v1 + (v2 - v1) * ((2.0f / 3.0f) - vH) * 6.0f);
return (v1);
}
/*
* Convert color RGB to HSL
* pHue HSL hue value [0 - 1]
* pSat HSL saturation value [0 - 1]
* pLue HSL luminance value [0 - 1]
*/
static void RGBToHSL(DWORD clr, float *pHue, float *pSat, float *pLue)
{
float R = (float)(GetRValue(clr) / 255.0f); //RGB from 0 to 255
float G = (float)(GetGValue(clr) / 255.0f);
float B = (float)(GetBValue(clr) / 255.0f);
float H, S, L;
float fMin = min(R, min(G, B)); //Min. value of RGB
float fMax = max(R, max(G, B)); //Max. value of RGB
float fDelta = fMax - fMin; //Delta RGB value
L = (fMax + fMin) / 2.0f;
if (fDelta == 0) //This is a gray, no chroma...
{
H = 0.0f; //HSL results from 0 to 1
S = 0.0f;
}
else //Chromatic data...
{
float del_R, del_G, del_B;
if (L < 0.5) S = fDelta / (fMax + fMin);
else S = fDelta / (2.0f - fMax - fMin);
del_R = (((fMax - R) / 6.0f) + (fDelta / 2.0f)) / fDelta;
del_G = (((fMax - G) / 6.0f) + (fDelta / 2.0f)) / fDelta;
del_B = (((fMax - B) / 6.0f) + (fDelta / 2.0f)) / fDelta;
if (R == fMax) H = del_B - del_G;
else if (G == fMax) H = (1.0f / 3.0f) + del_R - del_B;
else if (B == fMax) H = (2.0f / 3.0f) + del_G - del_R;
if (H < 0.0f) H += 1.0f;
if (H > 1.0f) H -= 1.0f;
}
*pHue = H;
*pSat = S;
*pLue = L;
}
/*
* Convert color HSL to RGB
* H HSL hue value [0 - 1]
* S HSL saturation value [0 - 1]
* L HSL luminance value [0 - 1]
*/
static DWORD HSLToRGB(float H, float S, float L)
{
BYTE R, G, B;
float var_1, var_2;
if (S == 0) //HSL from 0 to 1
{
R = G = B = (BYTE)(L * 255.0f); //RGB results from 0 to 255
}
else
{
if (L < 0.5) var_2 = L * (1.0f + S);
else var_2 = (L + S) - (S * L);
var_1 = 2.0f * L - var_2;
R = (BYTE)(255.0f * HueToRGB(var_1, var_2, H + (1.0f / 3.0f)));
G = (BYTE)(255.0f * HueToRGB(var_1, var_2, H));
B = (BYTE)(255.0f * HueToRGB(var_1, var_2, H - (1.0f / 3.0f)));
}
return RGB(R, G, B);
}
/*
* _HSLToRGB color HSL value to RGB
* clr RGB color value
* nHue HSL hue value [0 - 360]
* nSat HSL saturation value [0 - 200]
* nLue HSL luminance value [0 - 200]
*/
#define _HSLToRGB(h,s,l) (0xFF << 24 | HSLToRGB((float)h / 360.0f,(float)s / 200.0f,l / 200.0f))
///////////////////////////////////////////////////////////////////////
//
//
IMPLEMENT_DUICONTROL(CColorPaletteUI)
CColorPaletteUI::CColorPaletteUI()
: m_uButtonState(0)
, m_bIsInBar(false)
, m_bIsInPallet(false)
, m_nCurH(180)
, m_nCurS(200)
, m_nCurB(100)
, m_nPalletHeight(200)
, m_nBarHeight(10)
, m_pBits(NULL)
{
memset(&m_bmInfo, 0, sizeof(m_bmInfo));
m_hMemBitmap=NULL;
}
CColorPaletteUI::~CColorPaletteUI()
{
if (m_pBits) free(m_pBits);
if (m_hMemBitmap) {
::DeleteObject(m_hMemBitmap);
}
}
DWORD CColorPaletteUI::GetSelectColor()
{
DWORD dwColor = _HSLToRGB(m_nCurH, m_nCurS, m_nCurB);
return 0xFF << 24 | GetRValue(dwColor) << 16 | GetGValue(dwColor) << 8 | GetBValue(dwColor);
}
void CColorPaletteUI::SetSelectColor(DWORD dwColor)
{
float H = 0, S = 0, B = 0;
COLORREF dwBkClr = RGB(GetBValue(dwColor),GetGValue(dwColor),GetRValue(dwColor));
RGBToHSL(dwBkClr, &H, &S, &B);
m_nCurH = (int)(H*360);
m_nCurS = (int)(S*200);
m_nCurB = (int)(B*200);
NeedUpdate();
UpdatePalletData();
}
LPCTSTR CColorPaletteUI::GetClass() const
{
return _T("ColorPaletteUI");
}
LPVOID CColorPaletteUI::GetInterface(LPCTSTR pstrName)
{
if (_tcscmp(pstrName, DUI_CTR_COLORPALETTE) == 0) return static_cast<CColorPaletteUI*>(this);
return CControlUI::GetInterface(pstrName);
}
void CColorPaletteUI::SetPalletHeight(int nHeight)
{
m_nPalletHeight = nHeight;
}
int CColorPaletteUI::GetPalletHeight() const
{
return m_nPalletHeight;
}
void CColorPaletteUI::SetBarHeight(int nHeight)
{
if (nHeight>150) {
nHeight = 150; //限制最大高度,由于当前设计,nheight超出190,程序会因越界访问崩溃
}
m_nBarHeight = nHeight;
}
int CColorPaletteUI::GetBarHeight() const
{
return m_nBarHeight;
}
void CColorPaletteUI::SetThumbImage(LPCTSTR pszImage)
{
if (m_strThumbImage != pszImage)
{
m_strThumbImage = pszImage;
NeedUpdate();
}
}
LPCTSTR CColorPaletteUI::GetThumbImage() const
{
return m_strThumbImage.GetData();
}
void CColorPaletteUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
if (_tcscmp(pstrName, _T("palletheight")) == 0) SetPalletHeight(_ttoi(pstrValue));
else if (_tcscmp(pstrName, _T("barheight")) == 0) SetBarHeight(_ttoi(pstrValue));
else if (_tcscmp(pstrName, _T("thumbimage")) == 0) SetThumbImage(pstrValue);
else CControlUI::SetAttribute(pstrName, pstrValue);
}
void CColorPaletteUI::DoInit()
{
m_MemDc = CreateCompatibleDC(GetManager()->GetPaintDC());
m_hMemBitmap = CreateCompatibleBitmap(GetManager()->GetPaintDC(), 400, 360);
HBITMAP pOldBit = (HBITMAP)SelectObject(m_MemDc, m_hMemBitmap);
::GetObject(m_hMemBitmap, sizeof(m_bmInfo), &m_bmInfo);
DWORD dwSize = m_bmInfo.bmHeight * m_bmInfo.bmWidthBytes;
m_pBits = (BYTE *)malloc(dwSize);
::GetBitmapBits(m_hMemBitmap, dwSize, m_pBits);
}
void CColorPaletteUI::SetPos(RECT rc)
{
CControlUI::SetPos(rc);
m_ptLastPalletMouse.x = m_nCurH * (m_rcItem.right - m_rcItem.left) / 360 + m_rcItem.left;
m_ptLastPalletMouse.y = (200 - m_nCurB) * m_nPalletHeight / 200 + m_rcItem.top;
UpdatePalletData();
UpdateBarData();
}
void CColorPaletteUI::DoEvent(TEventUI& event)
{
CControlUI::DoEvent(event);
if (event.Type == UIEVENT_BUTTONDOWN)
{
if (event.ptMouse.x >= m_rcItem.left && event.ptMouse.y >= m_rcItem.top &&
event.ptMouse.x < m_rcItem.right && event.ptMouse.y < m_rcItem.top + m_nPalletHeight)
{
int x = (event.ptMouse.x - m_rcItem.left) * 360 / (m_rcItem.right - m_rcItem.left);
int y = (event.ptMouse.y - m_rcItem.top) * 200 / m_nPalletHeight;
x = min(max(x, 0), 360);
y = min(max(y, 0), 200);
m_ptLastPalletMouse = event.ptMouse;
if (m_ptLastPalletMouse.x < m_rcItem.left) m_ptLastPalletMouse.x = m_rcItem.left;
if (m_ptLastPalletMouse.x > m_rcItem.right) m_ptLastPalletMouse.x = m_rcItem.right;
if (m_ptLastPalletMouse.y < m_rcItem.top) m_ptLastPalletMouse.y = m_rcItem.top;
if (m_ptLastPalletMouse.y > m_rcItem.top + m_nPalletHeight) m_ptLastPalletMouse.y = m_rcItem.top + m_nPalletHeight;
m_nCurH = x;
m_nCurB = 200 - y;
m_uButtonState |= UISTATE_PUSHED;
m_bIsInPallet = true;
m_bIsInBar = false;
UpdateBarData();
}
//::PtInRect(&m_rcItem, event.ptMouse)
if (event.ptMouse.x >= m_rcItem.left && event.ptMouse.y >= m_rcItem.bottom - m_nBarHeight &&
event.ptMouse.x < m_rcItem.right && event.ptMouse.y < m_rcItem.bottom)
{
m_nCurS = (event.ptMouse.x - m_rcItem.left) * 200 / (m_rcItem.right - m_rcItem.left);
m_uButtonState |= UISTATE_PUSHED;
m_bIsInBar = true;
m_bIsInPallet = false;
UpdatePalletData();
}
Invalidate();
return;
}
if (event.Type == UIEVENT_BUTTONUP)
{
DWORD color=0;
if ((m_uButtonState | UISTATE_PUSHED) && (IsEnabled()))
{
color = GetSelectColor();
m_pManager->SendNotify(this, DUI_MSGTYPE_COLORCHANGED, color, 0);
}
m_uButtonState &= ~UISTATE_PUSHED;
m_bIsInPallet = false;
m_bIsInBar = false;
Invalidate();
return;
}
if (event.Type == UIEVENT_MOUSEMOVE)
{
if (!(m_uButtonState &UISTATE_PUSHED))
{
m_bIsInBar = false;
m_bIsInPallet = false;
}
if (m_bIsInPallet == true)
{
POINT pt = event.ptMouse;
pt.x -= m_rcItem.left;
pt.y -= m_rcItem.top;
if (pt.x >= 0 && pt.y >= 0 && pt.x <= m_rcItem.right && pt.y <= m_rcItem.top + m_nPalletHeight)
{
int x = pt.x * 360 / (m_rcItem.right - m_rcItem.left);
int y = pt.y * 200 / m_nPalletHeight;
x = min(max(x, 0), 360);
y = min(max(y, 0), 200);
m_ptLastPalletMouse = event.ptMouse;
if (m_ptLastPalletMouse.x < m_rcItem.left) m_ptLastPalletMouse.x = m_rcItem.left;
if (m_ptLastPalletMouse.x > m_rcItem.right) m_ptLastPalletMouse.x = m_rcItem.right;
if (m_ptLastPalletMouse.y < m_rcItem.top) m_ptLastPalletMouse.y = m_rcItem.top;
if (m_ptLastPalletMouse.y > m_rcItem.top + m_nPalletHeight) m_ptLastPalletMouse.y = m_rcItem.top + m_nPalletHeight;
m_nCurH = x;
m_nCurB = 200 - y;
UpdateBarData();
}
}
if (m_bIsInBar == true)
{
m_nCurS = (event.ptMouse.x - m_rcItem.left) * 200 / (m_rcItem.right - m_rcItem.left);
m_nCurS = min(max(m_nCurS, 0), 200);
UpdatePalletData();
}
Invalidate();
return;
}
}
void CColorPaletteUI::PaintBkColor(HDC hDC)
{
PaintPallet(hDC);
}
void CColorPaletteUI::PaintPallet(HDC hDC)
{
int nSaveDC = ::SaveDC(hDC);
::SetStretchBltMode(hDC, HALFTONE);
//拉伸模式将内存图画到控件上
StretchBlt(hDC, m_rcItem.left, m_rcItem.top, m_rcItem.right - m_rcItem.left, m_nPalletHeight, m_MemDc, 0, 0, 360, 199, SRCCOPY);
StretchBlt(hDC, m_rcItem.left, m_rcItem.bottom - m_nBarHeight, m_rcItem.right - m_rcItem.left, m_nBarHeight, m_MemDc, 0, 210, 200, m_nBarHeight, SRCCOPY);
RECT rcCurSorPaint = { m_ptLastPalletMouse.x - 4, m_ptLastPalletMouse.y - 4, m_ptLastPalletMouse.x + 4, m_ptLastPalletMouse.y + 4 };
CRenderEngine::DrawImageString(hDC, m_pManager, rcCurSorPaint, m_rcPaint, m_strThumbImage);
rcCurSorPaint.left = m_rcItem.left + m_nCurS * (m_rcItem.right - m_rcItem.left) / 200 - 4;
rcCurSorPaint.right = m_rcItem.left + m_nCurS * (m_rcItem.right - m_rcItem.left) / 200 + 4;
rcCurSorPaint.top = m_rcItem.bottom - m_nBarHeight / 2 - 4;
rcCurSorPaint.bottom = m_rcItem.bottom - m_nBarHeight / 2 + 4;
CRenderEngine::DrawImageString(hDC, m_pManager, rcCurSorPaint, m_rcPaint, m_strThumbImage);
::RestoreDC(hDC, nSaveDC);
}
void CColorPaletteUI::UpdatePalletData()
{
int x, y;
BYTE *pPiexl;
DWORD dwColor;
for (y = 0; y < 200; ++y) {
for (x = 0; x < 360; ++x) {
pPiexl = LPBYTE(m_pBits) + ((199 - y)*m_bmInfo.bmWidthBytes) + ((x*m_bmInfo.bmBitsPixel) / 8);
dwColor = _HSLToRGB(x, m_nCurS, y);
pPiexl[0] = GetBValue(dwColor);
pPiexl[1] = GetGValue(dwColor);
pPiexl[2] = GetRValue(dwColor);
}
}
SetBitmapBits(m_hMemBitmap, m_bmInfo.bmWidthBytes * m_bmInfo.bmHeight, m_pBits);
}
void CColorPaletteUI::UpdateBarData()
{
int x, y;
BYTE *pPiexl;
DWORD dwColor;
//这里画出Bar
for (y = 0; y < m_nBarHeight; ++y) {
for (x = 0; x < 200; ++x) {
pPiexl = LPBYTE(m_pBits) + ((210 + y)*m_bmInfo.bmWidthBytes) + ((x*m_bmInfo.bmBitsPixel) / 8);
//*(DWORD*)pPiexl = _HSLToRGB(m_nCurH, x , m_nCurB);
dwColor = _HSLToRGB(m_nCurH, x, m_nCurB);
pPiexl[0] = GetBValue(dwColor);
pPiexl[1] = GetGValue(dwColor);
pPiexl[2] = GetRValue(dwColor);
}
}
SetBitmapBits(m_hMemBitmap, m_bmInfo.bmWidthBytes * m_bmInfo.bmHeight, m_pBits);
}
}