color.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*************************************************************************/
  2. /* color.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "color.h"
  31. #include "core/color_names.inc"
  32. #include "core/map.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/print_string.h"
  35. uint32_t Color::to_argb32() const {
  36. uint32_t c = (uint8_t)Math::round(a * 255);
  37. c <<= 8;
  38. c |= (uint8_t)Math::round(r * 255);
  39. c <<= 8;
  40. c |= (uint8_t)Math::round(g * 255);
  41. c <<= 8;
  42. c |= (uint8_t)Math::round(b * 255);
  43. return c;
  44. }
  45. uint32_t Color::to_abgr32() const {
  46. uint32_t c = (uint8_t)Math::round(a * 255);
  47. c <<= 8;
  48. c |= (uint8_t)Math::round(b * 255);
  49. c <<= 8;
  50. c |= (uint8_t)Math::round(g * 255);
  51. c <<= 8;
  52. c |= (uint8_t)Math::round(r * 255);
  53. return c;
  54. }
  55. uint32_t Color::to_rgba32() const {
  56. uint32_t c = (uint8_t)Math::round(r * 255);
  57. c <<= 8;
  58. c |= (uint8_t)Math::round(g * 255);
  59. c <<= 8;
  60. c |= (uint8_t)Math::round(b * 255);
  61. c <<= 8;
  62. c |= (uint8_t)Math::round(a * 255);
  63. return c;
  64. }
  65. uint64_t Color::to_abgr64() const {
  66. uint64_t c = (uint16_t)Math::round(a * 65535);
  67. c <<= 16;
  68. c |= (uint16_t)Math::round(b * 65535);
  69. c <<= 16;
  70. c |= (uint16_t)Math::round(g * 65535);
  71. c <<= 16;
  72. c |= (uint16_t)Math::round(r * 65535);
  73. return c;
  74. }
  75. uint64_t Color::to_argb64() const {
  76. uint64_t c = (uint16_t)Math::round(a * 65535);
  77. c <<= 16;
  78. c |= (uint16_t)Math::round(r * 65535);
  79. c <<= 16;
  80. c |= (uint16_t)Math::round(g * 65535);
  81. c <<= 16;
  82. c |= (uint16_t)Math::round(b * 65535);
  83. return c;
  84. }
  85. uint64_t Color::to_rgba64() const {
  86. uint64_t c = (uint16_t)Math::round(r * 65535);
  87. c <<= 16;
  88. c |= (uint16_t)Math::round(g * 65535);
  89. c <<= 16;
  90. c |= (uint16_t)Math::round(b * 65535);
  91. c <<= 16;
  92. c |= (uint16_t)Math::round(a * 65535);
  93. return c;
  94. }
  95. float Color::get_h() const {
  96. float min = MIN(r, g);
  97. min = MIN(min, b);
  98. float max = MAX(r, g);
  99. max = MAX(max, b);
  100. float delta = max - min;
  101. if (delta == 0)
  102. return 0;
  103. float h;
  104. if (r == max)
  105. h = (g - b) / delta; // between yellow & magenta
  106. else if (g == max)
  107. h = 2 + (b - r) / delta; // between cyan & yellow
  108. else
  109. h = 4 + (r - g) / delta; // between magenta & cyan
  110. h /= 6.0;
  111. if (h < 0)
  112. h += 1.0;
  113. return h;
  114. }
  115. float Color::get_s() const {
  116. float min = MIN(r, g);
  117. min = MIN(min, b);
  118. float max = MAX(r, g);
  119. max = MAX(max, b);
  120. float delta = max - min;
  121. return (max != 0) ? (delta / max) : 0;
  122. }
  123. float Color::get_v() const {
  124. float max = MAX(r, g);
  125. max = MAX(max, b);
  126. return max;
  127. }
  128. void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
  129. int i;
  130. float f, p, q, t;
  131. a = p_alpha;
  132. if (p_s == 0) {
  133. // acp_hromatic (grey)
  134. r = g = b = p_v;
  135. return;
  136. }
  137. p_h *= 6.0;
  138. p_h = Math::fmod(p_h, 6);
  139. i = Math::floor(p_h);
  140. f = p_h - i;
  141. p = p_v * (1 - p_s);
  142. q = p_v * (1 - p_s * f);
  143. t = p_v * (1 - p_s * (1 - f));
  144. switch (i) {
  145. case 0: // Red is the dominant color
  146. r = p_v;
  147. g = t;
  148. b = p;
  149. break;
  150. case 1: // Green is the dominant color
  151. r = q;
  152. g = p_v;
  153. b = p;
  154. break;
  155. case 2:
  156. r = p;
  157. g = p_v;
  158. b = t;
  159. break;
  160. case 3: // Blue is the dominant color
  161. r = p;
  162. g = q;
  163. b = p_v;
  164. break;
  165. case 4:
  166. r = t;
  167. g = p;
  168. b = p_v;
  169. break;
  170. default: // (5) Red is the dominant color
  171. r = p_v;
  172. g = p;
  173. b = q;
  174. break;
  175. }
  176. }
  177. bool Color::is_equal_approx(const Color &p_color) const {
  178. return Math::is_equal_approx(r, p_color.r) && Math::is_equal_approx(g, p_color.g) && Math::is_equal_approx(b, p_color.b) && Math::is_equal_approx(a, p_color.a);
  179. }
  180. void Color::invert() {
  181. r = 1.0 - r;
  182. g = 1.0 - g;
  183. b = 1.0 - b;
  184. }
  185. void Color::contrast() {
  186. r = Math::fmod(r + 0.5, 1.0);
  187. g = Math::fmod(g + 0.5, 1.0);
  188. b = Math::fmod(b + 0.5, 1.0);
  189. }
  190. Color Color::hex(uint32_t p_hex) {
  191. float a = (p_hex & 0xFF) / 255.0;
  192. p_hex >>= 8;
  193. float b = (p_hex & 0xFF) / 255.0;
  194. p_hex >>= 8;
  195. float g = (p_hex & 0xFF) / 255.0;
  196. p_hex >>= 8;
  197. float r = (p_hex & 0xFF) / 255.0;
  198. return Color(r, g, b, a);
  199. }
  200. Color Color::hex64(uint64_t p_hex) {
  201. float a = (p_hex & 0xFFFF) / 65535.0;
  202. p_hex >>= 16;
  203. float b = (p_hex & 0xFFFF) / 65535.0;
  204. p_hex >>= 16;
  205. float g = (p_hex & 0xFFFF) / 65535.0;
  206. p_hex >>= 16;
  207. float r = (p_hex & 0xFFFF) / 65535.0;
  208. return Color(r, g, b, a);
  209. }
  210. Color Color::from_rgbe9995(uint32_t p_rgbe) {
  211. float r = p_rgbe & 0x1ff;
  212. float g = (p_rgbe >> 9) & 0x1ff;
  213. float b = (p_rgbe >> 18) & 0x1ff;
  214. float e = (p_rgbe >> 27);
  215. float m = Math::pow(2, e - 15.0 - 9.0);
  216. float rd = r * m;
  217. float gd = g * m;
  218. float bd = b * m;
  219. return Color(rd, gd, bd, 1.0f);
  220. }
  221. static float _parse_col(const String &p_str, int p_ofs) {
  222. int ig = 0;
  223. for (int i = 0; i < 2; i++) {
  224. int c = p_str[i + p_ofs];
  225. int v = 0;
  226. if (c >= '0' && c <= '9') {
  227. v = c - '0';
  228. } else if (c >= 'a' && c <= 'f') {
  229. v = c - 'a';
  230. v += 10;
  231. } else if (c >= 'A' && c <= 'F') {
  232. v = c - 'A';
  233. v += 10;
  234. } else {
  235. return -1;
  236. }
  237. if (i == 0)
  238. ig += v * 16;
  239. else
  240. ig += v;
  241. }
  242. return ig;
  243. }
  244. Color Color::inverted() const {
  245. Color c = *this;
  246. c.invert();
  247. return c;
  248. }
  249. Color Color::contrasted() const {
  250. Color c = *this;
  251. c.contrast();
  252. return c;
  253. }
  254. Color Color::html(const String &p_color) {
  255. String color = p_color;
  256. if (color.length() == 0)
  257. return Color();
  258. if (color[0] == '#')
  259. color = color.substr(1, color.length() - 1);
  260. if (color.length() == 3 || color.length() == 4) {
  261. String exp_color;
  262. for (int i = 0; i < color.length(); i++) {
  263. exp_color += color[i];
  264. exp_color += color[i];
  265. }
  266. color = exp_color;
  267. }
  268. bool alpha = false;
  269. if (color.length() == 8) {
  270. alpha = true;
  271. } else if (color.length() == 6) {
  272. alpha = false;
  273. } else {
  274. ERR_FAIL_V_MSG(Color(), "Invalid color code: " + p_color + ".");
  275. }
  276. int a = 255;
  277. if (alpha) {
  278. a = _parse_col(color, 0);
  279. ERR_FAIL_COND_V_MSG(a < 0, Color(), "Invalid color code: " + p_color + ".");
  280. }
  281. int from = alpha ? 2 : 0;
  282. int r = _parse_col(color, from + 0);
  283. ERR_FAIL_COND_V_MSG(r < 0, Color(), "Invalid color code: " + p_color + ".");
  284. int g = _parse_col(color, from + 2);
  285. ERR_FAIL_COND_V_MSG(g < 0, Color(), "Invalid color code: " + p_color + ".");
  286. int b = _parse_col(color, from + 4);
  287. ERR_FAIL_COND_V_MSG(b < 0, Color(), "Invalid color code: " + p_color + ".");
  288. return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  289. }
  290. bool Color::html_is_valid(const String &p_color) {
  291. String color = p_color;
  292. if (color.length() == 0)
  293. return false;
  294. if (color[0] == '#')
  295. color = color.substr(1, color.length() - 1);
  296. bool alpha = false;
  297. if (color.length() == 8) {
  298. alpha = true;
  299. } else if (color.length() == 6) {
  300. alpha = false;
  301. } else {
  302. return false;
  303. }
  304. if (alpha) {
  305. int a = _parse_col(color, 0);
  306. if (a < 0) {
  307. return false;
  308. }
  309. }
  310. int from = alpha ? 2 : 0;
  311. int r = _parse_col(color, from + 0);
  312. if (r < 0) {
  313. return false;
  314. }
  315. int g = _parse_col(color, from + 2);
  316. if (g < 0) {
  317. return false;
  318. }
  319. int b = _parse_col(color, from + 4);
  320. if (b < 0) {
  321. return false;
  322. }
  323. return true;
  324. }
  325. Color Color::named(const String &p_name) {
  326. if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
  327. String name = p_name;
  328. // Normalize name
  329. name = name.replace(" ", "");
  330. name = name.replace("-", "");
  331. name = name.replace("_", "");
  332. name = name.replace("'", "");
  333. name = name.replace(".", "");
  334. name = name.to_lower();
  335. const Map<String, Color>::Element *color = _named_colors.find(name);
  336. ERR_FAIL_NULL_V_MSG(color, Color(), "Invalid color name: " + p_name + ".");
  337. return color->value();
  338. }
  339. String _to_hex(float p_val) {
  340. int v = Math::round(p_val * 255);
  341. v = CLAMP(v, 0, 255);
  342. String ret;
  343. for (int i = 0; i < 2; i++) {
  344. CharType c[2] = { 0, 0 };
  345. int lv = v & 0xF;
  346. if (lv < 10)
  347. c[0] = '0' + lv;
  348. else
  349. c[0] = 'a' + lv - 10;
  350. v >>= 4;
  351. String cs = (const CharType *)c;
  352. ret = cs + ret;
  353. }
  354. return ret;
  355. }
  356. String Color::to_html(bool p_alpha) const {
  357. String txt;
  358. txt += _to_hex(r);
  359. txt += _to_hex(g);
  360. txt += _to_hex(b);
  361. if (p_alpha)
  362. txt = _to_hex(a) + txt;
  363. return txt;
  364. }
  365. Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) const {
  366. p_h = Math::fmod(p_h * 360.0f, 360.0f);
  367. if (p_h < 0.0)
  368. p_h += 360.0f;
  369. const float h_ = p_h / 60.0f;
  370. const float c = p_v * p_s;
  371. const float x = c * (1.0f - Math::abs(Math::fmod(h_, 2.0f) - 1.0f));
  372. float r, g, b;
  373. switch ((int)h_) {
  374. case 0: {
  375. r = c;
  376. g = x;
  377. b = 0;
  378. } break;
  379. case 1: {
  380. r = x;
  381. g = c;
  382. b = 0;
  383. } break;
  384. case 2: {
  385. r = 0;
  386. g = c;
  387. b = x;
  388. } break;
  389. case 3: {
  390. r = 0;
  391. g = x;
  392. b = c;
  393. } break;
  394. case 4: {
  395. r = x;
  396. g = 0;
  397. b = c;
  398. } break;
  399. case 5: {
  400. r = c;
  401. g = 0;
  402. b = x;
  403. } break;
  404. default: {
  405. r = 0;
  406. g = 0;
  407. b = 0;
  408. } break;
  409. }
  410. const float m = p_v - c;
  411. return Color(m + r, m + g, m + b, p_a);
  412. }
  413. // FIXME: Remove once Godot 3.1 has been released
  414. float Color::gray() const {
  415. WARN_DEPRECATED_MSG("'Color.gray()' is deprecated and will be removed in a future version. Use 'Color.v' for a better grayscale approximation.");
  416. return (r + g + b) / 3.0;
  417. }
  418. Color::operator String() const {
  419. return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a);
  420. }
  421. Color Color::operator+(const Color &p_color) const {
  422. return Color(
  423. r + p_color.r,
  424. g + p_color.g,
  425. b + p_color.b,
  426. a + p_color.a);
  427. }
  428. void Color::operator+=(const Color &p_color) {
  429. r = r + p_color.r;
  430. g = g + p_color.g;
  431. b = b + p_color.b;
  432. a = a + p_color.a;
  433. }
  434. Color Color::operator-(const Color &p_color) const {
  435. return Color(
  436. r - p_color.r,
  437. g - p_color.g,
  438. b - p_color.b,
  439. a - p_color.a);
  440. }
  441. void Color::operator-=(const Color &p_color) {
  442. r = r - p_color.r;
  443. g = g - p_color.g;
  444. b = b - p_color.b;
  445. a = a - p_color.a;
  446. }
  447. Color Color::operator*(const Color &p_color) const {
  448. return Color(
  449. r * p_color.r,
  450. g * p_color.g,
  451. b * p_color.b,
  452. a * p_color.a);
  453. }
  454. Color Color::operator*(const real_t &rvalue) const {
  455. return Color(
  456. r * rvalue,
  457. g * rvalue,
  458. b * rvalue,
  459. a * rvalue);
  460. }
  461. void Color::operator*=(const Color &p_color) {
  462. r = r * p_color.r;
  463. g = g * p_color.g;
  464. b = b * p_color.b;
  465. a = a * p_color.a;
  466. }
  467. void Color::operator*=(const real_t &rvalue) {
  468. r = r * rvalue;
  469. g = g * rvalue;
  470. b = b * rvalue;
  471. a = a * rvalue;
  472. }
  473. Color Color::operator/(const Color &p_color) const {
  474. return Color(
  475. r / p_color.r,
  476. g / p_color.g,
  477. b / p_color.b,
  478. a / p_color.a);
  479. }
  480. Color Color::operator/(const real_t &rvalue) const {
  481. return Color(
  482. r / rvalue,
  483. g / rvalue,
  484. b / rvalue,
  485. a / rvalue);
  486. }
  487. void Color::operator/=(const Color &p_color) {
  488. r = r / p_color.r;
  489. g = g / p_color.g;
  490. b = b / p_color.b;
  491. a = a / p_color.a;
  492. }
  493. void Color::operator/=(const real_t &rvalue) {
  494. if (rvalue == 0) {
  495. r = 1.0;
  496. g = 1.0;
  497. b = 1.0;
  498. a = 1.0;
  499. } else {
  500. r = r / rvalue;
  501. g = g / rvalue;
  502. b = b / rvalue;
  503. a = a / rvalue;
  504. }
  505. };
  506. Color Color::operator-() const {
  507. return Color(
  508. 1.0 - r,
  509. 1.0 - g,
  510. 1.0 - b,
  511. 1.0 - a);
  512. }