-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditColors.cpp
297 lines (243 loc) · 7.06 KB
/
EditColors.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
// EditColors.cpp : implementation file
//
#include "stdafx.h"
#include "Fractal.h"
#include "EditColors.h"
#include "BitmapIcon.h"
// CEditColors dialog
IMPLEMENT_DYNAMIC(CEditColors, CDialog)
CEditColors::CEditColors(CWnd * pParent)
: CDialog(CEditColors::IDD, pParent), m_nColors(0), m_bChange(false)
{
}
CEditColors::~CEditColors()
{
}
void CEditColors::SetColors(int nColors)
{
if (m_nColors != nColors)
m_bChange = true;
m_nColors = nColors;
}
void CEditColors::SetRGB(COLORREF RGB1,COLORREF RGB2,BYTE RR,BYTE GG,BYTE BB,BOOL bR,BOOL bG,BOOL bB)
{
if (m_vecColors.size() != 2)
m_bChange = true;
m_vecColors.resize(2);
RGB1 = RGB(bR ? GetRValue(RGB1) : 0,bG ? GetGValue(RGB1) : 0,bB ? GetBValue(RGB1) : 0);
RGB2 = RGB(bR ? GetRValue(RGB2) : 0,bG ? GetGValue(RGB2) : 0,bB ? GetBValue(RGB2) : 0);
RR = bR ? RR : 0;
GG = bG ? GG : 0;
BB = bB ? BB : 0;
if (m_RR != RR || m_GG != GG || m_BB != BB ||
m_bR != bR || m_bG != bG || m_bB != bB ||
RGB1 != m_vecColors[0] || RGB2 != m_vecColors[1])
m_bChange = true;
m_RR = RR;
m_GG = GG;
m_BB = BB;
m_bR = bR;
m_bG = bG;
m_bB = bB;
m_vecColors[0] = RGB1;
m_vecColors[1] = RGB2;
if (m_bChange)
{
m_RGB.clear();
m_bChange = false;
}
}
void CEditColors::SetRGB(COLORREF RGB1,COLORREF RGB2,COLORREF RGB3,COLORREF RGB4,COLORREF RGB5,COLORREF RGB6,BYTE RR,BYTE GG,BYTE BB,BOOL bR,BOOL bG,BOOL bB)
{
if (m_vecColors.size() != 6)
m_bChange = true;
m_vecColors.resize(6);
RGB1 = RGB(bR ? GetRValue(RGB1) : 0,bG ? GetGValue(RGB1) : 0,bB ? GetBValue(RGB1) : 0);
RGB2 = RGB(bR ? GetRValue(RGB2) : 0,bG ? GetGValue(RGB2) : 0,bB ? GetBValue(RGB2) : 0);
RGB3 = RGB(bR ? GetRValue(RGB3) : 0,bG ? GetGValue(RGB3) : 0,bB ? GetBValue(RGB3) : 0);
RGB4 = RGB(bR ? GetRValue(RGB4) : 0,bG ? GetGValue(RGB4) : 0,bB ? GetBValue(RGB4) : 0);
RGB5 = RGB(bR ? GetRValue(RGB5) : 0,bG ? GetGValue(RGB5) : 0,bB ? GetBValue(RGB5) : 0);
RGB6 = RGB(bR ? GetRValue(RGB6) : 0,bG ? GetGValue(RGB6) : 0,bB ? GetBValue(RGB6) : 0);
RR = bR ? RR : 0;
GG = bG ? GG : 0;
BB = bB ? BB : 0;
if (m_RR != RR || m_GG != GG || m_BB != BB ||
m_bR != bR || m_bG != bG || m_bB != bB ||
RGB1 != m_vecColors[0] || RGB2 != m_vecColors[1] || RGB3 != m_vecColors[2] ||
RGB4 != m_vecColors[3] || RGB5 != m_vecColors[4] || RGB6 != m_vecColors[5])
m_bChange = true;
m_RR = RR;
m_GG = GG;
m_BB = BB;
m_bR = bR;
m_bG = bG;
m_bB = bB;
m_vecColors[0] = RGB1;
m_vecColors[1] = RGB2;
m_vecColors[2] = RGB3;
m_vecColors[3] = RGB4;
m_vecColors[4] = RGB5;
m_vecColors[5] = RGB6;
if (m_bChange)
{
m_RGB.clear();
m_bChange = false;
}
}
void CEditColors::UpdateColor(std::vector<std::vector<BYTE> > vecRGB)
{
m_RGB = vecRGB;
}
void CEditColors::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX,IDC_LIST_COLORS,m_Colors);
}
// Make the Bitmap and color text
HBITMAP CEditColors::MakeColor(BYTE R,BYTE G,BYTE B,CString & csColor)
{
// Get the color value
COLORREF crRGB = RGB(R,G,B);
// Create a DIB of the color
CDIBFrame Image(32,32);
CDC * pImageDC = (CDC *)Image;
pImageDC->FillSolidRect(1,1,30,30,crRGB);
// Convert to a DDB
HBITMAP hBitmap = Image.ToDDB();
// Get the RGB text
csColor.Format(L"RGB(%d,%d,%d)",R,G,B);
return hBitmap;
}
// Build the color map
void CEditColors::BuildColorMap()
{
// Build the palette
m_Gradient.Init(m_nColors,32);
m_Gradient.GradientFill(&m_vecColors[0],(int)m_vecColors.size(),m_RR,m_GG,m_BB);
// Prepare the color map
m_RGB.resize(3);
m_RGB[0].resize(m_nColors + 1,0);
m_RGB[1].resize(m_nColors + 1,0);
m_RGB[2].resize(m_nColors + 1,0);
// Create the icons for the image list
for (int iColor = 0;iColor < m_nColors;++iColor)
{
// Get the color
BYTE R,G,B;
m_Gradient.GetPixel(iColor,16,R,G,B);
// Set the color in the color map
m_RGB[0][iColor + 1] = R;
m_RGB[1][iColor + 1] = G;
m_RGB[2][iColor + 1] = B;
}
// Make sure the last color is the last color in the range (makes a big difference for binary palette)
m_RGB[0][m_nColors] = GetRValue(m_vecColors[m_vecColors.size() - 1]);
m_RGB[1][m_nColors] = GetGValue(m_vecColors[m_vecColors.size() - 1]);
m_RGB[2][m_nColors] = GetBValue(m_vecColors[m_vecColors.size() - 1]);
}
// Build the image list and text from the color map
void CEditColors::BuildImageList()
{
// Create the image list
if (m_ImageList.GetSafeHandle())
m_ImageList.DeleteImageList();
m_ImageList.Create(32,32,ILC_COLORDDB,m_nColors,m_nColors);
// Create the icons for the image list
for (int iColor = 0;iColor < m_nColors;++iColor)
{
// Get the color
BYTE R,G,B;
R = m_RGB[0][iColor + 1];
G = m_RGB[1][iColor + 1];
B = m_RGB[2][iColor + 1];
// Get the bitmap and text for the color
CString csColor;
CBitmap Bitmap;
Bitmap.Attach(MakeColor(R,G,B,csColor));
// Add to the image list
m_ImageList.Add(&Bitmap,&Bitmap);
// Add the item, the text, and index to the image list
m_Colors.InsertItem(iColor,csColor,iColor);
}
// Set the image list to the icon view
m_Colors.SetImageList(&m_ImageList,LVSIL_NORMAL);
}
// Get and optionally build the color map
std::vector<std::vector<BYTE> > CEditColors::GetColorMap()
{
if (m_RGB.empty())
BuildColorMap();
return m_RGB;
}
BOOL CEditColors::OnInitDialog()
{
BOOL bRes = CDialog::OnInitDialog();
if (m_nColors)
{
if (m_RGB.empty())
{
// Build the color map
BuildColorMap();
}
// Build the image list
BuildImageList();
}
else
EndDialog(IDCANCEL);
return bRes;
}
BEGIN_MESSAGE_MAP(CEditColors, CDialog)
ON_NOTIFY(NM_DBLCLK, IDC_LIST_COLORS, &CEditColors::OnNMDblclkListColors)
END_MESSAGE_MAP()
// CEditColors message handlers
void CEditColors::OnNMDblclkListColors(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
int iItem = pNMItemActivate->iItem;
if (iItem != -1)
{
// Get the color
BYTE R,G,B;
R = m_RGB[0][iItem + 1];
G = m_RGB[1][iItem + 1];
B = m_RGB[2][iItem + 1];
// Edit the color
CColorDialog ClrDlg;
ClrDlg.m_cc.Flags |= CC_FULLOPEN | CC_RGBINIT;
COLORREF crRGB = RGB(R,G,B);
ClrDlg.m_cc.rgbResult = crRGB;
if (ClrDlg.DoModal() == IDOK)
{
// Get the new color
crRGB = ClrDlg.GetColor();
// Get the color components
R = GetRValue(crRGB);
G = GetGValue(crRGB);
B = GetBValue(crRGB);
// Set the color in the color map (channel filtering happens later for the map)
m_RGB[0][iItem + 1] = R;
m_RGB[1][iItem + 1] = G;
m_RGB[2][iItem + 1] = B;
// Check for color channel filtering
if (R & !m_bR)
AfxMessageBox(L"Red channel will be removed do to red channel settings");
if (G & !m_bG)
AfxMessageBox(L"Green channel will be removed do to green channel settings");
if (B & !m_bB)
AfxMessageBox(L"Blue channel will be removed do to blue channel settings");
// Set the color values that make the color
R = m_bR ? R : 0;
G = m_bG ? G : 0;
B = m_bB ? B : 0;
// Get the bitmap and text
CString csColor;
CBitmap Bitmap;
Bitmap.Attach(MakeColor(R,G,B,csColor));
// Update the image list
m_ImageList.Replace(iItem,&Bitmap,&Bitmap);
// Update the text
m_Colors.SetItemText(iItem,0,csColor);
}
}
*pResult = 0;
}