Skip to content

Commit 17a5132

Browse files
committed
Little refactoring & optimizations
1 parent fcdb636 commit 17a5132

File tree

12 files changed

+491
-189
lines changed

12 files changed

+491
-189
lines changed

AccentColorizer/AccentColorHelper.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
#include "ColorHelper.h"
33

44
COLORREF g_dwAccent;
5+
int g_hsvAccentH;
56

6-
bool UpdateAccentColors()
7+
bool UpdateAccentColor()
78
{
89
COLORREF dwAccentRGB;
910
BOOL bIsColorOpaque;
1011

1112
DwmGetColorizationColor(&dwAccentRGB, &bIsColorOpaque);
1213

13-
DWORD dwAccent = RGB2BGR(dwAccentRGB);
14+
DWORD dwAccent = rgb2bgr(dwAccentRGB);
1415

1516
if (g_dwAccent == dwAccent)
1617
{
1718
return false;
1819
}
1920

2021
g_dwAccent = dwAccent;
22+
g_hsvAccentH = rgb2hsv({
23+
(double) GetRValue(dwAccent) / 255,
24+
(double) GetGValue(dwAccent) / 255,
25+
(double) GetBValue(dwAccent) / 255 }).h;
26+
2127
return true;
2228
}

AccentColorizer/AccentColorHelper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#include "framework.h"
33

44
extern COLORREF g_dwAccent;
5+
extern int g_hsvAccentH;
56

6-
bool UpdateAccentColors();
7+
bool UpdateAccentColor();

AccentColorizer/BitmapHelper.cpp

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
11
#include "BitmapHelper.h"
22

3-
#pragma region Pixel Color
4-
inline int PixClr(int val)
3+
bool IterateBitmap(HBITMAP hbm, BitmapPixelHandler handler)
54
{
6-
return val & 0xFFFFFF;
7-
}
8-
9-
inline int PixR(BYTE* pPixel)
10-
{
11-
return PixClr(pPixel[2]);
12-
}
13-
inline int PixG(BYTE* pPixel)
14-
{
15-
return PixClr(pPixel[1]);
16-
}
17-
inline int PixB(BYTE* pPixel)
18-
{
19-
return PixClr(pPixel[0]);
20-
}
21-
inline int PixA(BYTE* pPixel)
22-
{
23-
return PixClr(pPixel[3]);
24-
}
25-
#pragma endregion
26-
27-
bool RecolorizeBitmap(HBITMAP hbm, BitmapHandler handler) {
285
BITMAP bm;
296
GetObject(hbm, sizeof(bm), &bm);
307

@@ -36,16 +13,20 @@ bool RecolorizeBitmap(HBITMAP hbm, BitmapHandler handler) {
3613
BYTE* pBits = new BYTE[bm.bmWidth * bm.bmHeight * 4];
3714
GetBitmapBits(hbm, bm.bmWidth * bm.bmHeight * 4, pBits);
3815

39-
for (int y = 0; y < bm.bmHeight; y++)
16+
BYTE* pPixel;
17+
int x, y;
18+
int r, g, b, a;
19+
20+
for (y = 0; y < bm.bmHeight; y++)
4021
{
41-
BYTE* pPixel = pBits + bm.bmWidth * 4 * y;
22+
pPixel = pBits + bm.bmWidth * 4 * y;
4223

43-
for (int x = 0; x < bm.bmWidth; x++)
24+
for (x = 0; x < bm.bmWidth; x++)
4425
{
45-
int r = PixR(pPixel); // [2]
46-
int g = PixG(pPixel); // [1]
47-
int b = PixB(pPixel); // [0]
48-
int a = PixA(pPixel); // [3]
26+
r = pPixel[2] & 0xFFFFFF;
27+
g = pPixel[1] & 0xFFFFFF;
28+
b = pPixel[0] & 0xFFFFFF;
29+
a = pPixel[3] & 0xFFFFFF;
4930

5031
handler(r, g, b, a);
5132

AccentColorizer/BitmapHelper.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "framework.h"
33

4-
typedef void (*BitmapHandler)(int& r, int& g, int& b, int& a);
5-
bool RecolorizeBitmap(HBITMAP hbm, BitmapHandler handler);
4+
typedef void (*BitmapPixelHandler)(int& r, int& g, int& b, int& a);
5+
6+
bool IterateBitmap(HBITMAP hbm, BitmapPixelHandler handler);

AccentColorizer/ColorHelper.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#include "ColorHelper.h"
22

3+
DWORD rgb2bgr(COLORREF color)
4+
{
5+
return (color & 0xFF000000) | ((color & 0xFF0000) >> 16) | (color & 0x00FF00) | ((color & 0x0000FF) << 16);
6+
}
7+
8+
39
// https://stackoverflow.com/a/6930407
410

5-
hsv rgb2hsv(rgb in)
11+
hsv_t rgb2hsv(rgb_t in)
612
{
7-
hsv out;
13+
hsv_t out;
814
double min, max, delta;
915

1016
min = in.r < in.g ? in.r : in.g;
@@ -47,11 +53,11 @@ hsv rgb2hsv(rgb in)
4753
return out;
4854
}
4955

50-
rgb hsv2rgb(hsv in)
56+
rgb_t hsv2rgb(hsv_t in)
5157
{
5258
double hh, p, q, t, ff;
5359
long i;
54-
rgb out;
60+
rgb_t out;
5561

5662
if (in.s <= 0.0) { // < is bogus, just shuts up warnings
5763
out.r = in.v;
@@ -103,15 +109,4 @@ rgb hsv2rgb(hsv in)
103109
break;
104110
}
105111
return out;
106-
}
107-
108-
int GetHSVh(COLORREF dwColor)
109-
{
110-
return rgb2hsv(
111-
{
112-
(double)GetRValue(dwColor),
113-
(double)GetGValue(dwColor),
114-
(double)GetBValue(dwColor)
115-
}
116-
).h;
117112
}

AccentColorizer/ColorHelper.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
#pragma once
22
#include "framework.h"
33

4-
typedef struct {
5-
double r; // a fraction between 0 and 1
6-
double g; // a fraction between 0 and 1
7-
double b; // a fraction between 0 and 1
8-
} rgb;
4+
DWORD rgb2bgr(COLORREF rgb);
95

10-
typedef struct {
11-
double h; // angle in degrees
12-
double s; // a fraction between 0 and 1
13-
double v; // a fraction between 0 and 1
14-
} hsv;
6+
struct rgb_t
7+
{
8+
double r, g, b;
9+
};
1510

16-
hsv rgb2hsv(rgb in);
17-
rgb hsv2rgb(hsv in);
11+
struct hsv_t
12+
{
13+
double h, s, v;
14+
};
1815

19-
int GetHSVh(COLORREF dwColor);
20-
21-
#define RGB2BGR(color) (color & 0xFF000000) | ((color & 0xFF0000) >> 16) | (color & 0x00FF00) | ((color & 0x0000FF) << 16)
16+
hsv_t rgb2hsv(rgb_t in);
17+
rgb_t hsv2rgb(hsv_t in);

AccentColorizer/SettingsHelper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "SettingsHelper.h"
22
#include "SystemHelper.h"
3+
34
#include <VersionHelpers.h>
45

56
bool IsRegistryValueEnabled(LPCWSTR key, LPCWSTR value)

0 commit comments

Comments
 (0)