Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Color/RGBAUtil.cpp
5654 views
1
#include "Common/Data/Color/RGBAUtil.h"
2
3
template <typename T>
4
static T clamp(T f, T low, T high) {
5
if (f < low)
6
return low;
7
if (f > high)
8
return high;
9
return f;
10
}
11
12
uint32_t whiteAlpha(float alpha) {
13
if (alpha < 0.0f) alpha = 0.0f;
14
if (alpha > 1.0f) alpha = 1.0f;
15
uint32_t color = (int)(alpha*255) << 24;
16
color |= 0xFFFFFF;
17
return color;
18
}
19
20
uint32_t blackAlpha(float alpha) {
21
if (alpha < 0.0f) alpha = 0.0f;
22
if (alpha > 1.0f) alpha = 1.0f;
23
return (int)(alpha*255)<<24;
24
}
25
26
uint32_t colorAlpha(uint32_t rgb, float alpha) {
27
if (alpha < 0.0f) alpha = 0.0f;
28
if (alpha > 1.0f) alpha = 1.0f;
29
return ((int)(alpha*255)<<24) | (rgb & 0xFFFFFF);
30
}
31
32
uint32_t colorBlend(uint32_t rgb1, uint32_t rgb2, float alpha) {
33
const float invAlpha = (1.0f - alpha);
34
int r = (int)(((rgb1 >> 0) & 0xFF) * alpha + ((rgb2 >> 0) & 0xFF) * invAlpha);
35
int g = (int)(((rgb1 >> 8) & 0xFF) * alpha + ((rgb2 >> 8) & 0xFF) * invAlpha);
36
int b = (int)(((rgb1 >> 16) & 0xFF) * alpha + ((rgb2 >> 16) & 0xFF) * invAlpha);
37
int a = (int)(((rgb1 >> 24) & 0xFF) * alpha + ((rgb2 >> 24) & 0xFF) * invAlpha);
38
39
uint32_t c = clamp(a, 0, 255) << 24;
40
c |= clamp(b, 0, 255) << 16;
41
c |= clamp(g, 0, 255) << 8;
42
c |= clamp(r, 0, 255);
43
return c;
44
}
45
46
uint32_t colorAdd(uint32_t color, uint32_t color2) {
47
const int r = (int)((color >> 0) & 0xFF) + (int)((color2 >> 0) & 0xFF);
48
const int g = (int)((color >> 8) & 0xFF) + (int)((color2 >> 8) & 0xFF);
49
const int b = (int)((color >> 16) & 0xFF) + (int)((color2 >> 16) & 0xFF);
50
const int a = (int)((color >> 24) & 0xFF) + (int)((color2 >> 24) & 0xFF);
51
uint32_t c = clamp(a, 0, 255) << 24;
52
c |= clamp(b, 0, 255) << 16;
53
c |= clamp(g, 0, 255) << 8;
54
c |= clamp(r, 0, 255);
55
return c;
56
}
57
58
uint32_t alphaMul(uint32_t color, float alphaMul) {
59
const uint32_t rgb = color & 0xFFFFFF;
60
int32_t alpha = color >> 24;
61
alpha = (int32_t)(alpha * alphaMul);
62
if (alpha < 0)
63
alpha = 0;
64
if (alpha > 255)
65
alpha = 255;
66
return (alpha << 24) | rgb;
67
}
68
69
uint32_t rgba(float r, float g, float b, float alpha) {
70
uint32_t color = (int)(alpha * 255.0f) << 24;
71
color |= (int)(b * 255.0f) << 16;
72
color |= (int)(g * 255.0f) << 8;
73
color |= (int)(r * 255.0f);
74
return color;
75
}
76
77
uint32_t rgba_clamp(float r, float g, float b, float a) {
78
return rgba(clamp(r, 0.0f, 1.0f), clamp(g, 0.0f, 1.0f), clamp(b, 0.0f, 1.0f), clamp(a, 0.0f, 1.0f));
79
}
80
81