color.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*************************************************************************/
  2. /* color.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "color.h"
  30. #include "color_names.inc"
  31. #include "map.h"
  32. #include "math_funcs.h"
  33. #include "print_string.h"
  34. uint32_t Color::to_ARGB32() const {
  35. uint32_t c = (uint8_t)(a * 255);
  36. c <<= 8;
  37. c |= (uint8_t)(r * 255);
  38. c <<= 8;
  39. c |= (uint8_t)(g * 255);
  40. c <<= 8;
  41. c |= (uint8_t)(b * 255);
  42. return c;
  43. }
  44. uint32_t Color::to_32() const {
  45. uint32_t c = (uint8_t)(a * 255);
  46. c <<= 8;
  47. c |= (uint8_t)(r * 255);
  48. c <<= 8;
  49. c |= (uint8_t)(g * 255);
  50. c <<= 8;
  51. c |= (uint8_t)(b * 255);
  52. return c;
  53. }
  54. float Color::get_h() const {
  55. float min = MIN(r, g);
  56. min = MIN(min, b);
  57. float max = MAX(r, g);
  58. max = MAX(max, b);
  59. float delta = max - min;
  60. if (delta == 0)
  61. return 0;
  62. float h;
  63. if (r == max)
  64. h = (g - b) / delta; // between yellow & magenta
  65. else if (g == max)
  66. h = 2 + (b - r) / delta; // between cyan & yellow
  67. else
  68. h = 4 + (r - g) / delta; // between magenta & cyan
  69. h /= 6.0;
  70. if (h < 0)
  71. h += 1.0;
  72. return h;
  73. }
  74. float Color::get_s() const {
  75. float min = MIN(r, g);
  76. min = MIN(min, b);
  77. float max = MAX(r, g);
  78. max = MAX(max, b);
  79. float delta = max - min;
  80. return (max != 0) ? (delta / max) : 0;
  81. }
  82. float Color::get_v() const {
  83. float max = MAX(r, g);
  84. max = MAX(max, b);
  85. return max;
  86. }
  87. void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) {
  88. int i;
  89. float f, p, q, t;
  90. a = p_alpha;
  91. if (p_s == 0) {
  92. // acp_hromatic (grey)
  93. r = g = b = p_v;
  94. return;
  95. }
  96. p_h *= 6.0;
  97. p_h = Math::fmod(p_h, 6);
  98. i = Math::floor(p_h);
  99. f = p_h - i;
  100. p = p_v * (1 - p_s);
  101. q = p_v * (1 - p_s * f);
  102. t = p_v * (1 - p_s * (1 - f));
  103. switch (i) {
  104. case 0: // Red is the dominant color
  105. r = p_v;
  106. g = t;
  107. b = p;
  108. break;
  109. case 1: // Green is the dominant color
  110. r = q;
  111. g = p_v;
  112. b = p;
  113. break;
  114. case 2:
  115. r = p;
  116. g = p_v;
  117. b = t;
  118. break;
  119. case 3: // Blue is the dominant color
  120. r = p;
  121. g = q;
  122. b = p_v;
  123. break;
  124. case 4:
  125. r = t;
  126. g = p;
  127. b = p_v;
  128. break;
  129. default: // (5) Red is the dominant color
  130. r = p_v;
  131. g = p;
  132. b = q;
  133. break;
  134. }
  135. }
  136. void Color::invert() {
  137. r = 1.0 - r;
  138. g = 1.0 - g;
  139. b = 1.0 - b;
  140. }
  141. void Color::contrast() {
  142. r = Math::fmod(r + 0.5, 1.0);
  143. g = Math::fmod(g + 0.5, 1.0);
  144. b = Math::fmod(b + 0.5, 1.0);
  145. }
  146. Color Color::hex(uint32_t p_hex) {
  147. float a = (p_hex & 0xFF) / 255.0;
  148. p_hex >>= 8;
  149. float b = (p_hex & 0xFF) / 255.0;
  150. p_hex >>= 8;
  151. float g = (p_hex & 0xFF) / 255.0;
  152. p_hex >>= 8;
  153. float r = (p_hex & 0xFF) / 255.0;
  154. return Color(r, g, b, a);
  155. }
  156. static float _parse_col(const String &p_str, int p_ofs) {
  157. int ig = 0;
  158. for (int i = 0; i < 2; i++) {
  159. int c = p_str[i + p_ofs];
  160. int v = 0;
  161. if (c >= '0' && c <= '9') {
  162. v = c - '0';
  163. } else if (c >= 'a' && c <= 'f') {
  164. v = c - 'a';
  165. v += 10;
  166. } else if (c >= 'A' && c <= 'F') {
  167. v = c - 'A';
  168. v += 10;
  169. } else {
  170. return -1;
  171. }
  172. if (i == 0)
  173. ig += v * 16;
  174. else
  175. ig += v;
  176. }
  177. return ig;
  178. }
  179. Color Color::inverted() const {
  180. Color c = *this;
  181. c.invert();
  182. return c;
  183. }
  184. Color Color::contrasted() const {
  185. Color c = *this;
  186. c.contrast();
  187. return c;
  188. }
  189. Color Color::html(const String &p_color) {
  190. String color = p_color;
  191. if (color.length() == 0)
  192. return Color();
  193. if (color[0] == '#')
  194. color = color.substr(1, color.length() - 1);
  195. bool alpha = false;
  196. if (color.length() == 8) {
  197. alpha = true;
  198. } else if (color.length() == 6) {
  199. alpha = false;
  200. } else {
  201. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  202. ERR_FAIL_V(Color());
  203. }
  204. int a = 255;
  205. if (alpha) {
  206. a = _parse_col(color, 0);
  207. if (a < 0) {
  208. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  209. ERR_FAIL_V(Color());
  210. }
  211. }
  212. int from = alpha ? 2 : 0;
  213. int r = _parse_col(color, from + 0);
  214. if (r < 0) {
  215. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  216. ERR_FAIL_V(Color());
  217. }
  218. int g = _parse_col(color, from + 2);
  219. if (g < 0) {
  220. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  221. ERR_FAIL_V(Color());
  222. }
  223. int b = _parse_col(color, from + 4);
  224. if (b < 0) {
  225. ERR_EXPLAIN("Invalid Color Code: " + p_color);
  226. ERR_FAIL_V(Color());
  227. }
  228. return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  229. }
  230. bool Color::html_is_valid(const String &p_color) {
  231. String color = p_color;
  232. if (color.length() == 0)
  233. return false;
  234. if (color[0] == '#')
  235. color = color.substr(1, color.length() - 1);
  236. bool alpha = false;
  237. if (color.length() == 8) {
  238. alpha = true;
  239. } else if (color.length() == 6) {
  240. alpha = false;
  241. } else {
  242. return false;
  243. }
  244. int a = 255;
  245. if (alpha) {
  246. a = _parse_col(color, 0);
  247. if (a < 0) {
  248. return false;
  249. }
  250. }
  251. int from = alpha ? 2 : 0;
  252. int r = _parse_col(color, from + 0);
  253. if (r < 0) {
  254. return false;
  255. }
  256. int g = _parse_col(color, from + 2);
  257. if (g < 0) {
  258. return false;
  259. }
  260. int b = _parse_col(color, from + 4);
  261. if (b < 0) {
  262. return false;
  263. }
  264. return true;
  265. }
  266. Color Color::named(const String &p_name) {
  267. if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
  268. String name = p_name;
  269. // Normalize name
  270. name = name.replace(" ", "");
  271. name = name.replace("-", "");
  272. name = name.replace("_", "");
  273. name = name.replace("'", "");
  274. name = name.replace(".", "");
  275. name = name.to_lower();
  276. const Map<String, Color>::Element *color = _named_colors.find(name);
  277. if (color) {
  278. return color->value();
  279. } else {
  280. ERR_EXPLAIN("Invalid Color Name: " + p_name);
  281. ERR_FAIL_V(Color());
  282. }
  283. }
  284. String _to_hex(float p_val) {
  285. int v = p_val * 255;
  286. v = CLAMP(v, 0, 255);
  287. String ret;
  288. for (int i = 0; i < 2; i++) {
  289. CharType c[2] = { 0, 0 };
  290. int lv = v & 0xF;
  291. if (lv < 10)
  292. c[0] = '0' + lv;
  293. else
  294. c[0] = 'a' + lv - 10;
  295. v >>= 4;
  296. String cs = (const CharType *)c;
  297. ret = cs + ret;
  298. }
  299. return ret;
  300. }
  301. String Color::to_html(bool p_alpha) const {
  302. String txt;
  303. txt += _to_hex(r);
  304. txt += _to_hex(g);
  305. txt += _to_hex(b);
  306. if (p_alpha)
  307. txt = _to_hex(a) + txt;
  308. return txt;
  309. }
  310. float Color::gray() const {
  311. return (r + g + b) / 3.0;
  312. }
  313. Color::operator String() const {
  314. return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a);
  315. }