Skip to content

Commit

Permalink
Little refactoring & optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvm committed Aug 7, 2023
1 parent fcdb636 commit 17a5132
Show file tree
Hide file tree
Showing 12 changed files with 491 additions and 189 deletions.
10 changes: 8 additions & 2 deletions AccentColorizer/AccentColorHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
#include "ColorHelper.h"

COLORREF g_dwAccent;
int g_hsvAccentH;

bool UpdateAccentColors()
bool UpdateAccentColor()
{
COLORREF dwAccentRGB;
BOOL bIsColorOpaque;

DwmGetColorizationColor(&dwAccentRGB, &bIsColorOpaque);

DWORD dwAccent = RGB2BGR(dwAccentRGB);
DWORD dwAccent = rgb2bgr(dwAccentRGB);

if (g_dwAccent == dwAccent)
{
return false;
}

g_dwAccent = dwAccent;
g_hsvAccentH = rgb2hsv({
(double) GetRValue(dwAccent) / 255,
(double) GetGValue(dwAccent) / 255,
(double) GetBValue(dwAccent) / 255 }).h;

return true;
}
3 changes: 2 additions & 1 deletion AccentColorizer/AccentColorHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#include "framework.h"

extern COLORREF g_dwAccent;
extern int g_hsvAccentH;

bool UpdateAccentColors();
bool UpdateAccentColor();
43 changes: 12 additions & 31 deletions AccentColorizer/BitmapHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
#include "BitmapHelper.h"

#pragma region Pixel Color
inline int PixClr(int val)
bool IterateBitmap(HBITMAP hbm, BitmapPixelHandler handler)
{
return val & 0xFFFFFF;
}

inline int PixR(BYTE* pPixel)
{
return PixClr(pPixel[2]);
}
inline int PixG(BYTE* pPixel)
{
return PixClr(pPixel[1]);
}
inline int PixB(BYTE* pPixel)
{
return PixClr(pPixel[0]);
}
inline int PixA(BYTE* pPixel)
{
return PixClr(pPixel[3]);
}
#pragma endregion

bool RecolorizeBitmap(HBITMAP hbm, BitmapHandler handler) {
BITMAP bm;
GetObject(hbm, sizeof(bm), &bm);

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

for (int y = 0; y < bm.bmHeight; y++)
BYTE* pPixel;
int x, y;
int r, g, b, a;

for (y = 0; y < bm.bmHeight; y++)
{
BYTE* pPixel = pBits + bm.bmWidth * 4 * y;
pPixel = pBits + bm.bmWidth * 4 * y;

for (int x = 0; x < bm.bmWidth; x++)
for (x = 0; x < bm.bmWidth; x++)
{
int r = PixR(pPixel); // [2]
int g = PixG(pPixel); // [1]
int b = PixB(pPixel); // [0]
int a = PixA(pPixel); // [3]
r = pPixel[2] & 0xFFFFFF;
g = pPixel[1] & 0xFFFFFF;
b = pPixel[0] & 0xFFFFFF;
a = pPixel[3] & 0xFFFFFF;

handler(r, g, b, a);

Expand Down
5 changes: 3 additions & 2 deletions AccentColorizer/BitmapHelper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "framework.h"

typedef void (*BitmapHandler)(int& r, int& g, int& b, int& a);
bool RecolorizeBitmap(HBITMAP hbm, BitmapHandler handler);
typedef void (*BitmapPixelHandler)(int& r, int& g, int& b, int& a);

bool IterateBitmap(HBITMAP hbm, BitmapPixelHandler handler);
25 changes: 10 additions & 15 deletions AccentColorizer/ColorHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "ColorHelper.h"

DWORD rgb2bgr(COLORREF color)
{
return (color & 0xFF000000) | ((color & 0xFF0000) >> 16) | (color & 0x00FF00) | ((color & 0x0000FF) << 16);
}


// https://stackoverflow.com/a/6930407

hsv rgb2hsv(rgb in)
hsv_t rgb2hsv(rgb_t in)
{
hsv out;
hsv_t out;
double min, max, delta;

min = in.r < in.g ? in.r : in.g;
Expand Down Expand Up @@ -47,11 +53,11 @@ hsv rgb2hsv(rgb in)
return out;
}

rgb hsv2rgb(hsv in)
rgb_t hsv2rgb(hsv_t in)
{
double hh, p, q, t, ff;
long i;
rgb out;
rgb_t out;

if (in.s <= 0.0) { // < is bogus, just shuts up warnings
out.r = in.v;
Expand Down Expand Up @@ -103,15 +109,4 @@ rgb hsv2rgb(hsv in)
break;
}
return out;
}

int GetHSVh(COLORREF dwColor)
{
return rgb2hsv(
{
(double)GetRValue(dwColor),
(double)GetGValue(dwColor),
(double)GetBValue(dwColor)
}
).h;
}
26 changes: 11 additions & 15 deletions AccentColorizer/ColorHelper.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#pragma once
#include "framework.h"

typedef struct {
double r; // a fraction between 0 and 1
double g; // a fraction between 0 and 1
double b; // a fraction between 0 and 1
} rgb;
DWORD rgb2bgr(COLORREF rgb);

typedef struct {
double h; // angle in degrees
double s; // a fraction between 0 and 1
double v; // a fraction between 0 and 1
} hsv;
struct rgb_t
{
double r, g, b;
};

hsv rgb2hsv(rgb in);
rgb hsv2rgb(hsv in);
struct hsv_t
{
double h, s, v;
};

int GetHSVh(COLORREF dwColor);

#define RGB2BGR(color) (color & 0xFF000000) | ((color & 0xFF0000) >> 16) | (color & 0x00FF00) | ((color & 0x0000FF) << 16)
hsv_t rgb2hsv(rgb_t in);
rgb_t hsv2rgb(hsv_t in);
1 change: 1 addition & 0 deletions AccentColorizer/SettingsHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "SettingsHelper.h"
#include "SystemHelper.h"

#include <VersionHelpers.h>

bool IsRegistryValueEnabled(LPCWSTR key, LPCWSTR value)
Expand Down
Loading

0 comments on commit 17a5132

Please sign in to comment.