y_colours.inc 11 KB

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