Skip to content

Commit 697e06b

Browse files
committed
refactor(mfc): modernize PCH usage and replace legacy min/max helpers
- Replace __min/__max with std::min/std::max in ColorCopDlg - Add <algorithm> and tidy standard library includes - Remove redundant includes now provided by stdafx.h (windows.h, cstdint, cmath) - Centralize Windows config in stdafx.h (NOMINMAX, WIN32_LEAN_AND_MEAN) - Make feature flag bit shifts unsigned (1u << n) to avoid sign/overflow pitfalls - Replace PI macro with constexpr kPi and promote MULTIPIX_* to constexpr
1 parent dc0cae2 commit 697e06b

2 files changed

Lines changed: 81 additions & 59 deletions

File tree

ColorCopDlg.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@
1717
#include "Label.h" // used for the links in the AboutDlg
1818
#include "SystemTray.h" // used to minimize to the systray
1919

20-
// Windows headers
21-
#include <windows.h>
20+
// Windows SDK headers (explicit APIs used in this file)
2221
#include <commctrl.h>
2322
#include <htmlhelp.h>
2423

2524
// C++ standard library headers
26-
#include <cstdint>
27-
#include <random>
28-
#include <cmath> // pow, sqrt, atan (or atan2)
29-
#include <cstdlib> // strtoul, abs (int)
30-
#include <cstring> // memcpy
25+
#include <algorithm> // std::min, std::max
26+
#include <random> // std::mt19937, std::uniform_int_distribution
27+
#include <cstdlib> // strtoul, abs
28+
#include <cstring> // memcpy
3129

3230
constexpr int WEBSAFE_STEP = 51;
3331
constexpr int RGB_MIN = 0;
3432
constexpr int RGB_MAX = 255;
35-
constexpr double kPi = 3.14159265358979323846;
3633

3734
class CAboutDlg : public CDialog {
3835
public:
@@ -959,7 +956,7 @@ void CColorCopDlg::UpdateCMYKFromRGB(int red, int green, int blue) {
959956
m_Magenta = static_cast<int>(std::pow(1.0 - g, 45));
960957
m_Yellow = static_cast<int>(std::pow(1.0 - b, 45));
961958

962-
m_Black = __min(__min(m_Cyan, m_Magenta), m_Yellow);
959+
m_Black = std::min({ m_Cyan, m_Magenta, m_Yellow });
963960

964961
m_Cyan = m_Cyan - m_Black;
965962
m_Magenta = m_Magenta - m_Black;
@@ -975,10 +972,9 @@ void CColorCopDlg::RGBtoHSL(double R, double G, double B) {
975972
/////////////////////////////////////
976973
// get min, max, and diff
977974
//
978-
MinNum = __min(R, __min(G, B)); // min
979-
MaxNum = __max(R, __max(G, B)); // max
980-
981-
Diff = (MaxNum - MinNum); // diff
975+
MinNum = std::min({ R, G, B });
976+
MaxNum = std::max({ R, G, B });
977+
Diff = (MaxNum - MinNum);
982978

983979
if (Diff == 0) { // this is a greyscale color
984980
Diff = 5.0; // since greyscale colors don't have compliments,
@@ -995,7 +991,6 @@ void CColorCopDlg::RGBtoHSL(double R, double G, double B) {
995991
Sat = (static_cast<double>(Diff)) / (static_cast<double>(MaxNum));
996992
}
997993

998-
999994
// find the Hue
1000995
R_Dist = static_cast<double>((MaxNum) - R) / static_cast<double>((Diff));
1001996
G_Dist = static_cast<double>((MaxNum) - G) / static_cast<double>((Diff));
@@ -1010,7 +1005,6 @@ void CColorCopDlg::RGBtoHSL(double R, double G, double B) {
10101005
}
10111006
Hue = Hue / 6.0;
10121007

1013-
10141008
if (Hue < 0.0) {
10151009
Hue = (Hue + 1.0);
10161010
}
@@ -2223,11 +2217,9 @@ void CColorCopDlg::OnPopupColorConverttograyscale() {
22232217
// which is why the image appears as a grayscale.
22242218

22252219
// formula: L = {Max(R, G, B) + Min(R, G, B)}/2;
2226-
Max = __max(m_Reddec, m_Greendec);
2227-
Max = __max(Max, m_Bluedec);
2220+
Max = std::max({ m_Reddec, m_Greendec, m_Bluedec });
2221+
Min = std::min({ m_Reddec, m_Greendec, m_Bluedec });
22282222

2229-
Min = __min(m_Reddec, m_Greendec);
2230-
Min = __min(Min, m_Bluedec);
22312223
L = static_cast<int>((Min) + Max) / 2;
22322224

22332225
m_Reddec = L;

StdAfx.h

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,77 @@
33

44
#pragma once
55

6-
// Feature flags (bitmask values)
7-
#define AlwaysOnTop (1 << 2)
8-
#define UpperCaseHex (1 << 3)
9-
#define OmitPound (1 << 4)
10-
#define SnaptoWebsafe (1 << 5)
11-
#define AutoCopytoClip (1 << 6)
12-
#define MimimizetoTray (1 << 7)
13-
#define EasyMove (1 << 8)
14-
#define MinimizeonStart (1 << 9)
15-
#define ExpandedDialog (1 << 10)
16-
#define ModeHTML (1 << 11)
17-
#define ModeDelphi (1 << 12)
18-
#define ModePowerBuilder (1 << 13)
19-
#define ModeVisualBasic (1 << 14)
20-
#define ModeVisualC (1 << 15)
21-
#define Sampling1 (1 << 16)
22-
#define Sampling3x3 (1 << 17)
23-
#define Sampling5x5 (1 << 18)
24-
#define MultipleInstances (1 << 19)
25-
#define DetectWebsafeColors (1 << 20)
26-
#define RGBINT (1 << 21)
27-
#define RGBFLOAT (1 << 22)
28-
#define MAGWHILEEYEDROP (1 << 23)
29-
#define USECROSSHAIR (1 << 24)
30-
#define SETCURSORONEYEDROP (1 << 25)
31-
#define MULTIPIXELSAMPLE (1 << 26)
32-
#define SamplingMULTI (1 << 27)
33-
#define SpaceRGB (1 << 28)
34-
#define SpaceCMYK (1 << 29)
35-
#define ModeClarion (1 << 30)
36-
37-
#define MULTIPIX_MIN 1
38-
#define MULTIPIX_MAX 15
39-
40-
#define PI (3.1415926535897932384626433832795)
41-
42-
// MFC includes
43-
#include <afxwin.h>
44-
#include <afxext.h>
6+
//------------------------------------------------------------------------------
7+
// Platform / Windows configuration
8+
//------------------------------------------------------------------------------
9+
10+
#define NOMINMAX // Prevent Windows macros from breaking std::min/std::max
11+
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used Windows headers
12+
13+
//------------------------------------------------------------------------------
14+
// Standard library
15+
//------------------------------------------------------------------------------
16+
17+
#include <cstdint>
18+
#include <cmath>
19+
20+
//------------------------------------------------------------------------------
21+
// Application feature flags (bitmask)
22+
//------------------------------------------------------------------------------
23+
//
24+
// NOTE:
25+
// These are intentionally kept as macros for minimal churn.
26+
// A future modernization step would be a strongly-typed enum class.
27+
//
28+
29+
#define AlwaysOnTop (1u << 2)
30+
#define UpperCaseHex (1u << 3)
31+
#define OmitPound (1u << 4)
32+
#define SnaptoWebsafe (1u << 5)
33+
#define AutoCopytoClip (1u << 6)
34+
#define MimimizetoTray (1u << 7)
35+
#define EasyMove (1u << 8)
36+
#define MinimizeonStart (1u << 9)
37+
#define ExpandedDialog (1u << 10)
38+
#define ModeHTML (1u << 11)
39+
#define ModeDelphi (1u << 12)
40+
#define ModePowerBuilder (1u << 13)
41+
#define ModeVisualBasic (1u << 14)
42+
#define ModeVisualC (1u << 15)
43+
#define Sampling1 (1u << 16)
44+
#define Sampling3x3 (1u << 17)
45+
#define Sampling5x5 (1u << 18)
46+
#define MultipleInstances (1u << 19)
47+
#define DetectWebsafeColors (1u << 20)
48+
#define RGBINT (1u << 21)
49+
#define RGBFLOAT (1u << 22)
50+
#define MAGWHILEEYEDROP (1u << 23)
51+
#define USECROSSHAIR (1u << 24)
52+
#define SETCURSORONEYEDROP (1u << 25)
53+
#define MULTIPIXELSAMPLE (1u << 26)
54+
#define SamplingMULTI (1u << 27)
55+
#define SpaceRGB (1u << 28)
56+
#define SpaceCMYK (1u << 29)
57+
#define ModeClarion (1u << 30)
58+
59+
//------------------------------------------------------------------------------
60+
// Constants
61+
//------------------------------------------------------------------------------
62+
63+
constexpr int MULTIPIX_MIN = 1;
64+
constexpr int MULTIPIX_MAX = 15;
65+
66+
// Prefer constexpr over macros for typed constants
67+
constexpr double kPi = 3.14159265358979323846;
68+
69+
//------------------------------------------------------------------------------
70+
// MFC headers
71+
//------------------------------------------------------------------------------
72+
73+
#include <afxwin.h> // Core MFC components
74+
#include <afxext.h> // Extensions (controls, OLE)
4575
#ifndef _AFX_NO_AFXCMN_SUPPORT
46-
#include <afxcmn.h>
76+
#include <afxcmn.h> // Common controls
4777
#endif
4878

4979
//{{AFX_INSERT_LOCATION}} // NOLINT(whitespace/comments)

0 commit comments

Comments
 (0)