forked from Hexagenic/wanikaniwallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.cpp
162 lines (143 loc) · 2.61 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "color.hpp"
uint8_t hexToInt(char hex)
{
switch(hex)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
default:
return 0;
}
}
namespace wanikani
{
Color::Color()
: a_(0xFF)
, b_(0x00)
, g_(0x00)
, r_(0x00)
{
}
Color::Color(const std::string hexcode)
{
uint32_t abgr = hexToABGR(hexcode);
ABGRToComp(abgr, r_, g_, b_, a_);
}
Color::Color(uint32_t abgr)
{
ABGRToComp(abgr, r_, g_, b_, a_);
}
Color::Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
: r_(red)
, g_(green)
, b_(blue)
, a_(alpha)
{
}
Color::Color(const Color &other)
: r_(other.r_)
, g_(other.g_)
, b_(other.b_)
, a_(other.a_)
{
}
Color Color::operator+(const Color& other) const
{
return Color(
r_ + other.r_,
g_ + other.g_,
b_ + other.b_,
a_ + other.a_);
}
Color Color::operator*(const Color& other) const
{
return Color(
(r_ * other.r_) / 0xFF,
(g_ * other.g_) / 0xFF,
(b_ * other.b_) / 0xFF,
(a_ * other.a_) / 0xFF);
}
Color& Color::operator*=(const Color& other)
{
r_ = (r_ * other.r_) / 0xFF;
g_ = (g_ * other.g_) / 0xFF;
b_ = (b_ * other.b_) / 0xFF;
a_ = (a_ * other.a_) / 0xFF;
return *this;
}
Color Color::operator*(uint8_t scalar) const
{
return Color(
(r_ * scalar) / 0xFF,
(g_ * scalar) / 0xFF,
(b_ * scalar) / 0xFF,
(a_ * scalar) / 0xFF);
}
const uint32_t Color::ABGR() const
{
return compToABGR(r_, g_, b_, a_);
}
void Color::ABGR(uint32_t abgr)
{
ABGRToComp(abgr, r_, g_, b_, a_);
}
uint32_t Color::compToABGR(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
return (alpha<<24) + (blue<<16) + (green<<8) + red;
}
uint32_t Color::hexToABGR(const std::string hexcode)
{
if(hexcode.size() == 8 && hexcode[0] == '0' && hexcode[1] == 'x')
{
uint8_t red = (hexToInt(hexcode[2])<<4) + hexToInt(hexcode[3]);
uint8_t green = (hexToInt(hexcode[4])<<4) + hexToInt(hexcode[5]);
uint8_t blue = (hexToInt(hexcode[6])<<4) + hexToInt(hexcode[7]);
return compToABGR(red, green, blue);
}
else
return 0xFFFF00FF;
}
void Color::ABGRToComp(uint32_t abgr, uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha)
{
alpha = (abgr >> 24) & 0xFF;
blue = (abgr >> 16) & 0xFF;
green = (abgr >> 8) & 0xFF;
red = (abgr >> 0) & 0xFF;
}
}