color.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*************************************************************************/
  2. /* color.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "color_names.inc"
  32. #include "map.h"
  33. #include "math_funcs.h"
  34. #include "print_string.h"
  35. uint32_t Color::to_argb32() const {
  36. uint32_t c = (uint8_t)(a * 255);
  37. c <<= 8;
  38. c |= (uint8_t)(r * 255);
  39. c <<= 8;
  40. c |= (uint8_t)(g * 255);
  41. c <<= 8;
  42. c |= (uint8_t)(b * 255);
  43. return c;
  44. }
  45. uint32_t Color::to_abgr32() const {
  46. uint32_t c = (uint8_t)(a * 255);
  47. c <<= 8;
  48. c |= (uint8_t)(b * 255);
  49. c <<= 8;
  50. c |= (uint8_t)(g * 255);
  51. c <<= 8;
  52. c |= (uint8_t)(r * 255);
  53. return c;
  54. }
  55. uint32_t Color::to_rgba32() const {
  56. uint32_t c = (uint8_t)(r * 255);
  57. c <<= 8;
  58. c |= (uint8_t)(g * 255);
  59. c <<= 8;
  60. c |= (uint8_t)(b * 255);
  61. c <<= 8;
  62. c |= (uint8_t)(a * 255);
  63. return c;
  64. }
  65. float Color::get_h() const {
  66. float min = MIN(r, g);
  67. min = MIN(min, b);
  68. float max = MAX(r, g);
  69. max = MAX(max, b);
  70. float delta = max - min;
  71. if (delta == 0)
  72. return 0;
  73. float h;
  74. if (r == max)
  75. h = (g - b) / delta; // between yellow & magenta
  76. else if (g == max)
  77. h = 2 + (b - r) / delta; // between cyan & yellow
  78. else
  79. h = 4 + (r - g) / delta; // between magenta & cyan
  80. h /= 6.0;
  81. if (h < 0)
  82. h += 1.0;
  83. return h;
  84. }
  85. float Color::get_s() const {
  86. float min = MIN(r, g);
  87. min = MIN(min, b);
  88. float max = MAX(r, g);
  89. max = MAX(max, b);
  90. float delta = max - min;
  91. return (max != 0) ? (delta / max) : 0;
  92. }
  93. float Color::get_v() const {
  94. float max = MAX(r, g);
  95. max = MAX(max, b);
  96. return max;
  97. }
  98. void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
  99. int i;
  100. float f, p, q, t;
  101. a = p_alpha;
  102. if (p_s == 0) {
  103. // acp_hromatic (grey)
  104. r = g = b = p_v;
  105. return;
  106. }
  107. p_h *= 6.0;
  108. p_h = Math::fmod(p_h, 6);
  109. i = Math::floor(p_h);
  110. f = p_h - i;
  111. p = p_v * (1 - p_s);
  112. q = p_v * (1 - p_s * f);
  113. t = p_v * (1 - p_s * (1 - f));
  114. switch (i) {
  115. case 0: // Red is the dominant color
  116. r = p_v;
  117. g = t;
  118. b = p;
  119. break;
  120. case 1: // Green is the dominant color
  121. r = q;
  122. g = p_v;
  123. b = p;
  124. break;
  125. case 2:
  126. r = p;
  127. g = p_v;
  128. b = t;
  129. break;
  130. case 3: // Blue is the dominant color
  131. r = p;
  132. g = q;
  133. b = p_v;
  134. break;
  135. case 4:
  136. r = t;
  137. g = p;
  138. b = p_v;
  139. break;
  140. default: // (5) Red is the dominant color
  141. r = p_v;
  142. g = p;
  143. b = q;
  144. break;
  145. }
  146. }
  147. void Color::invert() {
  148. r = 1.0 - r;
  149. g = 1.0 - g;
  150. b = 1.0 - b;
  151. }
  152. void Color::contrast() {
  153. r = Math::fmod(r + 0.5, 1.0);
  154. g = Math::fmod(g + 0.5, 1.0);
  155. b = Math::fmod(b + 0.5, 1.0);
  156. }
  157. Color Color::hex(uint32_t p_hex) {
  158. float a = (p_hex & 0xFF) / 255.0;
  159. p_hex >>= 8;
  160. float b = (p_hex & 0xFF) / 255.0;
  161. p_hex >>= 8;
  162. float g = (p_hex & 0xFF) / 255.0;
  163. p_hex >>= 8;
  164. float r = (p_hex & 0xFF) / 255.0;
  165. return Color(r, g, b, a);
  166. }
  167. static float _parse_col(const String &p_str, int p_ofs) {
  168. int ig = 0;
  169. for (int i = 0; i < 2; i++) {
  170. int c = p_str[i + p_ofs];
  171. int v = 0;
  172. if (c >= '0' && c <= '9') {
  173. v = c - '0';
  174. } else if (c >= 'a' && c <= 'f') {
  175. v = c - 'a';
  176. v += 10;
  177. } else if (c >= 'A' && c <= 'F') {
  178. v = c - 'A';
  179. v += 10;
  180. } else {
  181. return -1;
  182. }
  183. if (i == 0)
  184. ig += v * 16;
  185. else
  186. ig += v;
  187. }
  188. return ig;
  189. }
  190. Color Color::inverted() const {
  191. Color c = *this;
  192. c.invert();
  193. return c;
  194. }
  195. Color Color::contrasted() const {
  196. Color c = *this;
  197. c.contrast();
  198. return c;
  199. }
  200. Color Color::html(const String &p_color) {
  201. String color = p_color;
  202. if (color.length() == 0)
  203. return Color();
  204. if (color[0] == '#')
  205. color = color.substr(1, color.length() - 1);
  206. if (color.length() == 3 || color.length() == 4) {
  207. String exp_color;
  208. for (int i = 0; i < color.length(); i++) {
  209. exp_color += color[i];
  210. exp_color += color[i];
  211. }
  212. color = exp_color;
  213. }
  214. bool alpha = false;
  215. if (color.length() == 8) {
  216. alpha = true;
  217. } else if (color.length() == 6) {
  218. alpha = false;
  219. } else {
  220. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  221. ERR_FAIL_V(Color());
  222. }
  223. int a = 255;
  224. if (alpha) {
  225. a = _parse_col(color, 0);
  226. if (a < 0) {
  227. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  228. ERR_FAIL_V(Color());
  229. }
  230. }
  231. int from = alpha ? 2 : 0;
  232. int r = _parse_col(color, from + 0);
  233. if (r < 0) {
  234. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  235. ERR_FAIL_V(Color());
  236. }
  237. int g = _parse_col(color, from + 2);
  238. if (g < 0) {
  239. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  240. ERR_FAIL_V(Color());
  241. }
  242. int b = _parse_col(color, from + 4);
  243. if (b < 0) {
  244. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  245. ERR_FAIL_V(Color());
  246. }
  247. return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  248. }
  249. bool Color::html_is_valid(const String &p_color) {
  250. String color = p_color;
  251. if (color.length() == 0)
  252. return false;
  253. if (color[0] == '#')
  254. color = color.substr(1, color.length() - 1);
  255. bool alpha = false;
  256. if (color.length() == 8) {
  257. alpha = true;
  258. } else if (color.length() == 6) {
  259. alpha = false;
  260. } else {
  261. return false;
  262. }
  263. int a = 255;
  264. if (alpha) {
  265. a = _parse_col(color, 0);
  266. if (a < 0) {
  267. return false;
  268. }
  269. }
  270. int from = alpha ? 2 : 0;
  271. int r = _parse_col(color, from + 0);
  272. if (r < 0) {
  273. return false;
  274. }
  275. int g = _parse_col(color, from + 2);
  276. if (g < 0) {
  277. return false;
  278. }
  279. int b = _parse_col(color, from + 4);
  280. if (b < 0) {
  281. return false;
  282. }
  283. return true;
  284. }
  285. Color Color::named(const String &p_name) {
  286. if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
  287. String name = p_name;
  288. // Normalize name
  289. name = name.replace(" ", "");
  290. name = name.replace("-", "");
  291. name = name.replace("_", "");
  292. name = name.replace("'", "");
  293. name = name.replace(".", "");
  294. name = name.to_lower();
  295. const Map<String, Color>::Element *color = _named_colors.find(name);
  296. if (color) {
  297. return color->value();
  298. } else {
  299. ERR_EXPLAIN("Invalid Color Name: " + p_name);
  300. ERR_FAIL_V(Color());
  301. }
  302. }
  303. String _to_hex(float p_val) {
  304. int v = p_val * 255;
  305. v = CLAMP(v, 0, 255);
  306. String ret;
  307. for (int i = 0; i < 2; i++) {
  308. CharType c[2] = { 0, 0 };
  309. int lv = v & 0xF;
  310. if (lv < 10)
  311. c[0] = '0' + lv;
  312. else
  313. c[0] = 'a' + lv - 10;
  314. v >>= 4;
  315. String cs = (const CharType *)c;
  316. ret = cs + ret;
  317. }
  318. return ret;
  319. }
  320. String Color::to_html(bool p_alpha) const {
  321. String txt;
  322. txt += _to_hex(r);
  323. txt += _to_hex(g);
  324. txt += _to_hex(b);
  325. if (p_alpha)
  326. txt = _to_hex(a) + txt;
  327. return txt;
  328. }
  329. Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) {
  330. p_h = Math::fmod(p_h * 360.0f, 360.0f);
  331. if (p_h < 0.0)
  332. p_h += 360.0f;
  333. const float h_ = p_h / 60.0f;
  334. const float c = p_v * p_s;
  335. const float x = c * (1.0f - Math::abs(Math::fmod(h_, 2.0f) - 1.0f));
  336. float r, g, b;
  337. switch ((int)h_) {
  338. case 0: {
  339. r = c;
  340. g = x;
  341. b = 0;
  342. } break;
  343. case 1: {
  344. r = x;
  345. g = c;
  346. b = 0;
  347. } break;
  348. case 2: {
  349. r = 0;
  350. g = c;
  351. b = x;
  352. } break;
  353. case 3: {
  354. r = 0;
  355. g = x;
  356. b = c;
  357. } break;
  358. case 4: {
  359. r = x;
  360. g = 0;
  361. b = c;
  362. } break;
  363. case 5: {
  364. r = c;
  365. g = 0;
  366. b = x;
  367. } break;
  368. default: {
  369. r = 0;
  370. g = 0;
  371. b = 0;
  372. } break;
  373. }
  374. const float m = p_v - c;
  375. return Color(m + r, m + g, m + b, p_a);
  376. }
  377. float Color::gray() const {
  378. return (r + g + b) / 3.0;
  379. }
  380. Color::operator String() const {
  381. return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a);
  382. }
  383. Color Color::operator+(const Color &p_color) const {
  384. return Color(
  385. r + p_color.r,
  386. g + p_color.g,
  387. b + p_color.b,
  388. a + p_color.a);
  389. }
  390. void Color::operator+=(const Color &p_color) {
  391. r = r + p_color.r;
  392. g = g + p_color.g;
  393. b = b + p_color.b;
  394. a = a + p_color.a;
  395. }
  396. Color Color::operator-(const Color &p_color) const {
  397. return Color(
  398. r - p_color.r,
  399. g - p_color.g,
  400. b - p_color.b,
  401. a - p_color.a);
  402. }
  403. void Color::operator-=(const Color &p_color) {
  404. r = r - p_color.r;
  405. g = g - p_color.g;
  406. b = b - p_color.b;
  407. a = a - p_color.a;
  408. }
  409. Color Color::operator*(const Color &p_color) const {
  410. return Color(
  411. r * p_color.r,
  412. g * p_color.g,
  413. b * p_color.b,
  414. a * p_color.a);
  415. }
  416. Color Color::operator*(const real_t &rvalue) const {
  417. return Color(
  418. r * rvalue,
  419. g * rvalue,
  420. b * rvalue,
  421. a * rvalue);
  422. }
  423. void Color::operator*=(const Color &p_color) {
  424. r = r * p_color.r;
  425. g = g * p_color.g;
  426. b = b * p_color.b;
  427. a = a * p_color.a;
  428. }
  429. void Color::operator*=(const real_t &rvalue) {
  430. r = r * rvalue;
  431. g = g * rvalue;
  432. b = b * rvalue;
  433. a = a * rvalue;
  434. }
  435. Color Color::operator/(const Color &p_color) const {
  436. return Color(
  437. r / p_color.r,
  438. g / p_color.g,
  439. b / p_color.b,
  440. a / p_color.a);
  441. }
  442. Color Color::operator/(const real_t &rvalue) const {
  443. return Color(
  444. r / rvalue,
  445. g / rvalue,
  446. b / rvalue,
  447. a / rvalue);
  448. }
  449. void Color::operator/=(const Color &p_color) {
  450. r = r / p_color.r;
  451. g = g / p_color.g;
  452. b = b / p_color.b;
  453. a = a / p_color.a;
  454. }
  455. void Color::operator/=(const real_t &rvalue) {
  456. if (rvalue == 0) {
  457. r = 1.0;
  458. g = 1.0;
  459. b = 1.0;
  460. a = 1.0;
  461. } else {
  462. r = r / rvalue;
  463. g = g / rvalue;
  464. b = b / rvalue;
  465. a = a / rvalue;
  466. }
  467. };
  468. Color Color::operator-() const {
  469. return Color(
  470. 1.0 - r,
  471. 1.0 - g,
  472. 1.0 - b,
  473. 1.0 - a);
  474. }