color.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /**************************************************************************/
  2. /* color.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. }
  104. float h;
  105. if (r == max) {
  106. h = (g - b) / delta; // between yellow & magenta
  107. } else if (g == max) {
  108. h = 2 + (b - r) / delta; // between cyan & yellow
  109. } else {
  110. h = 4 + (r - g) / delta; // between magenta & cyan
  111. }
  112. h /= 6.0;
  113. if (h < 0) {
  114. h += 1.0;
  115. }
  116. return h;
  117. }
  118. float Color::get_s() const {
  119. float min = MIN(r, g);
  120. min = MIN(min, b);
  121. float max = MAX(r, g);
  122. max = MAX(max, b);
  123. float delta = max - min;
  124. return (max != 0) ? (delta / max) : 0;
  125. }
  126. float Color::get_v() const {
  127. float max = MAX(r, g);
  128. max = MAX(max, b);
  129. return max;
  130. }
  131. void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
  132. int i;
  133. float f, p, q, t;
  134. a = p_alpha;
  135. if (p_s == 0) {
  136. // acp_hromatic (grey)
  137. r = g = b = p_v;
  138. return;
  139. }
  140. p_h *= 6.0;
  141. p_h = Math::fmod(p_h, 6);
  142. i = Math::floor(p_h);
  143. f = p_h - i;
  144. p = p_v * (1 - p_s);
  145. q = p_v * (1 - p_s * f);
  146. t = p_v * (1 - p_s * (1 - f));
  147. switch (i) {
  148. case 0: // Red is the dominant color
  149. r = p_v;
  150. g = t;
  151. b = p;
  152. break;
  153. case 1: // Green is the dominant color
  154. r = q;
  155. g = p_v;
  156. b = p;
  157. break;
  158. case 2:
  159. r = p;
  160. g = p_v;
  161. b = t;
  162. break;
  163. case 3: // Blue is the dominant color
  164. r = p;
  165. g = q;
  166. b = p_v;
  167. break;
  168. case 4:
  169. r = t;
  170. g = p;
  171. b = p_v;
  172. break;
  173. default: // (5) Red is the dominant color
  174. r = p_v;
  175. g = p;
  176. b = q;
  177. break;
  178. }
  179. }
  180. bool Color::is_equal_approx(const Color &p_color) const {
  181. 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);
  182. }
  183. void Color::invert() {
  184. r = 1.0 - r;
  185. g = 1.0 - g;
  186. b = 1.0 - b;
  187. }
  188. void Color::contrast() {
  189. r = Math::fmod(r + 0.5, 1.0);
  190. g = Math::fmod(g + 0.5, 1.0);
  191. b = Math::fmod(b + 0.5, 1.0);
  192. }
  193. Color Color::hex(uint32_t p_hex) {
  194. float a = (p_hex & 0xFF) / 255.0;
  195. p_hex >>= 8;
  196. float b = (p_hex & 0xFF) / 255.0;
  197. p_hex >>= 8;
  198. float g = (p_hex & 0xFF) / 255.0;
  199. p_hex >>= 8;
  200. float r = (p_hex & 0xFF) / 255.0;
  201. return Color(r, g, b, a);
  202. }
  203. Color Color::hex64(uint64_t p_hex) {
  204. float a = (p_hex & 0xFFFF) / 65535.0;
  205. p_hex >>= 16;
  206. float b = (p_hex & 0xFFFF) / 65535.0;
  207. p_hex >>= 16;
  208. float g = (p_hex & 0xFFFF) / 65535.0;
  209. p_hex >>= 16;
  210. float r = (p_hex & 0xFFFF) / 65535.0;
  211. return Color(r, g, b, a);
  212. }
  213. Color Color::from_rgbe9995(uint32_t p_rgbe) {
  214. float r = p_rgbe & 0x1ff;
  215. float g = (p_rgbe >> 9) & 0x1ff;
  216. float b = (p_rgbe >> 18) & 0x1ff;
  217. float e = (p_rgbe >> 27);
  218. float m = Math::pow(2, e - 15.0 - 9.0);
  219. float rd = r * m;
  220. float gd = g * m;
  221. float bd = b * m;
  222. return Color(rd, gd, bd, 1.0f);
  223. }
  224. static float _parse_col(const String &p_str, int p_ofs) {
  225. int ig = 0;
  226. for (int i = 0; i < 2; i++) {
  227. int c = p_str[i + p_ofs];
  228. int v = 0;
  229. if (c >= '0' && c <= '9') {
  230. v = c - '0';
  231. } else if (c >= 'a' && c <= 'f') {
  232. v = c - 'a';
  233. v += 10;
  234. } else if (c >= 'A' && c <= 'F') {
  235. v = c - 'A';
  236. v += 10;
  237. } else {
  238. return -1;
  239. }
  240. if (i == 0) {
  241. ig += v * 16;
  242. } else {
  243. ig += v;
  244. }
  245. }
  246. return ig;
  247. }
  248. Color Color::inverted() const {
  249. Color c = *this;
  250. c.invert();
  251. return c;
  252. }
  253. Color Color::contrasted() const {
  254. Color c = *this;
  255. c.contrast();
  256. return c;
  257. }
  258. Color Color::html(const String &p_color) {
  259. String color = p_color;
  260. if (color.length() == 0) {
  261. return Color();
  262. }
  263. if (color[0] == '#') {
  264. color = color.substr(1, color.length() - 1);
  265. }
  266. if (color.length() == 3 || color.length() == 4) {
  267. String exp_color;
  268. for (int i = 0; i < color.length(); i++) {
  269. exp_color += color[i];
  270. exp_color += color[i];
  271. }
  272. color = exp_color;
  273. }
  274. bool alpha = false;
  275. if (color.length() == 8) {
  276. alpha = true;
  277. } else if (color.length() == 6) {
  278. alpha = false;
  279. } else {
  280. ERR_FAIL_V_MSG(Color(), "Invalid color code: " + p_color + ".");
  281. }
  282. int a = 255;
  283. if (alpha) {
  284. a = _parse_col(color, 0);
  285. ERR_FAIL_COND_V_MSG(a < 0, Color(), "Invalid color code: " + p_color + ".");
  286. }
  287. int from = alpha ? 2 : 0;
  288. int r = _parse_col(color, from + 0);
  289. ERR_FAIL_COND_V_MSG(r < 0, Color(), "Invalid color code: " + p_color + ".");
  290. int g = _parse_col(color, from + 2);
  291. ERR_FAIL_COND_V_MSG(g < 0, Color(), "Invalid color code: " + p_color + ".");
  292. int b = _parse_col(color, from + 4);
  293. ERR_FAIL_COND_V_MSG(b < 0, Color(), "Invalid color code: " + p_color + ".");
  294. return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  295. }
  296. bool Color::html_is_valid(const String &p_color) {
  297. String color = p_color;
  298. if (color.length() == 0) {
  299. return false;
  300. }
  301. if (color[0] == '#') {
  302. color = color.substr(1, color.length() - 1);
  303. }
  304. bool alpha = false;
  305. if (color.length() == 8) {
  306. alpha = true;
  307. } else if (color.length() == 6) {
  308. alpha = false;
  309. } else {
  310. return false;
  311. }
  312. if (alpha) {
  313. int a = _parse_col(color, 0);
  314. if (a < 0) {
  315. return false;
  316. }
  317. }
  318. int from = alpha ? 2 : 0;
  319. int r = _parse_col(color, from + 0);
  320. if (r < 0) {
  321. return false;
  322. }
  323. int g = _parse_col(color, from + 2);
  324. if (g < 0) {
  325. return false;
  326. }
  327. int b = _parse_col(color, from + 4);
  328. if (b < 0) {
  329. return false;
  330. }
  331. return true;
  332. }
  333. Color Color::named(const String &p_name) {
  334. if (_named_colors.empty()) {
  335. _populate_named_colors(); // from color_names.inc
  336. }
  337. String name = p_name;
  338. // Normalize name
  339. name = name.replace(" ", "");
  340. name = name.replace("-", "");
  341. name = name.replace("_", "");
  342. name = name.replace("'", "");
  343. name = name.replace(".", "");
  344. name = name.to_lower();
  345. const Map<String, Color>::Element *color = _named_colors.find(name);
  346. ERR_FAIL_NULL_V_MSG(color, Color(), "Invalid color name: " + p_name + ".");
  347. return color->value();
  348. }
  349. String _to_hex(float p_val) {
  350. int v = Math::round(p_val * 255);
  351. v = CLAMP(v, 0, 255);
  352. String ret;
  353. for (int i = 0; i < 2; i++) {
  354. CharType c[2] = { 0, 0 };
  355. int lv = v & 0xF;
  356. if (lv < 10) {
  357. c[0] = '0' + lv;
  358. } else {
  359. c[0] = 'a' + lv - 10;
  360. }
  361. v >>= 4;
  362. String cs = (const CharType *)c;
  363. ret = cs + ret;
  364. }
  365. return ret;
  366. }
  367. String Color::to_html(bool p_alpha) const {
  368. String txt;
  369. txt += _to_hex(r);
  370. txt += _to_hex(g);
  371. txt += _to_hex(b);
  372. if (p_alpha) {
  373. txt = _to_hex(a) + txt;
  374. }
  375. return txt;
  376. }
  377. Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) const {
  378. Color c;
  379. c.set_hsv(p_h, p_s, p_v, p_a);
  380. return c;
  381. }
  382. // FIXME: Remove once Godot 3.1 has been released
  383. float Color::gray() const {
  384. WARN_DEPRECATED_MSG("'Color.gray()' is deprecated and will be removed in a future version. Use 'Color.v' for a better grayscale approximation.");
  385. return (r + g + b) / 3.0;
  386. }
  387. Color::operator String() const {
  388. return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a);
  389. }
  390. Color Color::operator+(const Color &p_color) const {
  391. return Color(
  392. r + p_color.r,
  393. g + p_color.g,
  394. b + p_color.b,
  395. a + p_color.a);
  396. }
  397. void Color::operator+=(const Color &p_color) {
  398. r = r + p_color.r;
  399. g = g + p_color.g;
  400. b = b + p_color.b;
  401. a = a + p_color.a;
  402. }
  403. Color Color::operator-(const Color &p_color) const {
  404. return Color(
  405. r - p_color.r,
  406. g - p_color.g,
  407. b - p_color.b,
  408. a - p_color.a);
  409. }
  410. void Color::operator-=(const Color &p_color) {
  411. r = r - p_color.r;
  412. g = g - p_color.g;
  413. b = b - p_color.b;
  414. a = a - p_color.a;
  415. }
  416. Color Color::operator*(const Color &p_color) const {
  417. return Color(
  418. r * p_color.r,
  419. g * p_color.g,
  420. b * p_color.b,
  421. a * p_color.a);
  422. }
  423. Color Color::operator*(const real_t &rvalue) const {
  424. return Color(
  425. r * rvalue,
  426. g * rvalue,
  427. b * rvalue,
  428. a * rvalue);
  429. }
  430. void Color::operator*=(const Color &p_color) {
  431. r = r * p_color.r;
  432. g = g * p_color.g;
  433. b = b * p_color.b;
  434. a = a * p_color.a;
  435. }
  436. void Color::operator*=(const real_t &rvalue) {
  437. r = r * rvalue;
  438. g = g * rvalue;
  439. b = b * rvalue;
  440. a = a * rvalue;
  441. }
  442. Color Color::operator/(const Color &p_color) const {
  443. return Color(
  444. r / p_color.r,
  445. g / p_color.g,
  446. b / p_color.b,
  447. a / p_color.a);
  448. }
  449. Color Color::operator/(const real_t &rvalue) const {
  450. return Color(
  451. r / rvalue,
  452. g / rvalue,
  453. b / rvalue,
  454. a / rvalue);
  455. }
  456. void Color::operator/=(const Color &p_color) {
  457. r = r / p_color.r;
  458. g = g / p_color.g;
  459. b = b / p_color.b;
  460. a = a / p_color.a;
  461. }
  462. void Color::operator/=(const real_t &rvalue) {
  463. if (rvalue == 0) {
  464. r = 1.0;
  465. g = 1.0;
  466. b = 1.0;
  467. a = 1.0;
  468. } else {
  469. r = r / rvalue;
  470. g = g / rvalue;
  471. b = b / rvalue;
  472. a = a / rvalue;
  473. }
  474. };
  475. Color Color::operator-() const {
  476. return Color(
  477. 1.0 - r,
  478. 1.0 - g,
  479. 1.0 - b,
  480. 1.0 - a);
  481. }