[MSPAINT] Add NUM_COLORS and enum PAL_TYPE (#4203)

Reduce magic numbers and improve debuggability. CORE-17931
This commit is contained in:
Katayama Hirofumi MZ 2021-12-28 19:01:31 +09:00 committed by GitHub
parent 59ceeb1da0
commit d30e5eb51c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 35 deletions

View file

@ -4,6 +4,7 @@
* FILE: base/applications/mspaint/palettemodel.cpp * FILE: base/applications/mspaint/palettemodel.cpp
* PURPOSE: Keep track of palette data, notify listeners * PURPOSE: Keep track of palette data, notify listeners
* PROGRAMMERS: Benedikt Freisen * PROGRAMMERS: Benedikt Freisen
* Katayama Hirofumi MZ
*/ */
/* INCLUDES *********************************************************/ /* INCLUDES *********************************************************/
@ -16,72 +17,77 @@ PaletteModel::PaletteModel()
{ {
m_fgColor = 0x00000000; m_fgColor = 0x00000000;
m_bgColor = 0x00ffffff; m_bgColor = 0x00ffffff;
SelectPalette(1); SelectPalette(PAL_MODERN);
} }
int PaletteModel::SelectedPalette() PAL_TYPE PaletteModel::SelectedPalette()
{ {
return m_nSelectedPalette; return m_nSelectedPalette;
} }
void PaletteModel::SelectPalette(int nPalette) void PaletteModel::SelectPalette(PAL_TYPE nPalette)
{ {
int modernColors[28] = { static const COLORREF modernColors[NUM_COLORS] =
{
0x000000, 0x464646, 0x787878, 0x300099, 0x241ced, 0x0078ff, 0x0ec2ff, 0x000000, 0x464646, 0x787878, 0x300099, 0x241ced, 0x0078ff, 0x0ec2ff,
0x00f2ff, 0x1de6a8, 0x4cb122, 0xefb700, 0xf36d4d, 0x99362f, 0x98316f, 0x00f2ff, 0x1de6a8, 0x4cb122, 0xefb700, 0xf36d4d, 0x99362f, 0x98316f,
0xffffff, 0xdcdcdc, 0xb4b4b4, 0x3c5a9c, 0xb1a3ff, 0x7aaae5, 0x9ce4f5, 0xffffff, 0xdcdcdc, 0xb4b4b4, 0x3c5a9c, 0xb1a3ff, 0x7aaae5, 0x9ce4f5,
0xbdf9ff, 0xbcf9d3, 0x61bb9d, 0xead999, 0xd19a70, 0x8e6d54, 0xd5a5b5 0xbdf9ff, 0xbcf9d3, 0x61bb9d, 0xead999, 0xd19a70, 0x8e6d54, 0xd5a5b5
}; };
int oldColors[28] = { static const COLORREF oldColors[NUM_COLORS] =
{
0x000000, 0x808080, 0x000080, 0x008080, 0x008000, 0x808000, 0x800000, 0x000000, 0x808080, 0x000080, 0x008080, 0x008000, 0x808000, 0x800000,
0x800080, 0x408080, 0x404000, 0xff8000, 0x804000, 0xff0040, 0x004080, 0x800080, 0x408080, 0x404000, 0xff8000, 0x804000, 0xff0040, 0x004080,
0xffffff, 0xc0c0c0, 0x0000ff, 0x00ffff, 0x00ff00, 0xffff00, 0xff0000, 0xffffff, 0xc0c0c0, 0x0000ff, 0x00ffff, 0x00ff00, 0xffff00, 0xff0000,
0xff00ff, 0x80ffff, 0x80ff00, 0xffff80, 0xff8080, 0x8000ff, 0x4080ff 0xff00ff, 0x80ffff, 0x80ff00, 0xffff80, 0xff8080, 0x8000ff, 0x4080ff
}; };
if (nPalette == 1) switch (nPalette)
CopyMemory(m_colors, modernColors, sizeof(m_colors)); {
else if (nPalette == 2) case PAL_MODERN:
CopyMemory(m_colors, oldColors, sizeof(m_colors)); CopyMemory(m_colors, modernColors, sizeof(m_colors));
else break;
return; case PAL_OLDTYPE:
CopyMemory(m_colors, oldColors, sizeof(m_colors));
break;
}
m_nSelectedPalette = nPalette; m_nSelectedPalette = nPalette;
NotifyPaletteChanged(); NotifyPaletteChanged();
} }
int PaletteModel::GetColor(int nIndex) const COLORREF PaletteModel::GetColor(UINT nIndex) const
{ {
if (nIndex < 28) if (nIndex < NUM_COLORS)
return m_colors[nIndex]; return m_colors[nIndex];
else else
return 0; return 0;
} }
void PaletteModel::SetColor(int nIndex, int newColor) void PaletteModel::SetColor(UINT nIndex, COLORREF newColor)
{ {
if (nIndex < 28) if (nIndex < NUM_COLORS)
{ {
m_colors[nIndex] = newColor; m_colors[nIndex] = newColor;
NotifyPaletteChanged(); NotifyPaletteChanged();
} }
} }
int PaletteModel::GetFgColor() const COLORREF PaletteModel::GetFgColor() const
{ {
return m_fgColor; return m_fgColor;
} }
void PaletteModel::SetFgColor(int newColor) void PaletteModel::SetFgColor(COLORREF newColor)
{ {
m_fgColor = newColor; m_fgColor = newColor;
NotifyColorChanged(); NotifyColorChanged();
} }
int PaletteModel::GetBgColor() const COLORREF PaletteModel::GetBgColor() const
{ {
return m_bgColor; return m_bgColor;
} }
void PaletteModel::SetBgColor(int newColor) void PaletteModel::SetBgColor(COLORREF newColor)
{ {
m_bgColor = newColor; m_bgColor = newColor;
NotifyColorChanged(); NotifyColorChanged();

View file

@ -8,27 +8,35 @@
#pragma once #pragma once
#define NUM_COLORS 28
enum PAL_TYPE
{
PAL_MODERN = 1,
PAL_OLDTYPE = 2,
};
/* CLASSES **********************************************************/ /* CLASSES **********************************************************/
class PaletteModel class PaletteModel
{ {
private: private:
int m_colors[28]; COLORREF m_colors[NUM_COLORS];
int m_nSelectedPalette; PAL_TYPE m_nSelectedPalette;
int m_fgColor; COLORREF m_fgColor;
int m_bgColor; COLORREF m_bgColor;
void NotifyColorChanged(); void NotifyColorChanged();
void NotifyPaletteChanged(); void NotifyPaletteChanged();
public: public:
PaletteModel(); PaletteModel();
int SelectedPalette(); PAL_TYPE SelectedPalette();
void SelectPalette(int nPalette); void SelectPalette(PAL_TYPE nPalette);
int GetColor(int nIndex) const; COLORREF GetColor(UINT nIndex) const;
void SetColor(int nIndex, int newColor); void SetColor(UINT nIndex, COLORREF newColor);
int GetFgColor() const; COLORREF GetFgColor() const;
void SetFgColor(int newColor); void SetFgColor(COLORREF newColor);
int GetBgColor() const; COLORREF GetBgColor() const;
void SetBgColor(int newColor); void SetBgColor(COLORREF newColor);
}; };

View file

@ -346,8 +346,8 @@ LRESULT CMainWindow::OnInitMenuPopup(UINT nMsg, WPARAM wParam, LPARAM lParam, BO
CheckMenuItem(menu, IDM_VIEWZOOM400, CHECKED_IF(toolsModel.GetZoom() == 4000)); CheckMenuItem(menu, IDM_VIEWZOOM400, CHECKED_IF(toolsModel.GetZoom() == 4000));
CheckMenuItem(menu, IDM_VIEWZOOM800, CHECKED_IF(toolsModel.GetZoom() == 8000)); CheckMenuItem(menu, IDM_VIEWZOOM800, CHECKED_IF(toolsModel.GetZoom() == 8000));
CheckMenuItem(menu, IDM_COLORSMODERNPALETTE, CHECKED_IF(paletteModel.SelectedPalette() == 1)); CheckMenuItem(menu, IDM_COLORSMODERNPALETTE, CHECKED_IF(paletteModel.SelectedPalette() == PAL_MODERN));
CheckMenuItem(menu, IDM_COLORSOLDPALETTE, CHECKED_IF(paletteModel.SelectedPalette() == 2)); CheckMenuItem(menu, IDM_COLORSOLDPALETTE, CHECKED_IF(paletteModel.SelectedPalette() == PAL_OLDTYPE));
return 0; return 0;
} }
@ -586,10 +586,10 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
paletteModel.SetFgColor(choosecolor.rgbResult); paletteModel.SetFgColor(choosecolor.rgbResult);
break; break;
case IDM_COLORSMODERNPALETTE: case IDM_COLORSMODERNPALETTE:
paletteModel.SelectPalette(1); paletteModel.SelectPalette(PAL_MODERN);
break; break;
case IDM_COLORSOLDPALETTE: case IDM_COLORSOLDPALETTE:
paletteModel.SelectPalette(2); paletteModel.SelectPalette(PAL_OLDTYPE);
break; break;
case IDM_IMAGEINVERTCOLORS: case IDM_IMAGEINVERTCOLORS:
{ {