color.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*************************************************************************/
  2. /* color.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. void Color::invert() {
  178. r = 1.0 - r;
  179. g = 1.0 - g;
  180. b = 1.0 - b;
  181. }
  182. void Color::contrast() {
  183. r = Math::fmod(r + 0.5, 1.0);
  184. g = Math::fmod(g + 0.5, 1.0);
  185. b = Math::fmod(b + 0.5, 1.0);
  186. }
  187. Color Color::hex(uint32_t p_hex) {
  188. float a = (p_hex & 0xFF) / 255.0;
  189. p_hex >>= 8;
  190. float b = (p_hex & 0xFF) / 255.0;
  191. p_hex >>= 8;
  192. float g = (p_hex & 0xFF) / 255.0;
  193. p_hex >>= 8;
  194. float r = (p_hex & 0xFF) / 255.0;
  195. return Color(r, g, b, a);
  196. }
  197. Color Color::hex64(uint64_t p_hex) {
  198. float a = (p_hex & 0xFFFF) / 65535.0;
  199. p_hex >>= 16;
  200. float b = (p_hex & 0xFFFF) / 65535.0;
  201. p_hex >>= 16;
  202. float g = (p_hex & 0xFFFF) / 65535.0;
  203. p_hex >>= 16;
  204. float r = (p_hex & 0xFFFF) / 65535.0;
  205. return Color(r, g, b, a);
  206. }
  207. Color Color::from_rgbe9995(uint32_t p_rgbe) {
  208. float r = p_rgbe & 0x1ff;
  209. float g = (p_rgbe >> 9) & 0x1ff;
  210. float b = (p_rgbe >> 18) & 0x1ff;
  211. float e = (p_rgbe >> 27);
  212. float m = Math::pow(2, e - 15.0 - 9.0);
  213. float rd = r * m;
  214. float gd = g * m;
  215. float bd = b * m;
  216. return Color(rd, gd, bd, 1.0f);
  217. }
  218. static float _parse_col(const String &p_str, int p_ofs) {
  219. int ig = 0;
  220. for (int i = 0; i < 2; i++) {
  221. int c = p_str[i + p_ofs];
  222. int v = 0;
  223. if (c >= '0' && c <= '9') {
  224. v = c - '0';
  225. } else if (c >= 'a' && c <= 'f') {
  226. v = c - 'a';
  227. v += 10;
  228. } else if (c >= 'A' && c <= 'F') {
  229. v = c - 'A';
  230. v += 10;
  231. } else {
  232. return -1;
  233. }
  234. if (i == 0)
  235. ig += v * 16;
  236. else
  237. ig += v;
  238. }
  239. return ig;
  240. }
  241. Color Color::inverted() const {
  242. Color c = *this;
  243. c.invert();
  244. return c;
  245. }
  246. Color Color::contrasted() const {
  247. Color c = *this;
  248. c.contrast();
  249. return c;
  250. }
  251. Color Color::html(const String &p_color) {
  252. String color = p_color;
  253. if (color.length() == 0)
  254. return Color();
  255. if (color[0] == '#')
  256. color = color.substr(1, color.length() - 1);
  257. if (color.length() == 3 || color.length() == 4) {
  258. String exp_color;
  259. for (int i = 0; i < color.length(); i++) {
  260. exp_color += color[i];
  261. exp_color += color[i];
  262. }
  263. color = exp_color;
  264. }
  265. bool alpha = false;
  266. if (color.length() == 8) {
  267. alpha = true;
  268. } else if (color.length() == 6) {
  269. alpha = false;
  270. } else {
  271. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  272. ERR_FAIL_V(Color());
  273. }
  274. int a = 255;
  275. if (alpha) {
  276. a = _parse_col(color, 0);
  277. if (a < 0) {
  278. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  279. ERR_FAIL_V(Color());
  280. }
  281. }
  282. int from = alpha ? 2 : 0;
  283. int r = _parse_col(color, from + 0);
  284. if (r < 0) {
  285. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  286. ERR_FAIL_V(Color());
  287. }
  288. int g = _parse_col(color, from + 2);
  289. if (g < 0) {
  290. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  291. ERR_FAIL_V(Color());
  292. }
  293. int b = _parse_col(color, from + 4);
  294. if (b < 0) {
  295. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  296. ERR_FAIL_V(Color());
  297. }
  298. return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  299. }
  300. bool Color::html_is_valid(const String &p_color) {
  301. String color = p_color;
  302. if (color.length() == 0)
  303. return false;
  304. if (color[0] == '#')
  305. color = color.substr(1, color.length() - 1);
  306. bool alpha = false;
  307. if (color.length() == 8) {
  308. alpha = true;
  309. } else if (color.length() == 6) {
  310. alpha = false;
  311. } else {
  312. return false;
  313. }
  314. int a = 255;
  315. if (alpha) {
  316. a = _parse_col(color, 0);
  317. if (a < 0) {
  318. return false;
  319. }
  320. }
  321. int from = alpha ? 2 : 0;
  322. int r = _parse_col(color, from + 0);
  323. if (r < 0) {
  324. return false;
  325. }
  326. int g = _parse_col(color, from + 2);
  327. if (g < 0) {
  328. return false;
  329. }
  330. int b = _parse_col(color, from + 4);
  331. if (b < 0) {
  332. return false;
  333. }
  334. return true;
  335. }
  336. Color Color::named(const String &p_name) {
  337. if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
  338. String name = p_name;
  339. // Normalize name
  340. name = name.replace(" ", "");
  341. name = name.replace("-", "");
  342. name = name.replace("_", "");
  343. name = name.replace("'", "");
  344. name = name.replace(".", "");
  345. name = name.to_lower();
  346. const Map<String, Color>::Element *color = _named_colors.find(name);
  347. if (color) {
  348. return color->value();
  349. } else {
  350. ERR_EXPLAIN("Invalid Color Name: " + p_name);
  351. ERR_FAIL_V(Color());
  352. }
  353. }
  354. String _to_hex(float p_val) {
  355. int v = Math::round(p_val * 255);
  356. v = CLAMP(v, 0, 255);
  357. String ret;
  358. for (int i = 0; i < 2; i++) {
  359. CharType c[2] = { 0, 0 };
  360. int lv = v & 0xF;
  361. if (lv < 10)
  362. c[0] = '0' + lv;
  363. else
  364. c[0] = 'a' + lv - 10;
  365. v >>= 4;
  366. String cs = (const CharType *)c;
  367. ret = cs + ret;
  368. }
  369. return ret;
  370. }
  371. String Color::to_html(bool p_alpha) const {
  372. String txt;
  373. txt += _to_hex(r);
  374. txt += _to_hex(g);
  375. txt += _to_hex(b);
  376. if (p_alpha)
  377. txt = _to_hex(a) + txt;
  378. return txt;
  379. }
  380. Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) const {
  381. p_h = Math::fmod(p_h * 360.0f, 360.0f);
  382. if (p_h < 0.0)
  383. p_h += 360.0f;
  384. const float h_ = p_h / 60.0f;
  385. const float c = p_v * p_s;
  386. const float x = c * (1.0f - Math::abs(Math::fmod(h_, 2.0f) - 1.0f));
  387. float r, g, b;
  388. switch ((int)h_) {
  389. case 0: {
  390. r = c;
  391. g = x;
  392. b = 0;
  393. } break;
  394. case 1: {
  395. r = x;
  396. g = c;
  397. b = 0;
  398. } break;
  399. case 2: {
  400. r = 0;
  401. g = c;
  402. b = x;
  403. } break;
  404. case 3: {
  405. r = 0;
  406. g = x;
  407. b = c;
  408. } break;
  409. case 4: {
  410. r = x;
  411. g = 0;
  412. b = c;
  413. } break;
  414. case 5: {
  415. r = c;
  416. g = 0;
  417. b = x;
  418. } break;
  419. default: {
  420. r = 0;
  421. g = 0;
  422. b = 0;
  423. } break;
  424. }
  425. const float m = p_v - c;
  426. return Color(m + r, m + g, m + b, p_a);
  427. }
  428. // FIXME: Remove once Godot 3.1 has been released
  429. float Color::gray() const {
  430. ERR_EXPLAIN("Color.gray() is deprecated and will be removed in a future version. Use Color.get_v() for a better grayscale approximation.");
  431. WARN_DEPRECATED
  432. return (r + g + b) / 3.0;
  433. }
  434. Color::operator String() const {
  435. return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a);
  436. }
  437. Color Color::operator+(const Color &p_color) const {
  438. return Color(
  439. r + p_color.r,
  440. g + p_color.g,
  441. b + p_color.b,
  442. a + p_color.a);
  443. }
  444. void Color::operator+=(const Color &p_color) {
  445. r = r + p_color.r;
  446. g = g + p_color.g;
  447. b = b + p_color.b;
  448. a = a + p_color.a;
  449. }
  450. Color Color::operator-(const Color &p_color) const {
  451. return Color(
  452. r - p_color.r,
  453. g - p_color.g,
  454. b - p_color.b,
  455. a - p_color.a);
  456. }
  457. void Color::operator-=(const Color &p_color) {
  458. r = r - p_color.r;
  459. g = g - p_color.g;
  460. b = b - p_color.b;
  461. a = a - p_color.a;
  462. }
  463. Color Color::operator*(const Color &p_color) const {
  464. return Color(
  465. r * p_color.r,
  466. g * p_color.g,
  467. b * p_color.b,
  468. a * p_color.a);
  469. }
  470. Color Color::operator*(const real_t &rvalue) const {
  471. return Color(
  472. r * rvalue,
  473. g * rvalue,
  474. b * rvalue,
  475. a * rvalue);
  476. }
  477. void Color::operator*=(const Color &p_color) {
  478. r = r * p_color.r;
  479. g = g * p_color.g;
  480. b = b * p_color.b;
  481. a = a * p_color.a;
  482. }
  483. void Color::operator*=(const real_t &rvalue) {
  484. r = r * rvalue;
  485. g = g * rvalue;
  486. b = b * rvalue;
  487. a = a * rvalue;
  488. }
  489. Color Color::operator/(const Color &p_color) const {
  490. return Color(
  491. r / p_color.r,
  492. g / p_color.g,
  493. b / p_color.b,
  494. a / p_color.a);
  495. }
  496. Color Color::operator/(const real_t &rvalue) const {
  497. return Color(
  498. r / rvalue,
  499. g / rvalue,
  500. b / rvalue,
  501. a / rvalue);
  502. }
  503. void Color::operator/=(const Color &p_color) {
  504. r = r / p_color.r;
  505. g = g / p_color.g;
  506. b = b / p_color.b;
  507. a = a / p_color.a;
  508. }
  509. void Color::operator/=(const real_t &rvalue) {
  510. if (rvalue == 0) {
  511. r = 1.0;
  512. g = 1.0;
  513. b = 1.0;
  514. a = 1.0;
  515. } else {
  516. r = r / rvalue;
  517. g = g / rvalue;
  518. b = b / rvalue;
  519. a = a / rvalue;
  520. }
  521. };
  522. Color Color::operator-() const {
  523. return Color(
  524. 1.0 - r,
  525. 1.0 - g,
  526. 1.0 - b,
  527. 1.0 - a);
  528. }