1 // color_edit_dialog.cpp : implementation file
2 //
3 
4 #include "stdafx.h"
5 #include "color_edit_dialog.h"
6 #include "afxdialogex.h"
7 
8 #define SWATCH_SIZE 4
9 #define PALETTE_BOX_SIZE 14
10 
11 enum color_edit_dlg_test_commands{
12     TEST_SET_COLOR_NAME = 1,
13     TEST_SET_COLOR_RED,
14     TEST_SET_COLOR_GREEN,
15     TEST_SET_COLOR_BLUE,
16     TEST_SET_COLOR_HUE,
17     TEST_SET_COLOR_SAT,
18     TEST_SET_COLOR_LUM,
19     TEST_SET_PALETTE_COLOR_INDEX,
20     TEST_SAVE_COLOR_EDIT,
21     TEST_CANCEL_COLOR_EDIT
22 };
23 
24 extern CString target_class_name;
25 
BEGIN_MESSAGE_MAP(color_gradient_win,CWnd)26 BEGIN_MESSAGE_MAP(color_gradient_win, CWnd)
27     ON_WM_PAINT()
28     ON_WM_LBUTTONDOWN()
29     ON_WM_SETFOCUS()
30     ON_WM_KILLFOCUS()
31     ON_WM_ERASEBKGND()
32 END_MESSAGE_MAP()
33 
34 color_gradient_win::color_gradient_win(CWnd *pParent)
35 {
36     int dpi = GetSystemDPI();
37 
38     mpParent = (color_edit_dialog *)pParent;
39 
40     if (mpParent->GetPalette()->palette)
41     {
42         m_swatch_size = MulDiv(PALETTE_BOX_SIZE, dpi, DEFAULT_DPI_96);
43     }
44     else
45     {
46         m_swatch_size = MulDiv(SWATCH_SIZE, dpi, DEFAULT_DPI_96);
47     }
48 
49     mpProject = GetOpenProject();
50 }
51 
52 ///////////////////////////////////////////////////////////////////////////////
OnPaint()53 void color_gradient_win::OnPaint()
54 {
55     CWnd::OnPaint();
56 
57     CRect client;
58     GetClientRect(&client);
59 
60     CDC *pDC = GetDC();
61 
62     /* Create off-screen DC for drawing. */
63     CDC        dcMem;
64     HBITMAP    hbmMem;
65     HANDLE     hOld;
66 
67     dcMem.CreateCompatibleDC(pDC);
68     hbmMem = CreateCompatibleBitmap(pDC->GetSafeHdc(), client.Width(), client.Height());
69     hOld = SelectObject(dcMem, hbmMem);
70 
71     dcMem.FillSolidRect(client, GetSysColor(COLOR_3DFACE));
72 
73     if (mpParent->GetPalette()->palette)
74     {
75         PaintColorPalette(&dcMem);
76         PaintColorPaletteHighlight(&dcMem);
77     }
78     else
79     {
80         PaintColorGradient(&dcMem);
81         PaintColorGradientHighlight(&dcMem);
82     }
83 
84     /* Transfer the off-screen DC to the current DC. */
85     pDC->BitBlt(0, 0, client.Width(), client.Height(), &dcMem, 0, 0, SRCCOPY);
86 
87     /* Free-up the off-screen DC.  */
88     SelectObject(dcMem, hOld);
89     DeleteObject(hbmMem);
90     DeleteDC(dcMem);
91 
92     ReleaseDC(pDC);
93 }
94 
95 ///////////////////////////////////////////////////////////////////////////////
OnEraseBkgnd(CDC * pDC)96 BOOL color_gradient_win::OnEraseBkgnd(CDC* pDC)
97 {
98     return TRUE;
99 }
100 
101 ///////////////////////////////////////////////////////////////////////////////
PaintColorPalette(CDC * pDC)102 void color_gradient_win::PaintColorPalette(CDC *pDC)
103 {
104     CRect boxrect;
105     CRect swatch;
106     int row, column;
107     int index = 0;
108 
109     GetClientRect(boxrect);
110 
111     int rows, columns;
112 
113     GetColorGradientDimension(&rows, &columns);
114 
115     swatch.left = boxrect.left;
116     swatch.top = boxrect.top;
117     swatch.right = swatch.left + m_swatch_size;
118     swatch.bottom = swatch.top + m_swatch_size;
119 
120     palette_info *palinfo = mpParent->GetPalette();
121     GX_COLOR *color = palinfo->palette;
122 
123     for (row = 1; row <= rows; row++)
124     {
125         swatch.left = boxrect.left;
126         swatch.right = swatch.left + m_swatch_size;
127 
128         for (column = 0; column < columns; column++, index++)
129         {
130             if (index >= palinfo->used_size)
131             {
132                return;
133             }
134 
135             COLORREF cr = GxColorToColorRef(*color);
136             color++;
137             CBrush swatchbrush(cr);
138             CPen swatchpen(PS_SOLID, 0, cr);
139             pDC->SelectObject(swatchbrush);
140             pDC->SelectObject(swatchpen);
141             pDC->Rectangle(swatch);
142             swatch.OffsetRect(m_swatch_size, 0);
143         }
144         swatch.OffsetRect(0, m_swatch_size);
145     }
146 }
147 
148 ///////////////////////////////////////////////////////////////////////////////
PaintColorPaletteHighlight(CDC * pDC)149 void color_gradient_win::PaintColorPaletteHighlight(CDC* pDC)
150 {
151     // Calculate selected box
152     CRect selected_box;
153     GetClientRect(selected_box);
154 
155     selected_box.right = selected_box.left + m_swatch_size;
156     selected_box.bottom = selected_box.top + m_swatch_size;
157 
158     int select_row, select_column;
159 
160     GetSelectedPos(&select_row, &select_column);
161 
162     selected_box.OffsetRect(m_swatch_size * select_column, m_swatch_size * select_row);
163 
164     // Highlith selected color box.
165     CPen outline_pen;
166     CPen fill_pen;
167 
168     if (GetFocus() == this)
169     {
170         outline_pen.CreatePen(PS_SOLID, 1, RGB_COLOR_BLACK);
171         fill_pen.CreatePen(PS_SOLID, 1, RGB_COLOR_WHITE);
172     }
173     else
174     {
175         outline_pen.CreatePen(PS_SOLID, 1, RGB_COLOR_BLACK);
176         fill_pen.CreatePen(PS_SOLID, 1, RGB_COLOR_GRAY);
177     }
178 
179     pDC->SelectObject(GetStockObject(NULL_BRUSH));
180     pDC->SelectObject(outline_pen);
181     pDC->Rectangle(selected_box);
182 
183     selected_box.DeflateRect(1, 1);
184     pDC->SelectObject(fill_pen);
185     pDC->Rectangle(selected_box);
186 
187     selected_box.DeflateRect(1, 1);
188     pDC->SelectObject(outline_pen);
189     pDC->Rectangle(selected_box);
190 }
191 
192 ///////////////////////////////////////////////////////////////////////////////
PaintColorGradient(CDC * pDC)193 void color_gradient_win::PaintColorGradient(CDC *pDC)
194 {
195     CRect boxrect;
196     CRect swatch;
197     int row, column;
198 
199     GetClientRect(boxrect);
200 
201     int rows, columns;
202 
203     GetColorGradientDimension(&rows, &columns);
204 
205     swatch.left = boxrect.left;
206     swatch.top = boxrect.top;
207     swatch.right = swatch.left + m_swatch_size;
208     swatch.bottom = swatch.top + m_swatch_size;
209 
210     CBrush tempbrush(RGB(0, 0, 0));
211     CPen temppen(PS_SOLID, 0, RGB(0, 0, 0));
212 
213     int red, green, blue;
214     double Saturation, Hue, Luminance;
215     int lum;
216 
217     mpParent->GetHSLColor(NULL, NULL, &lum);
218     Luminance = lum / 100.0;
219 
220     for (row = 0; row < rows; row++)
221     {
222         Saturation = (double)row / (double)(rows - 1);
223         swatch.left = boxrect.left;
224         swatch.right = swatch.left + m_swatch_size;
225 
226         for (column = 0; column < columns; column++)
227         {
228             Hue = (double)column / (double)(columns - 1);
229             mpParent->HslToRgb(Hue, Saturation, Luminance, red, green, blue);
230             CBrush swatchbrush(RGB(red, green, blue));
231             CPen swatchpen(PS_SOLID, 0, RGB(red, green, blue));
232             pDC->SelectObject(swatchbrush);
233             pDC->SelectObject(swatchpen);
234             pDC->Rectangle(swatch);
235             swatch.OffsetRect(m_swatch_size, 0);
236         }
237         swatch.OffsetRect(0, m_swatch_size);
238     }
239 }
240 
241 ///////////////////////////////////////////////////////////////////////////////
PaintColorGradientHighlight(CDC * pDC)242 void color_gradient_win::PaintColorGradientHighlight(CDC* pDC)
243 {
244 
245     // Calculate selected box
246     int select_row, select_column;
247     GetSelectedPos(&select_row, &select_column);
248 
249     CRect selected_box;
250     GetClientRect(selected_box);
251 
252     selected_box.right = selected_box.left + m_swatch_size;
253     selected_box.bottom = selected_box.top + m_swatch_size;
254 
255     selected_box.OffsetRect(m_swatch_size * select_column, m_swatch_size * select_row);
256 
257     CPen highlight_pen;
258 
259     if (GetFocus() == this)
260     {
261         highlight_pen.CreatePen(PS_SOLID, 0, RGB_COLOR_WHITE);
262     }
263     else
264     {
265         highlight_pen.CreatePen(PS_SOLID, 0, RGB_COLOR_BLACK);
266     }
267 
268     int cx = (selected_box.left + selected_box.right) >> 1;
269     int cy = (selected_box.top + selected_box.bottom) >> 1;
270 
271     // Highlight selected box
272     pDC->SelectObject(&highlight_pen);
273     pDC->MoveTo(selected_box.left - m_swatch_size, cy);
274     pDC->LineTo(selected_box.left, cy);
275     pDC->MoveTo(selected_box.right + m_swatch_size, cy);
276     pDC->LineTo(selected_box.right, cy);
277     pDC->MoveTo(cx, selected_box.top - m_swatch_size);
278     pDC->LineTo(cx, selected_box.top);
279     pDC->MoveTo(cx, selected_box.bottom + m_swatch_size);
280     pDC->LineTo(cx, selected_box.bottom);
281 }
282 
283 ///////////////////////////////////////////////////////////////////////////////
GetColorGradientDimension(int * rows,int * cols)284 void color_gradient_win::GetColorGradientDimension(int *rows, int *cols)
285 {
286     CRect boxrect;
287 
288     GetClientRect(boxrect);
289 
290     if (rows)
291     {
292         (*rows) = (boxrect.Height() / m_swatch_size);
293     }
294 
295     if (cols)
296     {
297         (*cols) = (boxrect.Width() / m_swatch_size);
298     }
299 }
300 
301 ///////////////////////////////////////////////////////////////////////////////
InvalidateSelection(int row,int col)302 void color_gradient_win::InvalidateSelection(int row, int col)
303 {
304     CRect swatch;
305     GetClientRect(swatch);
306 
307     swatch.right = swatch.left + m_swatch_size;
308     swatch.bottom = swatch.top + m_swatch_size;
309 
310     if (!mpParent->GetPalette()->palette)
311     {
312         swatch.InflateRect(m_swatch_size, m_swatch_size);
313     }
314 
315     swatch.OffsetRect(m_swatch_size * col, m_swatch_size * row);
316 
317     // Invalidate selection area
318     InvalidateRect(&swatch);
319 }
320 
321 ///////////////////////////////////////////////////////////////////////////////
GetSelectedPos(int * row,int * col)322 void color_gradient_win::GetSelectedPos(int *row, int *col)
323 {
324     if (mpParent->GetPalette()->palette)
325     {
326         int color_index = mpParent->GetPaletteColor();
327         int columns;
328 
329         GetColorGradientDimension(NULL, &columns);
330 
331         if (row)
332         {
333             (*row) = color_index / columns;
334         }
335 
336         if (col)
337         {
338             (*col) = color_index % columns;
339         }
340     }
341     else
342     {
343         int sat, hue, lum;
344         int rows, columns;
345 
346         GetColorGradientDimension(&rows, &columns);
347 
348         mpParent->GetHSLColor(&hue, &sat, &lum);
349 
350         if (row)
351         {
352             (*row) = (sat * (rows - 1) + 50) / 100;
353         }
354 
355         if (col)
356         {
357             (*col) = (hue * (columns - 1) + 180) / 360;
358         }
359     }
360 }
361 
362 ///////////////////////////////////////////////////////////////////////////////
SetSelectedPos(int row,int col)363 void color_gradient_win::SetSelectedPos(int row, int col)
364 {
365     int rows, columns;
366     GetColorGradientDimension(&rows, &columns);
367 
368     if ((row < 0) || (row >= rows) || (col < 0) || (col >= columns))
369     {
370         return;
371     }
372     palette_info* palinfo = mpParent->GetPalette();
373 
374     int old_row, old_col;
375     GetSelectedPos(&old_row, &old_col);
376 
377     GX_BOOL update = FALSE;
378 
379     if (palinfo->palette)
380     {
381         int index = row * columns + col;
382 
383         if ((index >= 0) && (index < palinfo->used_size))
384         {
385             mpParent->SetPaletteColor(index);
386             update = TRUE;;
387         }
388     }
389     else
390     {
391         double satfloat = (double)row / (double)(rows - 1);
392         double huefloat = (double)col / (double)(columns - 1);
393         satfloat *= 100;
394         int sat = (int)(satfloat + 0.5);
395 
396         huefloat *= 360;
397         int hue = (int)(huefloat + 0.5);
398 
399         color_edit_dialog* dlg = (color_edit_dialog*)GetParent();
400         dlg->SetHSLColor(hue, sat);
401 
402         update = TRUE;
403     }
404 
405     if (update)
406     {
407         InvalidateSelection(old_row, old_col);
408         InvalidateSelection(row, col);
409     }
410 }
411 
412 ///////////////////////////////////////////////////////////////////////////////
OnLButtonDown(UINT nFlags,CPoint point)413 void color_gradient_win::OnLButtonDown(UINT nFlags, CPoint point)
414 {
415     if (GetFocus() != this)
416     {
417         SetFocus();
418     }
419 
420     CRect boxrect;
421     GetClientRect(boxrect);
422 
423     if (boxrect.PtInRect(point))
424     {
425         int row, column;
426         column = (point.x - boxrect.left) / m_swatch_size;
427         row = (point.y - boxrect.top) / m_swatch_size;
428 
429         SetSelectedPos(row, column);
430     }
431 }
432 
433 ///////////////////////////////////////////////////////////////////////////////
PreTranslateMessage(MSG * pMsg)434 BOOL color_gradient_win::PreTranslateMessage(MSG* pMsg)
435 {
436     if (pMsg->message == WM_KEYDOWN)
437     {
438         BOOL processed = TRUE;
439         int selected_row, selected_col;
440 
441         GetSelectedPos(&selected_row, &selected_col);
442 
443         switch (pMsg->wParam)
444         {
445         case VK_UP:
446             selected_row--;
447             break;
448 
449         case VK_DOWN:
450             selected_row++;
451             break;
452 
453         case VK_LEFT:
454             selected_col--;
455             break;
456 
457         case VK_RIGHT:
458             selected_col++;
459             break;
460 
461         default:
462             processed = FALSE;
463             break;
464         }
465 
466         if (processed)
467         {
468             SetSelectedPos(selected_row, selected_col);
469             return TRUE;
470         }
471     }
472 
473     return CWnd::PreTranslateMessage(pMsg);
474 }
475 
476 
477 ///////////////////////////////////////////////////////////////////////////////
OnSetFocus(CWnd * pOldWnd)478 void color_gradient_win::OnSetFocus(CWnd* pOldWnd)
479 {
480     CWnd::OnSetFocus(pOldWnd);
481 
482     Invalidate();
483 }
484 
485 ///////////////////////////////////////////////////////////////////////////////
OnKillFocus(CWnd * pNewWnd)486 void color_gradient_win::OnKillFocus(CWnd* pNewWnd)
487 {
488     CWnd::OnKillFocus(pNewWnd);
489 
490     Invalidate();
491 }
492 
BEGIN_MESSAGE_MAP(color_edit_dialog,express_dialog)493 BEGIN_MESSAGE_MAP(color_edit_dialog, express_dialog)
494     ON_EN_KILLFOCUS(IDC_EDIT_RED, &color_edit_dialog::OnEnChangeEditRed)
495     ON_EN_KILLFOCUS(IDC_EDIT_GREEN, &color_edit_dialog::OnEnChangeEditGreen)
496     ON_EN_KILLFOCUS(IDC_EDIT_BLUE, &color_edit_dialog::OnEnChangeEditBlue)
497     ON_EN_KILLFOCUS(IDC_EDIT_ALPHA, &color_edit_dialog::OnEnChangeEditAlpha)
498     ON_EN_KILLFOCUS(IDC_EDIT_HUE, &color_edit_dialog::OnEnChangeEditHue)
499     ON_EN_KILLFOCUS(IDC_EDIT_SATURATION, &color_edit_dialog::OnEnChangeEditSaturation)
500     ON_EN_KILLFOCUS(IDC_EDIT_LUMINANCE, &color_edit_dialog::OnEnChangeEditLuminance)
501     ON_MESSAGE(STUDIO_TEST, &color_edit_dialog::OnTestMessage)
502     ON_WM_HSCROLL()
503     ON_WM_PAINT()
504     ON_WM_LBUTTONDOWN()
505     ON_BN_CLICKED(IDCANCEL, OnCancel)
506     ON_BN_CLICKED(IDOK, OnOK)
507     ON_WM_CREATE()
508 END_MESSAGE_MAP()
509 
510 
511 // color_edit_dialog dialog
512 
513 ///////////////////////////////////////////////////////////////////////////////
514 color_edit_dialog::color_edit_dialog(int display_index, CWnd* pParent /*=NULL*/)
515     : express_dialog(color_edit_dialog::IDD, pParent)
516 {
517     IconId = IDB_COLORS;
518     SetTitleText("Edit Color");
519 
520     mpRes = NULL;
521     mName = "NEW_COLOR";
522     mRed = mGreen = mBlue = 0;
523     mAlpha = 255;
524     mDisplayIndex = display_index;
525     mResId = GX_MAX_DEFAULT_COLORS;
526     mColorIndex = 0;
527 
528     mpColorGradientWin = NULL;
529     mpProject = GetOpenProject();
530 
531     if (mpProject)
532     {
533         INT active_theme = mpProject->mDisplays[mDisplayIndex].active_theme;
534         GX_COLOR *palette = mpProject->mDisplays[mDisplayIndex].themes[active_theme].palette;
535 
536         /* Create dynamic palette for some formats. */
537         /* default mPalette. */
538         mPalette.palette = NULL;
539         mPalette.total_size = 0;
540         mPalette.used_size = 0;
541 
542         switch (mpProject->mDisplays[display_index].colorformat)
543         {
544         case GX_COLOR_FORMAT_8BIT_PALETTE:
545             if (palette)
546             {
547                 mPalette.palette = palette;
548                 mPalette.total_size = mpProject->mDisplays[display_index].themes[active_theme].palette_total_size;
549                 mPalette.used_size = mpProject->mDisplays[display_index].themes[active_theme].palette_predefined;
550             }
551             break;
552 
553         case GX_COLOR_FORMAT_8BIT_PACKED_PIXEL:
554             if (palette == GX_NULL)
555             {
556                 ProjectConfigDlg::CreateDefault332RGBPalette(mPalette);
557             }
558             break;
559 
560         case GX_COLOR_FORMAT_4BIT_GRAY:
561             if (palette == GX_NULL)
562             {
563                 ProjectConfigDlg::CreateDefault4BppPalette(mPalette);
564             }
565             break;
566 
567         case GX_COLOR_FORMAT_MONOCHROME:
568             if (palette == GX_NULL)
569             {
570                 ProjectConfigDlg::CreateDefault1BppPalette(mPalette);
571             }
572             break;
573         }
574     }
575 }
576 
577 ///////////////////////////////////////////////////////////////////////////////
color_edit_dialog(resource_item * res,int display_index,CWnd * pParent)578 color_edit_dialog::color_edit_dialog(resource_item *res, int display_index, CWnd* pParent /*=NULL*/)
579     : express_dialog(color_edit_dialog::IDD, pParent)
580 {
581     IconId = IDB_COLORS;
582     SetTitleText("Edit Color");
583 
584     mpRes = res->mpRes;
585     mName = res->mpRes->name;
586     GX_COLOR color = res->GetRGBColor(display_index);
587 
588     mAlpha = (color >> 24) & 0xff;
589     mRed = (color >> 16) & 0xff;;
590     mGreen = (color >> 8) & 0xff;
591     mBlue = color & 0xff;
592     mDisplayIndex = display_index;
593     mpColorGradientWin = NULL;
594 
595     mpProject = GetOpenProject();
596     mResId = mpProject->GetResourceId(display_index, res->mpRes);
597 
598     INT active_theme = mpProject->mDisplays[mDisplayIndex].active_theme;
599     GX_COLOR *palette = mpProject->mDisplays[mDisplayIndex].themes[active_theme].palette;
600 
601     int color_format = mpProject->mDisplays[display_index].colorformat;
602 
603     if (!IsAlphaFormat(color_format))
604     {
605         mAlpha = 0xff;
606     }
607 
608     /* Create dynamic palette for some formats. */
609     /* default mPalette. */
610     mPalette.palette = NULL;
611     mPalette.total_size = 0;
612     mPalette.used_size = 0;
613 
614     switch (color_format)
615     {
616     case GX_COLOR_FORMAT_8BIT_PALETTE:
617         if (palette)
618         {
619             mPalette.palette = palette;
620             mPalette.total_size = mpProject->mDisplays[display_index].themes[active_theme].palette_total_size;
621             mPalette.used_size = mpProject->mDisplays[display_index].themes[active_theme].palette_predefined;
622             mColorIndex = res->mpRes->colorval;
623         }
624         break;
625 
626     case GX_COLOR_FORMAT_8BIT_PACKED_PIXEL:
627         if (palette == GX_NULL)
628         {
629             ProjectConfigDlg::CreateDefault332RGBPalette(mPalette);
630         }
631         break;
632 
633     case GX_COLOR_FORMAT_4BIT_GRAY:
634         if (palette == GX_NULL)
635         {
636             ProjectConfigDlg::CreateDefault4BppPalette(mPalette);
637         }
638         break;
639 
640     case GX_COLOR_FORMAT_MONOCHROME:
641         if (palette == GX_NULL)
642         {
643             ProjectConfigDlg::CreateDefault1BppPalette(mPalette);
644         }
645         break;
646     }
647 }
648 
649 ///////////////////////////////////////////////////////////////////////////////
~color_edit_dialog()650 color_edit_dialog::~color_edit_dialog()
651 {
652     switch (mpProject->mDisplays[mDisplayIndex].colorformat)
653     {
654     case GX_COLOR_FORMAT_8BIT_PACKED_PIXEL:
655     case GX_COLOR_FORMAT_4BIT_GRAY:
656     case GX_COLOR_FORMAT_MONOCHROME:
657 
658         if (mPalette.palette != GX_NULL)
659         {
660             delete[] mPalette.palette;
661         }
662         break;
663 
664     default:
665         break;
666     }
667 
668     if (mpColorGradientWin)
669     {
670         delete mpColorGradientWin;
671     }
672 }
673 
674 ///////////////////////////////////////////////////////////////////////////////
OnOK()675 void color_edit_dialog::OnOK()
676 {
677     CWnd *child = GetFocus();
678     if (child)
679     {
680         int id = child->GetDlgCtrlID();
681         switch (id)
682         {
683         case IDC_EDIT_RED:
684         case IDC_EDIT_GREEN:
685         case IDC_EDIT_BLUE:
686         case IDC_EDIT_ALPHA:
687         case IDC_EDIT_HUE:
688         case IDC_EDIT_SATURATION:
689         case IDC_EDIT_LUMINANCE:
690             SetFocus();
691             return;
692 
693         default:
694             break;
695         }
696     }
697 
698     CEdit* edit = (CEdit *)GetDlgItem(IDC_COLOR_NAME);
699     CString text;
700     GetDlgItemText(IDC_COLOR_NAME, text);
701 
702     if (mpRes == NULL)
703     {
704         if (NameExists(mDisplayIndex, RES_TYPE_COLOR, text))
705         {
706             Notify("Color name already exits.", this);
707             SetDlgItemText(IDC_COLOR_NAME, NULL);
708             return;
709         }
710     }
711     else
712     {
713         if (_tcscmp(text, mName))
714         {
715             if (NameExists(mDisplayIndex, RES_TYPE_COLOR, text))
716             {
717                 Notify("Color name already exits.", this);
718                 SetDlgItemText(IDC_COLOR_NAME, mName);
719                 return;
720             }
721         }
722     }
723 
724     if (TestInputName(edit, "Color Name", mName, this))
725     {
726         return express_dialog::OnOK();
727     }
728 }
729 
730 ///////////////////////////////////////////////////////////////////////////////
OnCreate(LPCREATESTRUCT lpCreateStruct)731 int color_edit_dialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
732 {
733     if (express_dialog::OnCreate(lpCreateStruct) == -1)
734         return -1;
735 
736     mColorUpdateMsg.Create(L"", WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this);
737     SetLiveRegion(mColorUpdateMsg.GetSafeHwnd());
738 
739     return 0;
740 }
741 
742 
743 ///////////////////////////////////////////////////////////////////////////////
OnInitDialog()744 BOOL color_edit_dialog::OnInitDialog()
745 {
746     express_dialog::OnInitDialog();
747 
748     CRect color_lable_size;
749     CRect boxrect;
750     GetClientRect(&boxrect);
751 
752     //Get rectangle box of color name edit control
753     GetDlgItem(IDC_COLORS)->GetWindowRect(&color_lable_size);
754     ScreenToClient(&color_lable_size);
755 
756     boxrect.top = color_lable_size.bottom + 10;
757     boxrect.bottom -= (m_status_bar_height + 10);
758     boxrect.left = color_lable_size.left;
759     boxrect.right = color_lable_size.right;
760     mpColorGradientWin = new color_gradient_win(this);
761     mpColorGradientWin->Create(target_class_name, _T("Colors"), WS_VISIBLE | WS_CHILD | WS_TABSTOP, boxrect, this, 0);
762     mpColorGradientWin->SetWindowPos(GetDlgItem(IDC_COLORS), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
763 
764     AddCancelButton();
765     AddSaveButton();
766 
767     SetAccessibleHelpString(GetDlgItem(IDC_EDIT_RED)->GetSafeHwnd(), _T("Red value must be between 0 and 255"));
768     SetAccessibleHelpString(GetDlgItem(IDC_EDIT_GREEN)->GetSafeHwnd(), _T("Green value must be between 0 and 255"));
769     SetAccessibleHelpString(GetDlgItem(IDC_EDIT_BLUE)->GetSafeHwnd(), _T("Blue value must be between 0 and 255"));
770     SetAccessibleHelpString(GetDlgItem(IDC_EDIT_ALPHA)->GetSafeHwnd(), _T("Alpha value must be between 0 and 255"));
771     SetAccessibleHelpString(GetDlgItem(IDC_EDIT_HUE)->GetSafeHwnd(), _T("Hue value must be between 0 and 360"));
772     SetAccessibleHelpString(GetDlgItem(IDC_EDIT_SATURATION)->GetSafeHwnd(), _T("Saturation value must be between 0 and 100"));
773     SetAccessibleHelpString(GetDlgItem(IDC_EDIT_LUMINANCE)->GetSafeHwnd(), _T("Luminance value must be between 0 and 100"));
774 
775     SetControlAccessibleName(GetDlgItem(IDC_RED_SLIDER)->GetSafeHwnd(), _T("Red"));
776     SetControlAccessibleName(GetDlgItem(IDC_GREEN_SLIDER)->GetSafeHwnd(), _T("Green"));
777     SetControlAccessibleName(GetDlgItem(IDC_BLUE_SLIDER)->GetSafeHwnd(), _T("Blue"));
778     SetControlAccessibleName(GetDlgItem(IDC_ALPHA_SLIDER)->GetSafeHwnd(), _T("Alpha"));
779     SetControlAccessibleName(GetDlgItem(IDC_HUE_SLIDER)->GetSafeHwnd(), _T("Hue"));
780     SetControlAccessibleName(GetDlgItem(IDC_SAT_SLIDER)->GetSafeHwnd(), _T("Sat"));
781     SetControlAccessibleName(GetDlgItem(IDC_LUM_SLIDER)->GetSafeHwnd(), _T("Lum"));
782 
783     return TRUE;  // return TRUE unless you set the focus to a control
784                   // EXCEPTION: OCX Property Pages should return FALSE
785 }
786 
787 ///////////////////////////////////////////////////////////////////////////////
DoDataExchange(CDataExchange * pDX)788 void color_edit_dialog::DoDataExchange(CDataExchange* pDX)
789 {
790     CDialog::DoDataExchange(pDX);
791     DDX_Text(pDX, IDC_COLOR_NAME, mName);
792     DDX_Control(pDX, IDC_EDIT_RED, mRedEdit);
793     DDX_Control(pDX, IDC_EDIT_GREEN, mGreenEdit);
794     DDX_Control(pDX, IDC_EDIT_BLUE, mBlueEdit);
795     DDX_Control(pDX, IDC_EDIT_ALPHA, mAlphaEdit);
796     DDX_Control(pDX, IDC_EDIT_HUE, mHueEdit);
797     DDX_Control(pDX, IDC_EDIT_SATURATION, mSaturationEdit);
798     DDX_Control(pDX, IDC_EDIT_LUMINANCE, mLuminanceEdit);
799     DDX_Control(pDX, IDC_RED_SLIDER, mRedSlider);
800     DDX_Control(pDX, IDC_GREEN_SLIDER, mGreenSlider);
801     DDX_Control(pDX, IDC_BLUE_SLIDER, mBlueSlider);
802     DDX_Control(pDX, IDC_HUE_SLIDER, mHueSlider);
803     DDX_Control(pDX, IDC_SAT_SLIDER, mSaturationSlider);
804     DDX_Control(pDX, IDC_LUM_SLIDER, mLuminanceSlider);
805     DDX_Control(pDX, IDC_ALPHA_SLIDER, mAlphaSlider);
806 
807     mRedSlider.SetRange(0, 255);
808     mGreenSlider.SetRange(0, 255);
809     mBlueSlider.SetRange(0, 255);
810     mAlphaSlider.SetRange(0, 255);
811 
812     mHueSlider.SetRange(0, 360);
813     mSaturationSlider.SetRange(0, 100);
814     mLuminanceSlider.SetRange(0, 100);
815 
816     DDX_Text(pDX, IDC_EDIT_RED, mRed);
817     DDX_Text(pDX, IDC_EDIT_GREEN, mGreen);
818     DDX_Text(pDX, IDC_EDIT_BLUE, mBlue);
819     DDX_Text(pDX, IDC_EDIT_ALPHA, mAlpha);
820 
821     DDX_Slider(pDX, IDC_RED_SLIDER, mRed);
822     DDX_Slider(pDX, IDC_GREEN_SLIDER, mGreen);
823     DDX_Slider(pDX, IDC_BLUE_SLIDER, mBlue);
824     DDX_Slider(pDX, IDC_ALPHA_SLIDER, mAlpha);
825 
826     if (!pDX->m_bSaveAndValidate)
827     {
828         UpdateHSLFields();
829     }
830 
831     if (mResId < GX_MAX_DEFAULT_COLORS)
832     {
833         GetDlgItem(IDC_COLOR_NAME)->EnableWindow(FALSE);
834         GetDlgItem(IDC_REQUIRED_FIELD_LEGEND)->ShowWindow(FALSE);
835     }
836     else
837     {
838         SetAccessibleFullDescription(GetDlgItem(IDC_COLOR_NAME)->GetSafeHwnd(), L"Required");
839         SetAccessibleDescription(GetDlgItem(IDC_COLOR_NAME)->GetSafeHwnd(), L"Required");
840     }
841 
842     /* Int palette or grayscale mode rgb color can not be edit. */
843     if ((mPalette.palette) || (mpProject->mDisplays[mDisplayIndex].colorformat <= GX_COLOR_FORMAT_8BIT_PALETTE))
844     {
845         mRedSlider.EnableWindow(FALSE);
846         mGreenSlider.EnableWindow(FALSE);
847         mBlueSlider.EnableWindow(FALSE);
848         mHueSlider.EnableWindow(FALSE);
849         mSaturationSlider.EnableWindow(FALSE);
850         mLuminanceSlider.EnableWindow(FALSE);
851 
852         mRedEdit.EnableWindow(FALSE);
853         mGreenEdit.EnableWindow(FALSE);
854         mBlueEdit.EnableWindow(FALSE);
855         mHueEdit.EnableWindow(FALSE);
856         mSaturationEdit.EnableWindow(FALSE);
857         mLuminanceEdit.EnableWindow(FALSE);
858     }
859 
860     if (!IsAlphaFormat(mpProject->mDisplays[mDisplayIndex].colorformat))
861     {
862         mAlphaSlider.EnableWindow(FALSE);
863         mAlphaEdit.EnableWindow(FALSE);
864     }
865 }
866 
867 ///////////////////////////////////////////////////////////////////////////////
IsAlphaFormat(int color_format)868 BOOL color_edit_dialog::IsAlphaFormat(int color_format)
869 {
870     if (color_format == GX_COLOR_FORMAT_32ARGB ||
871         color_format == GX_COLOR_FORMAT_32BGRA ||
872         color_format == GX_COLOR_FORMAT_4444ARGB ||
873         color_format == GX_COLOR_FORMAT_4444BGRA)
874     {
875         return TRUE;
876     }
877     return FALSE;
878 }
879 
880 ///////////////////////////////////////////////////////////////////////////////
AnnounceColorUpdate(CString msg)881 void color_edit_dialog::AnnounceColorUpdate(CString msg)
882 {
883     CMainFrame *pMain = (CMainFrame*) AfxGetApp()->GetMainWnd();
884 
885     if (pMain)
886     {
887         pMain->GetStatusMsgControl()->SetWindowText(msg);
888         pMain->GetStatusMsgControl()->NotifyWinEvent(
889             EVENT_OBJECT_LIVEREGIONCHANGED,
890             OBJID_CLIENT,
891             CHILDID_SELF);
892 
893         mColorUpdateMsg.SetWindowText(msg);
894         mColorUpdateMsg.NotifyWinEvent(
895             EVENT_OBJECT_LIVEREGIONCHANGED,
896             OBJID_CLIENT,
897             CHILDID_SELF);
898     }
899 }
900 
901 ///////////////////////////////////////////////////////////////////////////////
PaintColorPreview()902 void color_edit_dialog::PaintColorPreview()
903 {
904     COLORREF wincolor;
905     GX_DISPLAY *display = get_target_view_display();
906 
907     if (display->gx_display_color_format == GX_COLOR_FORMAT_8BIT_PALETTE)
908     {
909         int colorIndex = GetColor();
910         GX_COLOR color = 0;
911 
912         if (mPalette.palette && colorIndex < mPalette.total_size)
913         {
914             color = mPalette.palette[GetColor()];
915         }
916 
917         wincolor = resource_item::MakeColorRef(color);
918     }
919     else
920     {
921         wincolor = resource_item::MakeColorRef(GetColor());
922     }
923 
924     CRect boxrect;
925     CBrush boxbrush(wincolor);
926     CPen newpen(PS_SOLID, 0, wincolor);
927     CBrush *oldbrush;
928     CPen *oldpen;
929     CDC *dc = GetDC();
930     CRect color_name_size;
931     CRect color_preview_size;
932 
933     //Get rectangle box of color name edit control
934     GetDlgItem(IDC_COLOR_NAME)->GetWindowRect(&color_name_size);
935     ScreenToClient(&color_name_size);
936 
937     GetDlgItem(IDC_COLOR_PREVIEW_LABEL)->GetWindowRect(&color_preview_size);
938     ScreenToClient(&color_preview_size);
939 
940     //Make the preview box align with the top of color name edit control
941     GetClientRect(&boxrect);
942     oldbrush = dc->SelectObject(&boxbrush);
943     oldpen = dc->SelectObject(&newpen);
944     dc->Rectangle(color_preview_size.left, color_name_size.top, boxrect.right - 10, color_name_size.bottom);
945     dc->SelectObject(oldbrush);
946     dc->SelectObject(oldpen);
947     ReleaseDC(dc);
948 
949 }
950 
951 ///////////////////////////////////////////////////////////////////////////////
OnPaint()952 void color_edit_dialog::OnPaint()
953 {
954     express_dialog::OnPaint();
955 
956     if (mResId >= GX_MAX_DEFAULT_COLORS)
957     {
958         PaintRequiredAsterisk(IDC_COLOR_NAME);
959         PaintRequiredAsterisk(IDC_REQUIRED_FIELD_LEGEND);
960     }
961 
962     PaintColorPreview();
963 }
964 
965 
966 ///////////////////////////////////////////////////////////////////////////////
GetName()967 CString color_edit_dialog::GetName()
968 {
969     return mName;
970 }
971 
972 ///////////////////////////////////////////////////////////////////////////////
GetColor()973 GX_COLOR color_edit_dialog::GetColor()
974 {
975     GX_COLOR color = (mAlpha << 24) | (mRed << 16) | (mGreen << 8) | mBlue;
976 
977     GX_DISPLAY *display = get_target_view_display();
978 
979     if (display->gx_display_color_format == GX_COLOR_FORMAT_8BIT_PALETTE)
980     {
981         color = mColorIndex;
982     }
983 
984     return color;
985 }
986 
987 // color_edit_dialog message handlers
988 
989 ///////////////////////////////////////////////////////////////////////////////
OnEnChangeEditRed()990 void color_edit_dialog::OnEnChangeEditRed()
991 {
992     int val = GetDlgItemInt(IDC_EDIT_RED);
993     if (val >= 0 && val <= 255)
994     {
995         mRed = val;
996         mRedSlider.SetPos(mRed);
997         UpdateHSLFields();
998         PaintColorPreview();
999     }
1000     else
1001     {
1002         Notify("Invalid value for red. Valid range is from 0 to 255.", this);
1003         SetDlgItemInt(IDC_EDIT_RED, mRed);
1004     }
1005 }
1006 
1007 
1008 ///////////////////////////////////////////////////////////////////////////////
OnEnChangeEditGreen()1009 void color_edit_dialog::OnEnChangeEditGreen()
1010 {
1011     int val = GetDlgItemInt(IDC_EDIT_GREEN);
1012     if (val >= 0 && val <= 255)
1013     {
1014         mGreen = val;
1015         mGreenSlider.SetPos(mGreen);
1016         UpdateHSLFields();
1017         PaintColorPreview();
1018     }
1019     else
1020     {
1021         Notify("Invalid value for green. Valid range is from 0 to 255.", this);
1022         SetDlgItemInt(IDC_EDIT_GREEN, mGreen);
1023     }
1024 }
1025 
1026 
1027 ///////////////////////////////////////////////////////////////////////////////
OnEnChangeEditBlue()1028 void color_edit_dialog::OnEnChangeEditBlue()
1029 {
1030     int val = GetDlgItemInt(IDC_EDIT_BLUE);
1031     if (val >= 0 && val <= 255)
1032     {
1033         mBlue = val;
1034         mBlueSlider.SetPos(mBlue);
1035         UpdateHSLFields();
1036         PaintColorPreview();
1037     }
1038     else
1039     {
1040         Notify("Invalid value for blue. Valid range is from 0 to 255.", this);
1041         SetDlgItemInt(IDC_EDIT_BLUE, mBlue);
1042     }
1043 }
1044 
1045 ///////////////////////////////////////////////////////////////////////////////
OnEnChangeEditAlpha()1046 void color_edit_dialog::OnEnChangeEditAlpha()
1047 {
1048     int val = GetDlgItemInt(IDC_EDIT_ALPHA);
1049 
1050     if (val >= 0 && val <= 255)
1051     {
1052         mAlpha = val;
1053         mAlphaSlider.SetPos(mAlpha);
1054     }
1055     else
1056     {
1057         Notify("Invalid value for alpha. Valid range is from 0 to 255.", this);
1058         SetDlgItemInt(IDC_EDIT_ALPHA, mAlpha);
1059     }
1060 }
1061 
1062 
1063 ///////////////////////////////////////////////////////////////////////////////
OnEnChangeEditHue()1064 void color_edit_dialog::OnEnChangeEditHue()
1065 {
1066     int val = GetDlgItemInt(IDC_EDIT_HUE);
1067 
1068     if (val >= 0 && val <= 360)
1069     {
1070         mHue = val;
1071         mHueSlider.SetPos(val);
1072         UpdateRGBVals();
1073         UpdateRGBFields();
1074         mpColorGradientWin->Invalidate();
1075     }
1076     else
1077     {
1078         Notify("Invalid value for hue. Valid range is from 0 to 360.", this);
1079         SetDlgItemInt(IDC_EDIT_HUE, mHueSlider.GetPos());
1080     }
1081 }
1082 
1083 ///////////////////////////////////////////////////////////////////////////////
OnEnChangeEditLuminance()1084 void color_edit_dialog::OnEnChangeEditLuminance()
1085 {
1086     int val = GetDlgItemInt(IDC_EDIT_LUMINANCE);
1087 
1088     if (val >= 0 && val <= 100)
1089     {
1090         mLum = val;
1091         mLuminanceSlider.SetPos(val);
1092         UpdateRGBVals();
1093         UpdateRGBFields();
1094         mpColorGradientWin->Invalidate();
1095     }
1096     else
1097     {
1098         Notify("Invalid value for luminance. Valid range is from 0 to 100.", this);
1099         SetDlgItemInt(IDC_EDIT_LUMINANCE, mLuminanceSlider.GetPos());
1100     }
1101 }
1102 
1103 
1104 ///////////////////////////////////////////////////////////////////////////////
OnEnChangeEditSaturation()1105 void color_edit_dialog::OnEnChangeEditSaturation()
1106 {
1107     int val = GetDlgItemInt(IDC_EDIT_SATURATION);
1108 
1109     if (val >= 0 && val <= 100)
1110     {
1111         mSat = val;
1112         mSaturationSlider.SetPos(val);
1113         UpdateRGBVals();
1114         UpdateRGBFields();
1115         mpColorGradientWin->Invalidate();
1116     }
1117     else
1118     {
1119         Notify("Invalid value for saturation. Valid range is from 0 to 255.", this);
1120         SetDlgItemInt(IDC_EDIT_SATURATION, mSaturationSlider.GetPos());
1121     }
1122 
1123 }
1124 
1125 ///////////////////////////////////////////////////////////////////////////////
OnHScroll(UINT nSBCode,UINT nPos,CScrollBar * bar)1126 void color_edit_dialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *bar)
1127 {
1128     if (nSBCode == SB_THUMBPOSITION ||
1129         nSBCode == SB_THUMBTRACK ||
1130         nSBCode == SB_PAGELEFT ||
1131         nSBCode == SB_PAGERIGHT ||
1132         nSBCode == SB_LINELEFT ||
1133         nSBCode == SB_LINERIGHT)
1134     {
1135         int ctrl_id = bar->GetDlgCtrlID();
1136         int curpos = ((CSliderCtrl *)GetDlgItem(ctrl_id))->GetPos();
1137 
1138         nPos = curpos;
1139 
1140         switch (ctrl_id)
1141         {
1142         case IDC_RED_SLIDER:
1143             mRed = nPos;
1144             SetDlgItemInt(IDC_EDIT_RED, mRed);
1145             UpdateHSLFields();
1146             PaintColorPreview();
1147             mpColorGradientWin->Invalidate();
1148             break;
1149 
1150         case IDC_GREEN_SLIDER:
1151             mGreen = nPos;
1152             SetDlgItemInt(IDC_EDIT_GREEN, mGreen);
1153             UpdateHSLFields();
1154             PaintColorPreview();
1155             mpColorGradientWin->Invalidate();
1156             break;
1157 
1158         case IDC_BLUE_SLIDER:
1159             mBlue = nPos;
1160             SetDlgItemInt(IDC_EDIT_BLUE, mBlue);
1161             UpdateHSLFields();
1162             PaintColorPreview();
1163             mpColorGradientWin->Invalidate();
1164             break;
1165 
1166         case IDC_ALPHA_SLIDER:
1167             mAlpha = nPos;
1168             SetDlgItemInt(IDC_EDIT_ALPHA, mAlpha);
1169             break;
1170 
1171         case IDC_HUE_SLIDER:
1172             mHue = nPos;
1173             SetDlgItemInt(IDC_EDIT_HUE, nPos);
1174             UpdateRGBVals();
1175             UpdateRGBFields();
1176             mpColorGradientWin->Invalidate();
1177             break;
1178 
1179         case IDC_SAT_SLIDER:
1180             mSat = nPos;
1181             SetDlgItemInt(IDC_EDIT_SATURATION, nPos);
1182             UpdateRGBVals();
1183             UpdateRGBFields();
1184             mpColorGradientWin->Invalidate();
1185             break;
1186 
1187         case IDC_LUM_SLIDER:
1188             mLum = nPos;
1189             SetDlgItemInt(IDC_EDIT_LUMINANCE, nPos);
1190             UpdateRGBVals();
1191             UpdateRGBFields();
1192             mpColorGradientWin->Invalidate();
1193             break;
1194         }
1195     }
1196 }
1197 
1198 
1199 ///////////////////////////////////////////////////////////////////////////////
UpdateHSLFields()1200 void color_edit_dialog::UpdateHSLFields()
1201 {
1202     RgbToHsl(mRed, mGreen, mBlue, mHue, mSat, mLum);
1203 
1204     mHueSlider.SetPos(mHue);
1205     mSaturationSlider.SetPos(mSat);
1206     mLuminanceSlider.SetPos(mLum);
1207 
1208     SetDlgItemInt(IDC_EDIT_HUE, mHue);
1209     SetDlgItemInt(IDC_EDIT_SATURATION, mSat);
1210     SetDlgItemInt(IDC_EDIT_LUMINANCE, mLum);
1211 
1212     CString msg;
1213     msg.Format(L"hue %d, saturation: %d, luminance: %d", mHue, mSat, mLum);
1214     AnnounceColorUpdate(msg);
1215 
1216     if (!mPalette.palette && mpColorGradientWin)
1217     {
1218         mpColorGradientWin->Invalidate();
1219     }
1220 }
1221 
1222 ///////////////////////////////////////////////////////////////////////////////
UpdateRGBVals()1223 void color_edit_dialog::UpdateRGBVals()
1224 {
1225     int hue = GetDlgItemInt(IDC_EDIT_HUE);
1226     int sat = GetDlgItemInt(IDC_EDIT_SATURATION);
1227     int lum = GetDlgItemInt(IDC_EDIT_LUMINANCE);
1228 
1229     double huefloat = hue;
1230     huefloat /= 360.0;
1231 
1232     double satfloat = sat;
1233     satfloat /= 100.0;
1234 
1235     double lumfloat = lum;
1236     lumfloat /= 100.0;
1237 
1238     HslToRgb(huefloat, satfloat, lumfloat, mRed, mGreen, mBlue);
1239 }
1240 
1241 ///////////////////////////////////////////////////////////////////////////////
UpdateRGBFields()1242 void color_edit_dialog::UpdateRGBFields()
1243 {
1244     SetDlgItemInt(IDC_EDIT_RED, mRed);
1245     mRedSlider.SetPos(mRed);
1246 
1247     SetDlgItemInt(IDC_EDIT_GREEN, mGreen);
1248     mGreenSlider.SetPos(mGreen);
1249 
1250     SetDlgItemInt(IDC_EDIT_BLUE, mBlue);
1251     mBlueSlider.SetPos(mBlue);
1252 
1253     CString msg;
1254     msg.Format(L"red %d, green: %d, blue: %d", mRed, mGreen, mBlue);
1255     AnnounceColorUpdate(msg);
1256 
1257     PaintColorPreview();
1258 }
1259 
1260 ///////////////////////////////////////////////////////////////////////////////
HslToRgb(double hue,double sat,double lum,int & red,int & green,int & blue)1261 void color_edit_dialog::HslToRgb(double hue, double sat, double lum,
1262     int &red, int &green, int &blue)
1263 {
1264     double C = (1 - abs(2 * lum - 1)) * sat;
1265     double H = hue * 6;
1266     double X = C * (1 - abs((H - (int(H / 2)) * 2) - 1));
1267     double m = lum - C / 2;
1268     double red1 = 0, green1 = 0, blue1 = 0;
1269 
1270     if (H >= 6)
1271     {
1272         H = 0;
1273     }
1274 
1275     if (H < 1)
1276     {
1277         red1 = C;
1278         green1 = X;
1279     }
1280     else if (H < 2)
1281     {
1282         red1 = X;
1283         green1 = C;
1284     }
1285     else if (H < 3)
1286     {
1287         green1 = C;
1288         blue1 = X;
1289     }
1290     else if (H < 4)
1291     {
1292         green1 = X;
1293         blue1 = C;
1294     }
1295     else if (H < 5)
1296     {
1297         red1 = X;
1298         blue1 = C;
1299     }
1300     else
1301     {
1302         red1 = C;
1303         blue1 = X;
1304     }
1305 
1306     red1 += m;
1307     green1 += m;
1308     blue1 += m;
1309 
1310     red = int(red1 * 255);
1311     green = int(green1 * 255);
1312     blue = int(blue1 * 255);
1313 }
1314 
1315 ///////////////////////////////////////////////////////////////////////////////
RgbToHsl(int red,int green,int blue,int & hue,int & saturation,int & luminance)1316 void color_edit_dialog::RgbToHsl(int red, int green, int blue,
1317     int &hue, int &saturation, int &luminance)
1318 {
1319     double redfloat = red;
1320     double greenfloat = green;
1321     double bluefloat = blue;
1322 
1323     double huefloat = 0.0;
1324     double satfloat = 0.0;
1325     double lumfloat = 0.0;
1326 
1327     redfloat /= 255.0;
1328     greenfloat /= 255.0;
1329     bluefloat /= 255.0;
1330 
1331     double min = redfloat;
1332     if (greenfloat < min)
1333     {
1334         min = greenfloat;
1335     }
1336     if (bluefloat < min)
1337     {
1338         min = bluefloat;
1339     }
1340 
1341     double max = redfloat;
1342     int maxchannel = 0;
1343 
1344     if (max < greenfloat)
1345     {
1346         max = greenfloat;
1347         maxchannel = 1;
1348     }
1349     if (max < bluefloat)
1350     {
1351         max = bluefloat;
1352         maxchannel = 2;
1353     }
1354 
1355     lumfloat = (min + max) / 2.0;
1356 
1357     if (min != max)
1358     {
1359         if (lumfloat < 0.5)
1360         {
1361             satfloat = (max - min) / (max + min);
1362         }
1363         else
1364         {
1365             satfloat = (max - min) / (2.0 - max - min);
1366         }
1367 
1368         switch(maxchannel)
1369         {
1370         case 0: // red is our max
1371             huefloat = (greenfloat - bluefloat) / (max - min);
1372             break;
1373 
1374         case 1: // green is our max
1375             huefloat = 2.0 + (bluefloat - redfloat) / (max - min);
1376             break;
1377 
1378         case 2: // blue is our max
1379             huefloat = 4.0 + (redfloat - greenfloat) / (max - min);
1380             break;
1381         }
1382     }
1383 
1384     // convert values to int
1385     huefloat *= 60;
1386     if (huefloat < 0)
1387     {
1388         huefloat += 360.0;
1389     }
1390     hue = (int) (huefloat + 0.5);       // 0 to 360
1391 
1392     satfloat *= 100;
1393     saturation = (int) (satfloat + 0.5); // percentage
1394 
1395     lumfloat *= 100;
1396     luminance = (int) (lumfloat + 0.5); // percentage
1397 
1398 }
1399 
1400 ///////////////////////////////////////////////////////////////////////////////
SetPaletteColor(int index)1401 void color_edit_dialog::SetPaletteColor(int index)
1402 {
1403     mColorIndex = index;
1404 
1405     if (mPalette.palette)
1406     {
1407         GX_COLOR colorval = mPalette.palette[index];
1408 
1409         mRed = (colorval >> 16) & 0xff;
1410         mGreen = (colorval >> 8) & 0xff;
1411         mBlue = colorval & 0xff;
1412 
1413         UpdateRGBFields();
1414         UpdateHSLFields();
1415         PaintColorPreview();
1416     }
1417 }
1418 
1419 ///////////////////////////////////////////////////////////////////////////////
GetHSLColor(int * hue,int * sat,int * lum)1420 void color_edit_dialog::GetHSLColor(int *hue, int *sat, int *lum)
1421 {
1422     if (hue)
1423     {
1424         (*hue) = mHue;
1425     }
1426 
1427     if (sat)
1428     {
1429         (*sat) = mSat;
1430     }
1431 
1432     if (lum)
1433     {
1434         (*lum) = mLum;
1435     }
1436 }
1437 
1438 ///////////////////////////////////////////////////////////////////////////////
SetHSLColor(int hue,int sat)1439 void color_edit_dialog::SetHSLColor(int hue, int sat)
1440 {
1441     mSat = sat;
1442     mHue = hue;
1443 
1444     SetDlgItemInt(IDC_EDIT_SATURATION, mSat);
1445     SetDlgItemInt(IDC_EDIT_HUE, mHue);
1446     mHueSlider.SetPos(mHue);
1447     mSaturationSlider.SetPos(mSat);
1448     UpdateRGBVals();
1449     UpdateRGBFields();
1450 }
1451 
1452 ///////////////////////////////////////////////////////////////////////////////
PreTranslateMessage(MSG * pMsg)1453 BOOL color_edit_dialog::PreTranslateMessage(MSG *pMsg)
1454 {
1455     // TODO: Add your specialized code here and/or call the base class
1456 
1457     CWnd *focus_owner = GetFocus();
1458 
1459     if (focus_owner)
1460     {
1461         int ctrl_id = focus_owner->GetDlgCtrlID();
1462 
1463         switch (ctrl_id)
1464         {
1465         case IDC_EDIT_RED:
1466         case IDC_EDIT_GREEN:
1467         case IDC_EDIT_BLUE:
1468         case IDC_EDIT_ALPHA:
1469         case IDC_EDIT_HUE:
1470         case IDC_EDIT_LUMINANCE:
1471         case IDC_EDIT_SATURATION:
1472             if ((pMsg->message == WM_CHAR) && isprint(pMsg->wParam))
1473             {
1474                 if (pMsg->wParam < '0' || pMsg->wParam > '9')
1475                 {
1476                     ErrorMsg("Unacceptable Character. You can only type a number here.", this);
1477                     return TRUE;
1478                 }
1479             }
1480             break;
1481         }
1482     }
1483 
1484     return express_dialog::PreTranslateMessage(pMsg);
1485 }
1486 
1487 ///////////////////////////////////////////////////////////////////////////////
OnTestMessage(WPARAM wParam,LPARAM lParam)1488 LRESULT color_edit_dialog::OnTestMessage(WPARAM wParam, LPARAM lParam)
1489 {
1490     CWnd *pWnd;
1491     CRect boxrect;
1492     CStringArray params;
1493 
1494     switch (wParam)
1495     {
1496     case TEST_SET_COLOR_NAME:
1497         SetDlgItemText(IDC_COLOR_NAME, GetTestingParam(0));
1498         pWnd = GetDlgItem(IDC_COLOR_NAME);
1499         SendMessage(WM_COMMAND, MAKEWPARAM(IDC_COLOR_NAME, EN_KILLFOCUS), (LPARAM)pWnd->m_hWnd);
1500         break;
1501 
1502     case TEST_SET_COLOR_RED:
1503         SetDlgItemText(IDC_EDIT_RED, GetTestingParam(0));
1504         pWnd = GetDlgItem(IDC_EDIT_RED);
1505         SendMessage(WM_COMMAND, MAKEWPARAM(IDC_EDIT_RED, EN_KILLFOCUS), (LPARAM)pWnd->m_hWnd);
1506         break;
1507 
1508     case TEST_SET_COLOR_GREEN:
1509         SetDlgItemText(IDC_EDIT_GREEN, GetTestingParam(0));
1510         pWnd = GetDlgItem(IDC_EDIT_GREEN);
1511         SendMessage(WM_COMMAND, MAKEWPARAM(IDC_EDIT_GREEN, EN_KILLFOCUS), (LPARAM)pWnd->m_hWnd);
1512         break;
1513 
1514     case TEST_SET_COLOR_BLUE:
1515         SetDlgItemText(IDC_EDIT_BLUE, GetTestingParam(0));
1516         pWnd = GetDlgItem(IDC_EDIT_BLUE);
1517         SendMessage(WM_COMMAND, MAKEWPARAM(IDC_EDIT_BLUE, EN_KILLFOCUS), (LPARAM)pWnd->m_hWnd);
1518         break;
1519 
1520     case TEST_SET_COLOR_HUE:
1521         SetDlgItemText(IDC_EDIT_HUE, GetTestingParam(0));
1522         pWnd = GetDlgItem(IDC_EDIT_HUE);
1523         SendMessage(WM_COMMAND, MAKEWPARAM(IDC_EDIT_HUE, EN_KILLFOCUS), (LPARAM)pWnd->m_hWnd);
1524         break;
1525 
1526     case TEST_SET_COLOR_SAT:
1527         SetDlgItemText(IDC_EDIT_SATURATION, GetTestingParam(0));
1528         pWnd = GetDlgItem(IDC_EDIT_SATURATION);
1529         SendMessage(WM_COMMAND, MAKEWPARAM(IDC_EDIT_SATURATION, EN_KILLFOCUS), (LPARAM)pWnd->m_hWnd);
1530         break;
1531 
1532     case TEST_SET_COLOR_LUM:
1533         SetDlgItemText(IDC_EDIT_LUMINANCE, GetTestingParam(0));
1534         pWnd = GetDlgItem(IDC_EDIT_LUMINANCE);
1535         SendMessage(WM_COMMAND, MAKEWPARAM(IDC_EDIT_LUMINANCE, EN_KILLFOCUS), (LPARAM)pWnd->m_hWnd);
1536         break;
1537 
1538     case TEST_SET_PALETTE_COLOR_INDEX:
1539         mColorIndex = lParam;
1540         mRed = (mPalette.palette[mColorIndex] >> 16) & 0xff;
1541         mGreen = (mPalette.palette[mColorIndex] >> 8) & 0xff;
1542         mBlue = mPalette.palette[mColorIndex] & 0xff;
1543         UpdateRGBFields();
1544         UpdateHSLFields();
1545         PaintColorPreview();
1546         break;
1547 
1548     case TEST_SAVE_COLOR_EDIT:
1549         OnOK();
1550         break;
1551 
1552     case TEST_CANCEL_COLOR_EDIT:
1553         OnCancel();
1554         break;
1555     }
1556 
1557     return 0;
1558 }
1559