aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/core/math/color.h
blob: 013c4e4cace5da03ba35686fb6b7a527bbad2add (plain)
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
#pragma once

struct color24
{
	uint8_t r, g, b;
};

typedef struct color32_s
{
	bool operator!=(const struct color32_s& other) const
	{
		return r != other.r || g != other.g || b != other.b || a != other.a;
	}
	inline unsigned* asInt(void)
	{
		return reinterpret_cast<unsigned*>(this);
	}
	inline const unsigned* asInt(void) const
	{
		return reinterpret_cast<const unsigned*>(this);
	}
	inline void Copy(const color32_s& rhs)
	{
		*asInt() = *rhs.asInt();
	}

	uint8_t r, g, b, a;
} color32;

struct SourceColor
{
	unsigned char R;
	unsigned char G;
	unsigned char B;
	unsigned char A;

	SourceColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
	{
		R = r;
		G = g;
		B = b;
		A = a;
	}

	SourceColor()
	{
		R = 0;
		G = 0;
		B = 0;
		A = 0;
	}
};

//-----------------------------------------------------------------------------
// Purpose: Basic handler for an rgb set of colors
//			This class is fully inline
//-----------------------------------------------------------------------------
class Color
{
public:
	Color(int r, int g, int b, int a = 255)
	{
		_color[0] = (unsigned char)r;
		_color[1] = (unsigned char)g;
		_color[2] = (unsigned char)b;
		_color[3] = (unsigned char)a;
	}
	void SetColor(int _r, int _g, int _b, int _a = 0)
	{
		_color[0] = (unsigned char)_r;
		_color[1] = (unsigned char)_g;
		_color[2] = (unsigned char)_b;
		_color[3] = (unsigned char)_a;
	}
	void GetColor(int& _r, int& _g, int& _b, int& _a) const
	{
		_r = _color[0];
		_g = _color[1];
		_b = _color[2];
		_a = _color[3];
	}
	int GetValue(int index) const
	{
		return _color[index];
	}
	void SetRawColor(int color32)
	{
		*((int*)this) = color32;
	}
	int GetRawColor(void) const
	{
		return *((int*)this);
	}

	inline int r() const
	{
		return _color[0];
	}
	inline int g() const
	{
		return _color[1];
	}
	inline int b() const
	{
		return _color[2];
	}
	inline int a() const
	{
		return _color[3];
	}

	unsigned char& operator[](int index)
	{
		return _color[index];
	}

	const unsigned char& operator[](int index) const
	{
		return _color[index];
	}

	bool operator==(const Color& rhs) const
	{
		return (*((int*)this) == *((int*)&rhs));
	}

	bool operator!=(const Color& rhs) const
	{
		return !(operator==(rhs));
	}

	Color& operator=(const Color& rhs)
	{
		SetRawColor(rhs.GetRawColor());
		return *this;
	}

	Color& operator=(const color32& rhs)
	{
		_color[0] = rhs.r;
		_color[1] = rhs.g;
		_color[2] = rhs.b;
		_color[3] = rhs.a;
		return *this;
	}

	color32 ToColor32(void) const
	{
		color32 newColor {};
		newColor.r = _color[0];
		newColor.g = _color[1];
		newColor.b = _color[2];
		newColor.a = _color[3];
		return newColor;
	}

	std::string ToANSIColor()
	{
		std::string out = "\033[38;2;";
		out += std::to_string(_color[0]) + ";";
		out += std::to_string(_color[1]) + ";";
		out += std::to_string(_color[2]) + ";";
		out += "49m";
		return out;
	}

	SourceColor ToSourceColor()
	{
		return SourceColor(_color[0], _color[1], _color[2], _color[3]);
	}

private:
	unsigned char _color[4];
};

namespace NS::Colors
{
	extern Color SCRIPT_UI;
	extern Color SCRIPT_CL;
	extern Color SCRIPT_SV;
	extern Color NATIVE_UI;
	extern Color NATIVE_CL;
	extern Color NATIVE_SV;
	extern Color NATIVE_ENGINE;
	extern Color FILESYSTEM;
	extern Color RPAK;
	extern Color NORTHSTAR;
	extern Color ECHO;
	extern Color PLUGINSYS;
	extern Color PLUGIN;

	extern Color TRACE;
	extern Color DEBUG;
	extern Color INFO;
	extern Color WARN;
	extern Color ERR;
	extern Color CRIT;
	extern Color OFF;
}; // namespace NS::Colors