color.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "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_32() const {
  46. uint32_t c = (uint8_t)(a * 255);
  47. c <<= 8;
  48. c |= (uint8_t)(r * 255);
  49. c <<= 8;
  50. c |= (uint8_t)(g * 255);
  51. c <<= 8;
  52. c |= (uint8_t)(b * 255);
  53. return c;
  54. }
  55. float Color::get_h() const {
  56. float min = MIN(r, g);
  57. min = MIN(min, b);
  58. float max = MAX(r, g);
  59. max = MAX(max, b);
  60. float delta = max - min;
  61. if (delta == 0)
  62. return 0;
  63. float h;
  64. if (r == max)
  65. h = (g - b) / delta; // between yellow & magenta
  66. else if (g == max)
  67. h = 2 + (b - r) / delta; // between cyan & yellow
  68. else
  69. h = 4 + (r - g) / delta; // between magenta & cyan
  70. h /= 6.0;
  71. if (h < 0)
  72. h += 1.0;
  73. return h;
  74. }
  75. float Color::get_s() const {
  76. float min = MIN(r, g);
  77. min = MIN(min, b);
  78. float max = MAX(r, g);
  79. max = MAX(max, b);
  80. float delta = max - min;
  81. return (max != 0) ? (delta / max) : 0;
  82. }
  83. float Color::get_v() const {
  84. float max = MAX(r, g);
  85. max = MAX(max, b);
  86. return max;
  87. }
  88. void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
  89. int i;
  90. float f, p, q, t;
  91. a = p_alpha;
  92. if (p_s == 0) {
  93. // acp_hromatic (grey)
  94. r = g = b = p_v;
  95. return;
  96. }
  97. p_h *= 6.0;
  98. p_h = Math::fmod(p_h, 6);
  99. i = Math::floor(p_h);
  100. f = p_h - i;
  101. p = p_v * (1 - p_s);
  102. q = p_v * (1 - p_s * f);
  103. t = p_v * (1 - p_s * (1 - f));
  104. switch (i) {
  105. case 0: // Red is the dominant color
  106. r = p_v;
  107. g = t;
  108. b = p;
  109. break;
  110. case 1: // Green is the dominant color
  111. r = q;
  112. g = p_v;
  113. b = p;
  114. break;
  115. case 2:
  116. r = p;
  117. g = p_v;
  118. b = t;
  119. break;
  120. case 3: // Blue is the dominant color
  121. r = p;
  122. g = q;
  123. b = p_v;
  124. break;
  125. case 4:
  126. r = t;
  127. g = p;
  128. b = p_v;
  129. break;
  130. default: // (5) Red is the dominant color
  131. r = p_v;
  132. g = p;
  133. b = q;
  134. break;
  135. }
  136. }
  137. void Color::invert() {
  138. r = 1.0 - r;
  139. g = 1.0 - g;
  140. b = 1.0 - b;
  141. }
  142. void Color::contrast() {
  143. r = Math::fmod(r + 0.5, 1.0);
  144. g = Math::fmod(g + 0.5, 1.0);
  145. b = Math::fmod(b + 0.5, 1.0);
  146. }
  147. Color Color::hex(uint32_t p_hex) {
  148. float a = (p_hex & 0xFF) / 255.0;
  149. p_hex >>= 8;
  150. float b = (p_hex & 0xFF) / 255.0;
  151. p_hex >>= 8;
  152. float g = (p_hex & 0xFF) / 255.0;
  153. p_hex >>= 8;
  154. float r = (p_hex & 0xFF) / 255.0;
  155. return Color(r, g, b, a);
  156. }
  157. static float _parse_col(const String &p_str, int p_ofs) {
  158. int ig = 0;
  159. for (int i = 0; i < 2; i++) {
  160. int c = p_str[i + p_ofs];
  161. int v = 0;
  162. if (c >= '0' && c <= '9') {
  163. v = c - '0';
  164. } else if (c >= 'a' && c <= 'f') {
  165. v = c - 'a';
  166. v += 10;
  167. } else if (c >= 'A' && c <= 'F') {
  168. v = c - 'A';
  169. v += 10;
  170. } else {
  171. return -1;
  172. }
  173. if (i == 0)
  174. ig += v * 16;
  175. else
  176. ig += v;
  177. }
  178. return ig;
  179. }
  180. Color Color::inverted() const {
  181. Color c = *this;
  182. c.invert();
  183. return c;
  184. }
  185. Color Color::contrasted() const {
  186. Color c = *this;
  187. c.contrast();
  188. return c;
  189. }
  190. Color Color::html(const String &p_color) {
  191. String color = p_color;
  192. if (color.length() == 0)
  193. return Color();
  194. if (color[0] == '#')
  195. color = color.substr(1, color.length() - 1);
  196. bool alpha = false;
  197. if (color.length() == 8) {
  198. alpha = true;
  199. } else if (color.length() == 6) {
  200. alpha = false;
  201. } else {
  202. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  203. ERR_FAIL_V(Color());
  204. }
  205. int a = 255;
  206. if (alpha) {
  207. a = _parse_col(color, 0);
  208. if (a < 0) {
  209. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  210. ERR_FAIL_V(Color());
  211. }
  212. }
  213. int from = alpha ? 2 : 0;
  214. int r = _parse_col(color, from + 0);
  215. if (r < 0) {
  216. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  217. ERR_FAIL_V(Color());
  218. }
  219. int g = _parse_col(color, from + 2);
  220. if (g < 0) {
  221. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  222. ERR_FAIL_V(Color());
  223. }
  224. int b = _parse_col(color, from + 4);
  225. if (b < 0) {
  226. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  227. ERR_FAIL_V(Color());
  228. }
  229. return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  230. }
  231. bool Color::html_is_valid(const String &p_color) {
  232. String color = p_color;
  233. if (color.length() == 0)
  234. return false;
  235. if (color[0] == '#')
  236. color = color.substr(1, color.length() - 1);
  237. bool alpha = false;
  238. if (color.length() == 8) {
  239. alpha = true;
  240. } else if (color.length() == 6) {
  241. alpha = false;
  242. } else {
  243. return false;
  244. }
  245. int a = 255;
  246. if (alpha) {
  247. a = _parse_col(color, 0);
  248. if (a < 0) {
  249. return false;
  250. }
  251. }
  252. int from = alpha ? 2 : 0;
  253. int r = _parse_col(color, from + 0);
  254. if (r < 0) {
  255. return false;
  256. }
  257. int g = _parse_col(color, from + 2);
  258. if (g < 0) {
  259. return false;
  260. }
  261. int b = _parse_col(color, from + 4);
  262. if (b < 0) {
  263. return false;
  264. }
  265. return true;
  266. }
  267. Color Color::named(const String &p_name) {
  268. if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
  269. String name = p_name;
  270. // Normalize name
  271. name = name.replace(" ", "");
  272. name = name.replace("-", "");
  273. name = name.replace("_", "");
  274. name = name.replace("'", "");
  275. name = name.replace(".", "");
  276. name = name.to_lower();
  277. const Map<String, Color>::Element *color = _named_colors.find(name);
  278. if (color) {
  279. return color->value();
  280. } else {
  281. ERR_EXPLAIN("Invalid Color Name: " + p_name);
  282. ERR_FAIL_V(Color());
  283. }
  284. }
  285. void Color::cleanup() {
  286. _named_colors.clear();
  287. }
  288. String _to_hex(float p_val) {
  289. int v = p_val * 255;
  290. v = CLAMP(v, 0, 255);
  291. String ret;
  292. for (int i = 0; i < 2; i++) {
  293. CharType c[2] = { 0, 0 };
  294. int lv = v & 0xF;
  295. if (lv < 10)
  296. c[0] = '0' + lv;
  297. else
  298. c[0] = 'a' + lv - 10;
  299. v >>= 4;
  300. String cs = (const CharType *)c;
  301. ret = cs + ret;
  302. }
  303. return ret;
  304. }
  305. String Color::to_html(bool p_alpha) const {
  306. String txt;
  307. txt += _to_hex(r);
  308. txt += _to_hex(g);
  309. txt += _to_hex(b);
  310. if (p_alpha)
  311. txt = _to_hex(a) + txt;
  312. return txt;
  313. }
  314. float Color::gray() const {
  315. return (r + g + b) / 3.0;
  316. }
  317. Color::operator String() const {
  318. return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a);
  319. }