y_colours.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #if defined _INC_y_colours
  2. #endinput
  3. #endif
  4. #define _INC_y_colours
  5. /**
  6. * <library name="y_colours">
  7. * <section>
  8. * Description
  9. * </section>
  10. * This holds the colour information that used to be part of the text system
  11. * but which is far more useful than just for text. This now supports the full
  12. * set of X11 colours, both by name and by definition. You can also define
  13. * your own if you so choose (up to 32 - should be enough given that this
  14. * includes the X11 colours).
  15. * <section>
  16. * Version
  17. * </section>
  18. * 1.0
  19. * </library>
  20. *//** *//*
  21. Legal:
  22. Version: MPL 1.1
  23. The contents of this file are subject to the Mozilla Public License Version
  24. 1.1 the "License"; you may not use this file except in compliance with
  25. the License. You may obtain a copy of the License at
  26. http://www.mozilla.org/MPL/
  27. Software distributed under the License is distributed on an "AS IS" basis,
  28. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  29. for the specific language governing rights and limitations under the
  30. License.
  31. The Original Code is the YSI framework.
  32. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  33. Portions created by the Initial Developer are Copyright C 2011
  34. the Initial Developer. All Rights Reserved.
  35. Contributors:
  36. Y_Less
  37. koolk
  38. JoeBullet/Google63
  39. g_aSlice/Slice
  40. Misiur
  41. samphunter
  42. tianmeta
  43. maddinat0r
  44. spacemud
  45. Crayder
  46. Dayvison
  47. Ahmad45123
  48. Zeex
  49. irinel1996
  50. Yiin-
  51. Chaprnks
  52. Konstantinos
  53. Masterchen09
  54. Southclaws
  55. PatchwerkQWER
  56. m0k1
  57. paulommu
  58. udan111
  59. Thanks:
  60. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  61. ZeeX - Very productive conversations.
  62. koolk - IsPlayerinAreaEx code.
  63. TheAlpha - Danish translation.
  64. breadfish - German translation.
  65. Fireburn - Dutch translation.
  66. yom - French translation.
  67. 50p - Polish translation.
  68. Zamaroht - Spanish translation.
  69. Los - Portuguese translation.
  70. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  71. me to strive to better.
  72. Pixels^ - Running XScripters where the idea was born.
  73. Matite - Pestering me to release it and using it.
  74. Very special thanks to:
  75. Thiadmer - PAWN, whose limits continue to amaze me!
  76. Kye/Kalcor - SA:MP.
  77. SA:MP Team past, present and future - SA:MP.
  78. Optional plugins:
  79. Gamer_Z - GPS.
  80. Incognito - Streamer.
  81. Me - sscanf2, fixes2, Whirlpool.
  82. */
  83. #include "..\YSI_Internal\y_version"
  84. #include "..\YSI_Coding\y_stringhash"
  85. #include "..\YSI_Core\y_debug"
  86. #include "..\YSI_Coding\y_remote"
  87. #include "..\YSI_Core\y_utils"
  88. #if !defined MAX_TEXT_COLOURS
  89. #if defined MAX_TEXT_COLORS
  90. #define MAX_TEXT_COLOURS MAX_TEXT_COLORS
  91. #else
  92. #define MAX_TEXT_COLOURS (32)
  93. #endif
  94. #endif
  95. #if !defined NO_X11_COLOURS && !defined NO_X11_COLORS && !defined YSI_NO_X11
  96. #define _YSI_USE_X11 true
  97. #else
  98. #define _YSI_USE_X11 false
  99. #endif
  100. // Note that these are VERY unsafe, they're not designed for general use!
  101. #define MK_RGB(%0,%1,%2) ((%0)<<24|(%1)<<16|(%2)<<8|0xAA)
  102. #define MK_RGBA(%0,%1,%2,%3) ((%0)<<24|(%1)<<16|(%2)<<8|(%3))
  103. #define MK_D_RGB(%0,%1,%2) ((%0)<<24|(%1)<<16|(%2)<<8|0xAA)
  104. #define MK_D_RGBA(%0,%1,%2,%3) ((%0)<<24|(%1)<<16|(%2)<<8|(%3))
  105. #define MK_S_RGB(%0,%1,%2) #%0#%1#%2
  106. #define MK_S_RGBA(%0,%1,%2,%3) #%0#%1#%2
  107. #define MK_S_DEF(%0) ((%0) >>> 8)
  108. #if _YSI_USE_X11
  109. #include "y_colours/x11def"
  110. #endif
  111. #include "y_colours/gtdef"
  112. // Special macro for adding game text colours.
  113. #define GT(%0) GT_%0
  114. #define GT_~%0~ "{"#AeC01e"}{"#_GT_%0"}"
  115. // This does redefine a possible colour, but with a very obscure pattern. Also
  116. // with awkward mixed case to not match "AEC01E" or "aec01e".
  117. #define AeC01e"}{"#%0"}"~h~ AeC01e"}{"#%0h"}"
  118. // Main code start. Apparently there is a copy of the hash code in y_td.
  119. #define COLOUR_NAME_HASH(%0) YHash((%0), false, hash_bernstein, (YSI_g_sCheckSpaces) ? (-1) : (strfind((%0), " ")))
  120. #define COLOR_NAME_HASH COLOUR_NAME_HASH
  121. #if !defined X11_ALLOW_SPACES
  122. #define X11_ALLOW_SPACES (false)
  123. #endif
  124. static stock
  125. YSI_g_sColours[MAX_TEXT_COLOURS][2],
  126. bool:YSI_g_sCheckSpaces = X11_ALLOW_SPACES;
  127. remotefunc void:_SetColoursCanHaveSpaces(bool:set)
  128. {
  129. YSI_g_sCheckSpaces = set;
  130. }
  131. #define SetColorsCanHaveSpaces SetColoursCanHaveSpaces
  132. stock SetColoursCanHaveSpaces(bool:set)
  133. {
  134. // Set this true/false in ALL scripts, since the text system is distributed.
  135. broadcastfunc _SetColoursCanHaveSpaces(set);
  136. //return 1;
  137. }
  138. #define SetColor SetColour
  139. stock SetColour(const name[], color)
  140. {
  141. P:3("SetColour called: \"%s\", %i", name, color);
  142. #if _YSI_USE_X11
  143. //if (name[0] | 0x20 == 'x' && name[1] == '1' && name[2] == '1')
  144. if (!strcmp(name, "X11", true, 3))
  145. {
  146. if (name[3] == ' ' || name[3] == '_')
  147. {
  148. SetColourHash(COLOUR_NAME_HASH(name[4]), color);
  149. }
  150. else
  151. {
  152. SetColourHash(COLOUR_NAME_HASH(name[3]), color);
  153. }
  154. }
  155. else
  156. #endif
  157. {
  158. SetColourHash(COLOUR_NAME_HASH(name), color);
  159. }
  160. }
  161. stock SetColourHash(hash, color)
  162. {
  163. broadcastfunc _SetColourHash(hash, color);
  164. }
  165. #define SetColorHash SetColourHash
  166. remotefunc void:_SetColourHash(hash, color)
  167. {
  168. P:3("SetColourHash called: %i, %i", hash, color);
  169. color &= 0xFFFFFF00;
  170. #if _YSI_USE_X11
  171. new
  172. id = GetColourHash(hash);
  173. if (id != 0)
  174. {
  175. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  176. {
  177. new
  178. iColor = YSI_g_sColours[i][0];
  179. if (iColor == hash)
  180. {
  181. YSI_g_sColours[i][1] = color;
  182. return; // i;
  183. }
  184. else if (iColor == 0)
  185. {
  186. // Tried to rename an X11 colour.
  187. return; // -1;
  188. }
  189. }
  190. }
  191. #endif
  192. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  193. {
  194. new
  195. iColor = YSI_g_sColours[i][0];
  196. if (iColor == hash || iColor == 0)
  197. {
  198. YSI_g_sColours[i][0] = hash;
  199. YSI_g_sColours[i][1] = color;
  200. return; // i;
  201. }
  202. }
  203. return; // -1;
  204. }
  205. // This now uses REVERSE bernstein hash for switch compatibility.
  206. #define GetColor GetColour
  207. stock GetColour(const name[], alpha = 0xAA)
  208. {
  209. P:3("GetColour called: \"%s\", %i", name, alpha);
  210. //if (name[0] | 0x20 == 'x' && name[1] == '1' && name[2] == '2')
  211. #if _YSI_USE_X11
  212. if (!strcmp(name, "X11", true, 3))
  213. {
  214. if ((YSI_g_sCheckSpaces && name[3] == ' ') || name[3] == '_')
  215. {
  216. return GetColourHash(COLOUR_NAME_HASH(name[4]), alpha);
  217. }
  218. else
  219. {
  220. return GetColourHash(COLOUR_NAME_HASH(name[3]), alpha);
  221. }
  222. }
  223. else
  224. #endif
  225. {
  226. return GetColourHash(COLOUR_NAME_HASH(name), alpha);
  227. }
  228. }
  229. #define GetColorStream GetColourStream
  230. stock GetColourStream(const str[], &idx, alpha = 0xAA)
  231. {
  232. P:3("GetColourStream called: \"%s\", %i, %i", str, idx, alpha);
  233. // This doesn't work because the hash is backwards, not forwards. You can't
  234. // do a reverse hash incrementally. Actually you can, but not well.
  235. new
  236. pos = idx,
  237. ret = -1;
  238. #if _YSI_USE_X11
  239. //new
  240. // bool:checkSpace = YSI_g_sCheckSpaces;
  241. if (!strcmp(str[pos], "X11", true, 3))
  242. {
  243. pos += 3;
  244. if ((YSI_g_sCheckSpaces && str[pos] == ' ') || str[pos] == '_')
  245. {
  246. ++pos;
  247. }
  248. }
  249. ret = Colours_DoHashParse(str[pos], pos, YSI_g_sCheckSpaces);
  250. if (ret != -1)
  251. {
  252. idx = pos;
  253. return ret | alpha;
  254. }
  255. #endif
  256. // Do the incremental hash. This *should* be "idx = pos + 16;", but it
  257. // isn't as you shouldn't prefix custom colours with "X11" as they're not.
  258. new
  259. test = min(strlen(str[pos]), 16);
  260. while (test)
  261. {
  262. // This is a greedy test, and restricts custom text to 16 characters.
  263. ret = YHash(str[pos], false, hash_bernstein, test);
  264. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  265. {
  266. new
  267. iColor = YSI_g_sColours[i][0];
  268. if (iColor == ret)
  269. {
  270. idx = pos + test;
  271. return alpha | YSI_g_sColours[i][1];
  272. }
  273. else if (iColor == 0)
  274. {
  275. break;
  276. }
  277. }
  278. --test;
  279. }
  280. return 0;
  281. }
  282. #define GetColorHash GetColourHash
  283. stock GetColourHash(hash, alpha = 0xAA)
  284. {
  285. P:3("GetColourHash called: %i, %i", hash, alpha);
  286. alpha &= 0xFF;
  287. // Do the REVERSE hash from YHash
  288. #pragma tabsize 4
  289. #if _YSI_USE_X11
  290. #include "y_colours/x11switch"
  291. #endif
  292. #pragma tabsize 4
  293. // Do the default code here.
  294. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  295. {
  296. new
  297. iColor = YSI_g_sColours[i][0];
  298. if (iColor == hash)
  299. {
  300. return alpha | YSI_g_sColours[i][1];
  301. }
  302. else if (iColor == 0)
  303. {
  304. return 0;
  305. }
  306. }
  307. return 0;
  308. }
  309. #if _YSI_USE_X11
  310. static stock Colours_DoHashParse(const str[], &idx, bool:checkSpace)
  311. {
  312. // You can't use custom colours with the "#COLOR" format, but you can
  313. // with the "{COLOR}" format, this is to save on memory. It is in
  314. // theory possible to get it working with the hash format using an
  315. // incremental hash and compare. NOW SUPPORTED!
  316. #include "y_colours/x11parse"
  317. #pragma tabsize 4
  318. return -1;
  319. }
  320. #endif
  321. #define _COLOUR_GT_SPACE_DEF_0(%0) SAMP_GAME_TEXT_%0
  322. #define _COLOUR_GT_SPACE_DEF_1(%0) SAMP_GAME_TEXT_%0H
  323. #define _COLOUR_GT_SPACE_DEF_2(%0) SAMP_GAME_TEXT_%0HH
  324. #define _COLOUR_GT_SPACE_DEF_3(%0) SAMP_GAME_TEXT_%0HHH
  325. #define _COLOUR_GT_SPACE_DEF_4(%0) SAMP_GAME_TEXT_%0HHHH
  326. #define _COLOUR_GT_SPACE_DEF_5(%0) SAMP_GAME_TEXT_%0HHHHH
  327. #define _COLOUR_GT_SPACE(%0,%1) {('%0'|0x20)|(%1<<8),_COLOUR_GT_SPACE_DEF_%1(%0)>>>24,_COLOUR_GT_SPACE_DEF_%1(%0)>>16&0xFF,_COLOUR_GT_SPACE_DEF_%1(%0)>>8&0xFF}
  328. #define Colors_SAMPToGT Colours_SAMPToGT
  329. stock Colours_SAMPToGT(colour, start)
  330. {
  331. P:4("Colours_SAMPToGT called: %i", colour);
  332. // Find the closest matching game text colour to the given SA:MP colour.
  333. static const
  334. sc_aColours[][4] =
  335. {
  336. // These can't be used as you can't set text to them. You can
  337. // however use them at the start by not changing.
  338. _COLOUR_GT_SPACE(X,0), _COLOUR_GT_SPACE(X,1),
  339. _COLOUR_GT_SPACE(X,2),
  340. _COLOUR_GT_SPACE(R,0), _COLOUR_GT_SPACE(R,1),
  341. _COLOUR_GT_SPACE(R,2), _COLOUR_GT_SPACE(R,3),
  342. _COLOUR_GT_SPACE(R,4), _COLOUR_GT_SPACE(R,5),
  343. _COLOUR_GT_SPACE(G,0), _COLOUR_GT_SPACE(G,1),
  344. _COLOUR_GT_SPACE(G,2), _COLOUR_GT_SPACE(G,3),
  345. //_COLOUR_GT_SPACE(G,4), // Don't want this twice (see Y2).
  346. _COLOUR_GT_SPACE(B,0), _COLOUR_GT_SPACE(B,1),
  347. _COLOUR_GT_SPACE(B,2), _COLOUR_GT_SPACE(B,3),
  348. _COLOUR_GT_SPACE(Y,0), _COLOUR_GT_SPACE(Y,1),
  349. _COLOUR_GT_SPACE(Y,2),
  350. _COLOUR_GT_SPACE(P,0), _COLOUR_GT_SPACE(P,1),
  351. _COLOUR_GT_SPACE(P,2),
  352. _COLOUR_GT_SPACE(W,0),
  353. _COLOUR_GT_SPACE(L,0)
  354. };
  355. new
  356. dist = cellmax,
  357. found = '\0',
  358. r = colour >> 16,
  359. g = (colour >> 8) & 0xFF,
  360. b = colour & 0xFF,
  361. tr, tg, tb, cur;
  362. for ( ; start != sizeof (sc_aColours); ++start)
  363. {
  364. tr = r - sc_aColours[start][1],
  365. tg = g - sc_aColours[start][2],
  366. tb = b - sc_aColours[start][3],
  367. cur = (tr * tr) + (tg * tg) + (tb * tb);
  368. P:6("Colours_SAMPToGT: Test %x %d", sc_aColours[start][0], cur);
  369. if (cur < dist)
  370. {
  371. P:6("Colours_SAMPToGT: Change %x %d", found, dist);
  372. // This may sometimes give odd results in fades for draw results.
  373. // Though I've not seen it happen yet.
  374. dist = cur,
  375. found = sc_aColours[start][0];
  376. }
  377. }
  378. return found;
  379. }
  380. stock Colours_EncodeGameText(str[])
  381. {
  382. new
  383. ret;
  384. if (str[0] == '~')
  385. {
  386. switch (str[1] | 0x20)
  387. {
  388. case 'r', 'g', 'b', 'y', 'p', 'w', 'l', 'x':
  389. {
  390. ret = str[1] | 0x20;
  391. }
  392. case 'h':
  393. {
  394. ret = 0x100 | 'x';
  395. }
  396. default: return 0;
  397. }
  398. if (str[2] != '~') return 0;
  399. new
  400. pos = 3;
  401. while (str[pos++] == '~' && str[pos++] | 0x20 == 'h' && str[pos++] == '~')
  402. {
  403. ret += 0x100;
  404. }
  405. }
  406. return ret;
  407. }