color.cpp 6.9 KB

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