ChatPlexSDK-BS 1.0.1-v6.2.0
C++ ChatPlex BeatSaber SDK
Loading...
Searching...
No Matches
ColorU.hpp
1#pragma once
2
3#include <UnityEngine/Color.hpp>
4#include <UnityEngine/Color32.hpp>
5
7
8 namespace _u
9 {
10 using namespace UnityEngine;
11 }
12 namespace _v
13 {
14 using namespace CP_SDK::Utils;
15 }
16
18 class ColorU
19 {
20 public:
24 inline static _u::Color WithAlpha(_u::Color p_Color, float p_Alpha)
25 {
26 p_Color.a = p_Alpha;
27 return p_Color;
28 }
32 inline static _u::Color WithAlpha(const char* p_Color, float p_Alpha)
33 {
34 return WithAlpha(ToUnityColor(p_Color), p_Alpha);
35 }
36
37 public:
41 inline static _u::Color Convert(_u::Color32 p_Src)
42 {
43 return _u::Color(
44 static_cast<float>(p_Src.r) / 255.0f,
45 static_cast<float>(p_Src.g) / 255.0f,
46 static_cast<float>(p_Src.b) / 255.0f,
47 static_cast<float>(p_Src.a) / 255.0f
48 );
49 }
53 inline static _u::Color32 Convert(_u::Color p_Src)
54 {
55 return _u::Color32(
56 (uint8_t)(p_Src.r * 255.0f),
57 (uint8_t)(p_Src.g * 255.0f),
58 (uint8_t)(p_Src.b * 255.0f),
59 (uint8_t)(p_Src.a * 255.0f)
60 );
61 }
62
63 public:
67 inline static bool TryToUnityColor(const char* p_Src, _u::Color* p_Color)
68 {
69 *p_Color = _u::Color(0.0f, 0.0f, 0.0f, 1.0f);
70 if (p_Src == nullptr)
71 return false;
72
73 auto l_Length = strlen(p_Src);
74 if (l_Length == 0)
75 return false;
76
77 auto l_Offset = p_Src[0] == '#' ? 1 : 0;
78 auto l_R = (ConvertSingleByte(p_Src, l_Length, l_Offset + 0) << 4) | ConvertSingleByte(p_Src, l_Length, l_Offset + 1);
79 auto l_G = (ConvertSingleByte(p_Src, l_Length, l_Offset + 2) << 4) | ConvertSingleByte(p_Src, l_Length, l_Offset + 3);
80 auto l_B = (ConvertSingleByte(p_Src, l_Length, l_Offset + 4) << 4) | ConvertSingleByte(p_Src, l_Length, l_Offset + 5);
81 auto l_A = 255;
82
83 if ((l_Length - l_Offset) > 6)
84 l_A = (ConvertSingleByte(p_Src, l_Length, l_Offset + 6) << 4) | ConvertSingleByte(p_Src, l_Length, l_Offset + 7);
85
86 *p_Color = Convert(_u::Color32((uint8_t)l_R, (uint8_t)l_G, (uint8_t)l_B, (uint8_t)l_A));
87
88 return true;
89 }
92 inline static _u::Color ToUnityColor(const char* p_Src)
93 {
94 _u::Color l_Color;
95 TryToUnityColor(p_Src, &l_Color);
96 return l_Color;
97 }
98
99 public:
102 inline static std::u16string ToHexRGB(_u::Color p_Color)
103 {
104 static const char16_t s_IntToHex[16] = { u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'A', u'B', u'C', u'D', u'E', u'F' };
105
106 auto l_Color32 = Convert(p_Color);
107 return std::u16string(
108 {
109 u'#',
110 s_IntToHex[(l_Color32.r >> 4) & 0xF],
111 s_IntToHex[(l_Color32.r >> 0) & 0xF],
112 s_IntToHex[(l_Color32.g >> 4) & 0xF],
113 s_IntToHex[(l_Color32.g >> 0) & 0xF],
114 s_IntToHex[(l_Color32.b >> 4) & 0xF],
115 s_IntToHex[(l_Color32.b >> 0) & 0xF]
116 });
117 }
120 inline static std::u16string ToHexRGBA(_u::Color p_Color)
121 {
122 static const char16_t s_IntToHex[16] = { u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'A', u'B', u'C', u'D', u'E', u'F' };
123
124 auto l_Color32 = Convert(p_Color);
125 return std::u16string(
126 {
127 u'#',
128 s_IntToHex[(l_Color32.r >> 4) & 0xF],
129 s_IntToHex[(l_Color32.r >> 0) & 0xF],
130 s_IntToHex[(l_Color32.g >> 4) & 0xF],
131 s_IntToHex[(l_Color32.g >> 0) & 0xF],
132 s_IntToHex[(l_Color32.b >> 4) & 0xF],
133 s_IntToHex[(l_Color32.b >> 0) & 0xF],
134 s_IntToHex[(l_Color32.a >> 4) & 0xF],
135 s_IntToHex[(l_Color32.a >> 0) & 0xF]
136 });
137 }
138
139 private:
145 inline static int ConvertSingleByte(const char* p_Src, int p_Size, int p_Pos)
146 {
147 if (p_Pos >= p_Size)
148 return 0;
149
150 auto l_Char = p_Src[p_Pos];
151 if (l_Char >= '0' && l_Char <= '9')
152 return (l_Char - '0') + 0x00;
153 else if (l_Char >= 'a' && l_Char <= 'f')
154 return (l_Char - 'a') + 0x0A;
155 else if (l_Char >= 'A' && l_Char <= 'F')
156 return (l_Char - 'A') + 0x0A;
157
158 return 0;
159 }
160
161 };
162
163}
static _u::Color ToUnityColor(const char *p_Src)
String to unity color.
Definition ColorU.hpp:92
static _u::Color WithAlpha(const char *p_Color, float p_Alpha)
Get color with alpha.
Definition ColorU.hpp:32
static _u::Color WithAlpha(_u::Color p_Color, float p_Alpha)
Get color with alpha.
Definition ColorU.hpp:24
static bool TryToUnityColor(const char *p_Src, _u::Color *p_Color)
String to unity color.
Definition ColorU.hpp:67
static _u::Color Convert(_u::Color32 p_Src)
Convert a Color32 to Color.
Definition ColorU.hpp:41
static std::u16string ToHexRGB(_u::Color p_Color)
To hexadecimal RGB with # prefix.
Definition ColorU.hpp:102
static std::u16string ToHexRGBA(_u::Color p_Color)
To hexadecimal RGBA with # prefix.
Definition ColorU.hpp:120
static _u::Color32 Convert(_u::Color p_Src)
Convert a Color to Color32.
Definition ColorU.hpp:53
Quality of life extensions to interact with Unity types.
Definition ColorU.hpp:6
Various platform utils like Delegate/Action/Function/Event system.
Definition Delegate.hpp:8