Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. class Color {
  2. public:
  3.  
  4. Color() {
  5. SetColor(0, 0, 0, 255);
  6. }
  7.  
  8. Color(uint8_t r, uint8_t g, uint8_t b) {
  9. SetColor(r, g, b, 255);
  10. }
  11.  
  12. Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
  13. SetColor(r, g, b, a);
  14. }
  15.  
  16. void SetColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) {
  17. _color[0] = (uint8_t)r;
  18. _color[1] = (uint8_t)g;
  19. _color[2] = (uint8_t)b;
  20. _color[3] = (uint8_t)a;
  21. }
  22.  
  23. void GetColor(uint8_t &r, uint8_t &g, uint8_t &b, uint8_t &a) const {
  24. r = _color[0];
  25. g = _color[1];
  26. b = _color[2];
  27. a = _color[3];
  28. }
  29.  
  30. uint8_t &operator[](int index) {
  31. return _color[index];
  32. }
  33.  
  34. const uint8_t &operator[](int index) const {
  35. return _color[index];
  36. }
  37.  
  38. bool operator == (const Color &rhs) const {
  39. return (*((int *)this) == *((int *)&rhs));
  40. }
  41.  
  42. bool operator != (const Color &rhs) const {
  43. return !(operator==(rhs));
  44. }
  45.  
  46. int r() { return _color[0]; }
  47. int g() { return _color[1]; }
  48. int b() { return _color[2]; }
  49. int a() { return _color[3]; }
  50.  
  51. int GetR() { return _color[0]; }
  52. int GetG() { return _color[1]; }
  53. int GetB() { return _color[2]; }
  54. int GetA() { return _color[3]; }
  55.  
  56. void SetR(uint8_t _i) { _color[0] = _i; }
  57. void SetG(uint8_t _i) { _color[1] = _i; }
  58. void SetB(uint8_t _i) { _color[2] = _i; }
  59. void SetA(uint8_t _i) { _color[3] = _i; }
  60.  
  61. Color &operator=(const Color &rhs) {
  62. *(int*)(&_color[0]) = *(int*)&rhs._color[0];
  63. return *this;
  64. }
  65.  
  66. Color operator+(const Color &rhs) const {
  67. int red = _color[0] + rhs._color[0];
  68. int green = _color[1] + rhs._color[1];
  69. int blue = _color[2] + rhs._color[2];
  70. int alpha = _color[3] + rhs._color[3];
  71.  
  72. red = red > 255 ? 255 : red;
  73. green = green > 255 ? 255 : green;
  74. blue = blue > 255 ? 255 : blue;
  75. alpha = alpha > 255 ? 255 : alpha;
  76.  
  77. return Color(red, green, blue, alpha);
  78. }
  79.  
  80. Color operator-(const Color &rhs) const {
  81. int red = _color[0] - rhs._color[0];
  82. int green = _color[1] - rhs._color[1];
  83. int blue = _color[2] - rhs._color[2];
  84. int alpha = _color[3] - rhs._color[3];
  85.  
  86. red = red < 0 ? 0 : red;
  87. green = green < 0 ? 0 : green;
  88. blue = blue < 0 ? 0 : blue;
  89. alpha = alpha < 0 ? 0 : alpha;
  90. return Color(red, green, blue, alpha);
  91. }
  92.  
  93. operator const uint8_t*() {
  94. return (uint8_t*)(&_color[0]);
  95. }
  96.  
  97. static Color FromHSB(float hue, float saturation, float brightness)
  98. {
  99. float h = hue == 1.0f ? 0 : hue * 6.0f;
  100. float f = h - (int)h;
  101. float p = brightness * (1.0f - saturation);
  102. float q = brightness * (1.0f - saturation * f);
  103. float t = brightness * (1.0f - (saturation * (1.0f - f)));
  104.  
  105. if (h < 1)
  106. {
  107. return Color(
  108. (unsigned char)(brightness * 255),
  109. (unsigned char)(t * 255),
  110. (unsigned char)(p * 255)
  111. );
  112. }
  113. else if (h < 2)
  114. {
  115. return Color(
  116. (unsigned char)(q * 255),
  117. (unsigned char)(brightness * 255),
  118. (unsigned char)(p * 255)
  119. );
  120. }
  121. else if (h < 3)
  122. {
  123. return Color(
  124. (unsigned char)(p * 255),
  125. (unsigned char)(brightness * 255),
  126. (unsigned char)(t * 255)
  127. );
  128. }
  129. else if (h < 4)
  130. {
  131. return Color(
  132. (unsigned char)(p * 255),
  133. (unsigned char)(q * 255),
  134. (unsigned char)(brightness * 255)
  135. );
  136. }
  137. else if (h < 5)
  138. {
  139. return Color(
  140. (unsigned char)(t * 255),
  141. (unsigned char)(p * 255),
  142. (unsigned char)(brightness * 255)
  143. );
  144. }
  145. else
  146. {
  147. return Color(
  148. (unsigned char)(brightness * 255),
  149. (unsigned char)(p * 255),
  150. (unsigned char)(q * 255)
  151. );
  152. }
  153. }
  154.  
  155. DEFCOLOR_SRC(Black, 0, 0, 0);
  156. DEFCOLOR_SRC(White, 255, 255, 255);
  157. DEFCOLOR_SRC(Red, 255, 0, 0);
  158. DEFCOLOR_SRC(Green, 0, 255, 0);
  159. DEFCOLOR_SRC(Blue, 0, 0, 255);
  160. DEFCOLOR_SRC(Lime, 0, 255, 0);
  161. DEFCOLOR_SRC(Yellow, 255, 255, 0);
  162. DEFCOLOR_SRC(Cyan, 0, 255, 255);
  163. DEFCOLOR_SRC(Magenta, 255, 0, 255);
  164. DEFCOLOR_SRC(Silver, 192, 192, 192);
  165. DEFCOLOR_SRC(Gray, 128, 128, 128);
  166. DEFCOLOR_SRC(Maroon, 128, 0, 0);
  167. DEFCOLOR_SRC(Olive, 128, 128, 0);
  168. DEFCOLOR_SRC(Purple, 128, 0, 128);
  169. DEFCOLOR_SRC(Teal, 0, 128, 128);
  170. DEFCOLOR_SRC(Navy, 0, 0, 128);
  171. DEFCOLOR_SRC(DarkRed, 139, 0, 0);
  172. DEFCOLOR_SRC(Brown, 165, 42, 42);
  173. DEFCOLOR_SRC(Firebrick, 178, 34, 34);
  174. DEFCOLOR_SRC(Crimson, 220, 20, 60);
  175. DEFCOLOR_SRC(IndianRed, 205, 92, 92);
  176. DEFCOLOR_SRC(LightCoral, 240, 128, 128);
  177. DEFCOLOR_SRC(DarkSalmon, 233, 150, 122);
  178. DEFCOLOR_SRC(Salmon, 250, 128, 114);
  179. DEFCOLOR_SRC(LightSalmon, 255, 160, 122);
  180. DEFCOLOR_SRC(OrangeRed, 255, 69, 0);
  181. DEFCOLOR_SRC(DarkOrange, 255, 140, 0);
  182. DEFCOLOR_SRC(Orange, 255, 165, 0);
  183. DEFCOLOR_SRC(Gold, 255, 215, 0);
  184. DEFCOLOR_SRC(DarkGoldenRod, 184, 134, 11);
  185. DEFCOLOR_SRC(GoldenRod, 218, 165, 32);
  186. DEFCOLOR_SRC(YellowGreen, 154, 205, 50);
  187. DEFCOLOR_SRC(DarkOliveGreen, 85, 107, 47);
  188. DEFCOLOR_SRC(OliveDrab, 107, 142, 35);
  189. DEFCOLOR_SRC(LawnGreen, 124, 252, 0);
  190. DEFCOLOR_SRC(ChartReuse, 127, 255, 0);
  191. DEFCOLOR_SRC(GreenYellow, 173, 255, 47);
  192. DEFCOLOR_SRC(DarkGreen, 0, 100, 0);
  193. DEFCOLOR_SRC(ForestGreen, 34, 139, 34);
  194. DEFCOLOR_SRC(LimeGreen, 50, 205, 50);
  195. DEFCOLOR_SRC(DarkCyan, 0, 139, 139);
  196. DEFCOLOR_SRC(Aqua, 0, 255, 255);
  197. DEFCOLOR_SRC(LightCyan, 224, 255, 255);
  198. DEFCOLOR_SRC(DarkTurquoise, 0, 206, 209);
  199. DEFCOLOR_SRC(Turquoise, 64, 224, 208);
  200. DEFCOLOR_SRC(MediumTurquoise, 72, 209, 204);
  201. DEFCOLOR_SRC(PaleTurquoise, 175, 238, 238);
  202. DEFCOLOR_SRC(Aquamarine, 127, 255, 212);
  203. DEFCOLOR_SRC(PowderBlue, 176, 224, 230);
  204. DEFCOLOR_SRC(DodgerBlue, 30, 144, 255);
  205. DEFCOLOR_SRC(Lightblue, 173, 216, 230);
  206. DEFCOLOR_SRC(SkyBlue, 135, 206, 235);
  207. DEFCOLOR_SRC(LightSkyBlue, 135, 206, 250);
  208. DEFCOLOR_SRC(MidnightBlue, 25, 25, 112);
  209. DEFCOLOR_SRC(DarkBlue, 0, 0, 139);
  210. DEFCOLOR_SRC(MediumBlue, 0, 0, 205);
  211. DEFCOLOR_SRC(RoyalBlue, 65, 105, 225);
  212. DEFCOLOR_SRC(BlueViolet, 138, 43, 226);
  213. DEFCOLOR_SRC(Indigo, 75, 0, 130);
  214. DEFCOLOR_SRC(DarkSlateBlue, 72, 61, 139);
  215. DEFCOLOR_SRC(SlateBlue, 106, 90, 205);
  216. DEFCOLOR_SRC(MediumSlateBlue, 123, 104, 238);
  217. DEFCOLOR_SRC(MediumPurple, 147, 112, 219);
  218. DEFCOLOR_SRC(Darkmagenta, 139, 0, 139);
  219. DEFCOLOR_SRC(Darkviolet, 148, 0, 211);
  220. DEFCOLOR_SRC(DarkGray, 169, 169, 169);
  221. DEFCOLOR_SRC(LightGray, 211, 211, 211);
  222. DEFCOLOR_SRC(Gainsboro, 220, 220, 220);
  223. private:
  224. uint8_t _color[4];
  225. };
  226. struct Color2
  227. {
  228. int r;
  229. int g;
  230. int b;
  231. int a;
  232.  
  233. Color2()
  234. {
  235. this->r = 0;
  236. this->g = 0;
  237. this->b = 0;
  238. this->a = 255;
  239. }
  240.  
  241. Color2(int r, int g, int b)
  242. {
  243. this->r = r;
  244. this->g = g;
  245. this->b = b;
  246. this->a = 255;
  247. }
  248.  
  249. Color2(int r, int g, int b, int a)
  250. {
  251. this->r = r;
  252. this->g = g;
  253. this->b = b;
  254. this->a = a;
  255. }
  256.  
  257. Color2 operator / (float div)
  258. {
  259. Color2 color = *this;
  260. color.r = color.r / div;
  261. color.g = color.g / div;
  262. color.b = color.b / div;
  263. return color;
  264. }
  265.  
  266. Color2& operator /= (float div)
  267. {
  268. Color2& color = *this;
  269. color.r /= div;
  270. color.g /= div;
  271. color.b /= div;
  272. return color;
  273. }
  274.  
  275. Color2& operator *= (float coeff)
  276. {
  277. Color2& color = *this;
  278. color.r *= coeff;
  279. color.g *= coeff;
  280. color.b *= coeff;
  281. return color;
  282. }
  283.  
  284. static Color2 FromHSB(float hue, float saturation, float brightness)
  285. {
  286. float h = hue == 1.0f ? 0 : hue * 6.0f;
  287. float f = h - (int)h;
  288. float p = brightness * (1.0f - saturation);
  289. float q = brightness * (1.0f - saturation * f);
  290. float t = brightness * (1.0f - (saturation * (1.0f - f)));
  291.  
  292. if (h < 1)
  293. {
  294. return Color2(
  295. (unsigned char)(brightness * 255),
  296. (unsigned char)(t * 255),
  297. (unsigned char)(p * 255)
  298. );
  299. }
  300. else if (h < 2)
  301. {
  302. return Color2(
  303. (unsigned char)(q * 255),
  304. (unsigned char)(brightness * 255),
  305. (unsigned char)(p * 255)
  306. );
  307. }
  308. else if (h < 3)
  309. {
  310. return Color2(
  311. (unsigned char)(p * 255),
  312. (unsigned char)(brightness * 255),
  313. (unsigned char)(t * 255)
  314. );
  315. }
  316. else if (h < 4)
  317. {
  318. return Color2(
  319. (unsigned char)(p * 255),
  320. (unsigned char)(q * 255),
  321. (unsigned char)(brightness * 255)
  322. );
  323. }
  324. else if (h < 5)
  325. {
  326. return Color2(
  327. (unsigned char)(t * 255),
  328. (unsigned char)(p * 255),
  329. (unsigned char)(brightness * 255)
  330. );
  331. }
  332. else
  333. {
  334. return Color2(
  335. (unsigned char)(brightness * 255),
  336. (unsigned char)(p * 255),
  337. (unsigned char)(q * 255)
  338. );
  339. }
  340. }
  341.  
  342. static Color2 FromImColor(ImColor color)
  343. {
  344. return Color2(
  345. (int)(color.Value.x * 255),
  346. (int)(color.Value.y * 255),
  347. (int)(color.Value.z * 255),
  348. (int)(color.Value.w * 255)
  349. );
  350. }
  351.  
  352.  
  353. static ImColor ToImColor(Color2 color)
  354. {
  355. return ImColor(
  356. color.r / 255.f,
  357. color.g / 255.f,
  358. color.b / 255.f,
  359. color.a / 255.f
  360. );
  361. }
  362.  
  363. };
  364.  
  365. class Rainbow
  366. {
  367. public:
  368. float r, g, b, a;
  369. Rainbow(float r, float g, float b)
  370. {
  371. this->r = r;
  372. this->g = g;
  373. this->b = b;
  374. this->a = 255;
  375. }
  376. };
  377.  
  378. extern Color2 Red;
  379. extern Color2 Green;
  380. extern Color2 White;
  381. extern Color2 Black;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement