-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_color.h
More file actions
215 lines (204 loc) · 4.86 KB
/
basic_color.h
File metadata and controls
215 lines (204 loc) · 4.86 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
/*
* Author: Jaime Rivera
* Date : 2020.04.20
* Copyright : Copyright 2020 Jaime Rivera | www.jaimervq.com
* Brief: Basic implementation of a rgba color
*/
#include <algorithm>
#include <string>
// --------- BASIC COLOR --------- //
class BasicColor
{
private:
double r, g, b, a;
public:
// Constructors
BasicColor() : r(0.0), g(0.0), b(0.0), a(0.0) {}
BasicColor(double input_r, double input_g, double input_b) : r(input_r), g(input_g), b(input_b), a(1.0) {}
BasicColor(double input_r, double input_g, double input_b, double input_a) : r(input_r), g(input_g), b(input_b), a(input_a) {}
// Predefined colors
static const BasicColor White;
static const BasicColor Black;
static const BasicColor Red;
static const BasicColor Green;
static const BasicColor Blue;
static const BasicColor Cyan;
static const BasicColor Magenta;
static const BasicColor Yellow;
// Operators
BasicColor operator+(const BasicColor &right)
{
double new_r = this->r + right.r;
double new_g = this->g + right.g;
double new_b = this->b + right.b;
double new_a = this->a + right.a;
return BasicColor{new_r, new_g, new_b, new_a};
}
BasicColor operator-(const BasicColor &right)
{
double new_r = this->r - right.r;
double new_g = this->g - right.g;
double new_b = this->b - right.b;
double new_a = this->a - right.a;
return BasicColor{new_r, new_g, new_b, new_a};
}
BasicColor operator*(const BasicColor &right)
{
double new_r, new_g, new_b, new_a;
if (this->r < 0.0 && right.r < 0.0)
new_r = this->r;
else
new_r = this->r * right.r;
if (this->g < 0.0 && right.g < 0.0)
new_g = this->g;
else
new_g = this->g * right.g;
if (this->b < 0.0 && right.b < 0.0)
new_b = this->b;
else
new_b = this->b * right.b;
if (this->a < 0.0 && right.a < 0.0)
new_a = this->a;
else
new_a = this->a * right.a;
return BasicColor{new_r, new_g, new_b, new_a};
}
// Blend modes and IDs
BasicColor plus(const BasicColor &right)
{
return *this + right;
}
BasicColor minus(const BasicColor &right)
{
return *this - right;
}
BasicColor multiply(const BasicColor &right)
{
return *this * right;
}
BasicColor over(const BasicColor &right)
{
double new_r = this->r + (right.r * (1.0 - this->a));
double new_g = this->g + (right.g * (1.0 - this->a));
double new_b = this->b + (right.b * (1.0 - this->a));
double new_a = this->a + (right.a * (1.0 - this->a)); // Todo revisit this
return BasicColor{new_r, new_g, new_b, new_a};
}
BasicColor unpremultiplied_over(const BasicColor &right)
{
double new_r = (this->r * this->a) + (right.r * (1.0 - this->a));
double new_g = (this->g * this->a) + (right.g * (1.0 - this->a));
double new_b = (this->b * this->a) + (right.b * (1.0 - this->a));
double new_a = std::max(this->a, right.a); // Todo revisit this
return BasicColor{new_r, new_g, new_b, new_a};
}
static const int plus_ID{0};
static const int minus_ID{1};
static const int multiply_ID{2};
static const int over_ID{3};
static const int unpremultiplied_over_ID{4};
// Get
double get_r() { return r; }
double get_g() { return g; }
double get_b() { return b; }
double get_a() { return a; }
// Get (clipped)
double r01()
{
if (r < 0.0)
return 0.0;
else if (r > 1.0)
return 1.0;
return r;
}
double g01()
{
if (g < 0.0)
return 0.0;
else if (g > 1.0)
return 1.0;
return g;
}
double b01()
{
if (b < 0.0)
return 0.0;
else if (b > 1.0)
return 1.0;
return b;
}
double a01()
{
if (a < 0.0)
return 0.0;
else if (a > 1.0)
return 1.0;
return a;
}
int r255()
{
if (r < 0.0)
return 0;
else if (r > 1.0)
return 255;
return (int)(r * 255);
}
int g255()
{
if (g < 0.0)
return 0;
else if (g > 1.0)
return 255;
return (int)(g * 255);
}
int b255()
{
if (b < 0.0)
return 0;
else if (b > 1.0)
return 255;
return (int)(b * 255);
}
int a255()
{
if (a < 0.0)
return 0;
else if (a > 1.0)
return 255;
return (int)(a * 255);
}
};
const BasicColor BasicColor::White{1.0, 1.0, 1.0, 1.0};
const BasicColor BasicColor::Black{0.0, 0.0, 0.0, 1.0};
const BasicColor BasicColor::Red{1.0, 0.0, 0.0, 1.0};
const BasicColor BasicColor::Cyan{0.0, 1.0, 1.0, 1.0};
const BasicColor BasicColor::Green{0.0, 1.0, 0.0, 1.0};
const BasicColor BasicColor::Magenta{1.0, 0.0, 1.0, 1.0};
const BasicColor BasicColor::Blue{0.0, 0.0, 1.0, 1.0};
const BasicColor BasicColor::Yellow{1.0, 1.0, 0.0, 1.0};
// --------- GENERAL BLEND --------- //
BasicColor blend_two_colors(BasicColor A, int blend_type_ID, BasicColor B)
{
switch (blend_type_ID)
{
case BasicColor::plus_ID:
return A.plus(B);
break;
case BasicColor::minus_ID:
return A.minus(B);
break;
case BasicColor::multiply_ID:
return A.multiply(B);
break;
case BasicColor::over_ID:
return A.over(B);
break;
case BasicColor::unpremultiplied_over_ID:
return A.unpremultiplied_over(B);
break;
default:
return A.plus(B);
break;
}
}