forked from keizi666/charu3
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
color.cpp
65 lines (54 loc) · 1.68 KB
/
color.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <string>
#include <regex>
#include <vector>
#include "color.h"
uint32_t Color::Parse(const std::string str)
{
std::smatch m;
if (std::regex_match(str, m, std::regex("^\\s*#([0-9a-fA-F]{3})\\s*$"))) {
long rgb = std::stol(m[1].str(), nullptr, 16);
long r = (rgb >> 8) & 0x0f;
long g = (rgb >> 4) & 0x0f;
long b = rgb & 0x0f;
return static_cast<uint32_t>((((r << 4) | r) << 16) | (((g << 4) | g) << 8) | ((b << 4) | b));
}
else if (std::regex_match(str, m, std::regex("^\\s*#([0-9a-fA-F]{6})\\s*$"))) {
return static_cast<uint32_t>(std::stol(m[1].str(), nullptr, 16));
}
return 0;
}
std::string Color::String(uint32_t color)
{
const size_t len = 7; // #rrggbb
const size_t size = len + 1; // +1 for terminater (nul)
std::vector<char> buf(size);
std::snprintf(&buf[0], size, "#%.6x", color);
return std::string(&buf[0], &buf[0] + len);
}
COLORREF Color::Swap_RGB_BGR(COLORREF color)
{
COLORREF HighToLow = (0x00ff0000 & color) >> 16;
COLORREF G = 0x0000ff00 & color;
COLORREF LowToHigh = (0x000000ff & color) << 16;
return LowToHigh | G | HighToLow;
}
COLORREF Color::MultiplyBrightness(COLORREF color, double brightness)
{
COLORREF R, G, B;
R = (0x00ff0000 & color) >> 16;
R = (COLORREF)(R * brightness);
if (R > 0xff) R = 0xff;
G = (0x0000ff00 & color) >> 8;
G = (COLORREF)(G * brightness);
if (G > 0xff) G = 0xff;
B = 0x000000ff & color;
B = (COLORREF)(B * brightness);
if (B > 0xff) B = 0xff;
return (R << 16) | (G << 8) | B;
}