y_colours.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. 20/02/12:
  49. Added broadcastfunc of colours.
  50. 29/11/10:
  51. Added the ability to use "X11" prefixes in strings.
  52. Added colourspace resolution for converting {FF0000} to ~r~.
  53. 25/11/10:
  54. First version.
  55. </remarks>
  56. \**--------------------------------------------------------------------------**/
  57. #include "internal\y_version"
  58. #include "y_stringhash"
  59. #include "y_debug"
  60. #include "y_remote"
  61. #include "y_utils"
  62. #if !defined MAX_TEXT_COLOURS
  63. #if defined MAX_TEXT_COLORS
  64. #define MAX_TEXT_COLOURS MAX_TEXT_COLORS
  65. #else
  66. #define MAX_TEXT_COLOURS (32)
  67. #endif
  68. #endif
  69. #if !defined NO_X11_COLOURS && !defined NO_X11_COLORS && !defined YSI_NO_X11
  70. #define _YSI_USE_X11 true
  71. #else
  72. #define _YSI_USE_X11 false
  73. #endif
  74. // Note that these are VERY unsafe, they're not designed for general use!
  75. #define MK_RGB(%0,%1,%2) ((%0)<<24|(%1)<<16|(%2)<<8|0xAA)
  76. #define MK_RGBA(%0,%1,%2,%3) ((%0)<<24|(%1)<<16|(%2)<<8|(%3))
  77. #define MK_D_RGB(%0,%1,%2) ((%0)<<24|(%1)<<16|(%2)<<8|0xAA)
  78. #define MK_D_RGBA(%0,%1,%2,%3) ((%0)<<24|(%1)<<16|(%2)<<8|(%3))
  79. #define MK_S_RGB(%0,%1,%2) #%0#%1#%2
  80. #define MK_S_RGBA(%0,%1,%2,%3) #%0#%1#%2
  81. #define MK_S_DEF(%0) ((%0) >>> 8)
  82. // These are the RGB values for the gametext colours (got manually using GIMP).
  83. // These need to be added in to the main system so you can type and use them.
  84. #define SAMP_GAME_TEXT MK_RGB(144, 98, 16)
  85. #define SAMP_GAME_TEXT_ MK_RGB(144, 98, 16)
  86. #define SAMP_GAME_TEXT_H MK_RGB(216, 147, 24)
  87. #define SAMP_GAME_TEXT_HH MK_RGB(255, 255, 54)
  88. #define SAMP_GAME_TEXT_X MK_RGB(144, 98, 16)
  89. #define SAMP_GAME_TEXT_XH MK_RGB(216, 147, 24)
  90. #define SAMP_GAME_TEXT_XHH MK_RGB(255, 255, 54)
  91. #define SAMP_GAME_TEXT_R MK_RGB(180, 25, 29)
  92. #define SAMP_GAME_TEXT_RH MK_RGB(255, 37, 43)
  93. #define SAMP_GAME_TEXT_RHH MK_RGB(255, 55, 64)
  94. #define SAMP_GAME_TEXT_RHHH MK_RGB(255, 82, 96)
  95. #define SAMP_GAME_TEXT_RHHHH MK_RGB(255, 123, 144)
  96. #define SAMP_GAME_TEXT_RHHHHH MK_RGB(255, 184, 216)
  97. #define SAMP_GAME_TEXT_G MK_RGB( 54, 104, 44)
  98. #define SAMP_GAME_TEXT_GH MK_RGB( 81, 156, 66)
  99. #define SAMP_GAME_TEXT_GHH MK_RGB(121, 234, 99)
  100. #define SAMP_GAME_TEXT_GHHH MK_RGB(181, 255, 148)
  101. #define SAMP_GAME_TEXT_GHHHH MK_RGB(255, 255, 222)
  102. #define SAMP_GAME_TEXT_B MK_RGB( 50, 60, 127)
  103. #define SAMP_GAME_TEXT_BH MK_RGB( 75, 90, 190)
  104. #define SAMP_GAME_TEXT_BHH MK_RGB(112, 135, 255)
  105. #define SAMP_GAME_TEXT_BHHH MK_RGB(168, 202, 255)
  106. #define SAMP_GAME_TEXT_Y MK_RGB(226, 192, 99)
  107. #define SAMP_GAME_TEXT_YH MK_RGB(255, 255, 148)
  108. #define SAMP_GAME_TEXT_YHH MK_RGB(255, 255, 222) // Also in green
  109. #define SAMP_GAME_TEXT_P MK_RGB(168, 110, 252)
  110. #define SAMP_GAME_TEXT_PH MK_RGB(252, 165, 255)
  111. #define SAMP_GAME_TEXT_PHH MK_RGB(255, 247, 255)
  112. #define SAMP_GAME_TEXT_W MK_RGB(255, 255, 255)
  113. #define SAMP_GAME_TEXT_L MK_RGB( 0, 0, 0)
  114. // Second spellings.
  115. #define SAMP_GAMETEXT MK_RGB(144, 98, 16)
  116. #define SAMP_GAMETEXT_ MK_RGB(144, 98, 16)
  117. #define SAMP_GAMETEXT_H MK_RGB(216, 147, 24)
  118. #define SAMP_GAMETEXT_HH MK_RGB(255, 255, 54)
  119. #define SAMP_GAMETEXT_X MK_RGB(144, 98, 16)
  120. #define SAMP_GAMETEXT_XH MK_RGB(216, 147, 24)
  121. #define SAMP_GAMETEXT_XHH MK_RGB(255, 255, 54)
  122. #define SAMP_GAMETEXT_R MK_RGB(180, 25, 29)
  123. #define SAMP_GAMETEXT_RH MK_RGB(255, 37, 43)
  124. #define SAMP_GAMETEXT_RHH MK_RGB(255, 55, 64)
  125. #define SAMP_GAMETEXT_RHHH MK_RGB(255, 82, 96)
  126. #define SAMP_GAMETEXT_RHHHH MK_RGB(255, 123, 144)
  127. #define SAMP_GAMETEXT_RHHHHH MK_RGB(255, 184, 216)
  128. #define SAMP_GAMETEXT_G MK_RGB( 54, 104, 44)
  129. #define SAMP_GAMETEXT_GH MK_RGB( 81, 156, 66)
  130. #define SAMP_GAMETEXT_GHH MK_RGB(121, 234, 99)
  131. #define SAMP_GAMETEXT_GHHH MK_RGB(181, 255, 148)
  132. #define SAMP_GAMETEXT_GHHHH MK_RGB(255, 255, 222)
  133. #define SAMP_GAMETEXT_B MK_RGB( 50, 60, 127)
  134. #define SAMP_GAMETEXT_BH MK_RGB( 75, 90, 190)
  135. #define SAMP_GAMETEXT_BHH MK_RGB(112, 135, 255)
  136. #define SAMP_GAMETEXT_BHHH MK_RGB(168, 202, 255)
  137. #define SAMP_GAMETEXT_Y MK_RGB(226, 192, 99)
  138. #define SAMP_GAMETEXT_YH MK_RGB(255, 255, 148)
  139. #define SAMP_GAMETEXT_YHH MK_RGB(255, 255, 222) // Also in green
  140. #define SAMP_GAMETEXT_P MK_RGB(168, 110, 252)
  141. #define SAMP_GAMETEXT_PH MK_RGB(252, 165, 255)
  142. #define SAMP_GAMETEXT_PHH MK_RGB(255, 247, 255)
  143. #define SAMP_GAMETEXT_W MK_RGB(255, 255, 255)
  144. #define SAMP_GAMETEXT_L MK_RGB( 0, 0, 0)
  145. // Main code start. Apparently there is a copy of the hash code in y_td.
  146. #define COLOUR_NAME_HASH(%0) YHash((%0), false, hash_bernstein, (YSI_g_sCheckSpaces) ? (-1) : (strfind((%0), " ")))
  147. #define COLOR_NAME_HASH COLOUR_NAME_HASH
  148. //#include "internal\y_colourparse"
  149. #if _YSI_USE_X11
  150. #include "internal\y_x11def"
  151. #endif
  152. #if !defined X11_ALLOW_SPACES
  153. #define X11_ALLOW_SPACES (false)
  154. #endif
  155. static stock
  156. YSI_g_sColours[MAX_TEXT_COLOURS][2],
  157. bool:YSI_g_sCheckSpaces = X11_ALLOW_SPACES;
  158. remotefunc void:_SetColoursCanHaveSpaces(bool:set)
  159. {
  160. YSI_g_sCheckSpaces = set;
  161. }
  162. #define SetColorsCanHaveSpaces SetColoursCanHaveSpaces
  163. stock SetColoursCanHaveSpaces(bool:set)
  164. {
  165. // Set this true/false in ALL scripts, since the text system is distributed.
  166. broadcastfunc _SetColoursCanHaveSpaces(set);
  167. //return 1;
  168. }
  169. #define SetColor SetColour
  170. stock SetColour(const name[], color)
  171. {
  172. P:3("SetColour called: \"%s\", %i", name, color);
  173. #if _YSI_USE_X11
  174. //if (name[0] | 0x20 == 'x' && name[1] == '1' && name[2] == '1')
  175. if (!strcmp(name, "X11", true, 3))
  176. {
  177. if (name[3] == ' ' || name[3] == '_')
  178. {
  179. SetColourHash(COLOUR_NAME_HASH(name[4]), color);
  180. }
  181. else
  182. {
  183. SetColourHash(COLOUR_NAME_HASH(name[3]), color);
  184. }
  185. }
  186. else
  187. #endif
  188. {
  189. SetColourHash(COLOUR_NAME_HASH(name), color);
  190. }
  191. }
  192. stock SetColourHash(hash, color)
  193. {
  194. broadcastfunc _SetColourHash(hash, color);
  195. }
  196. #define SetColorHash SetColourHash
  197. remotefunc void:_SetColourHash(hash, color)
  198. {
  199. P:3("SetColourHash called: %i, %i", hash, color);
  200. color &= 0xFFFFFF00;
  201. #if _YSI_USE_X11
  202. new
  203. id = GetColourHash(hash);
  204. if (id != 0)
  205. {
  206. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  207. {
  208. new
  209. iColor = YSI_g_sColours[i][0];
  210. if (iColor == hash)
  211. {
  212. YSI_g_sColours[i][1] = color;
  213. return; // i;
  214. }
  215. else if (iColor == 0)
  216. {
  217. // Tried to rename an X11 colour.
  218. return; // -1;
  219. }
  220. }
  221. }
  222. #endif
  223. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  224. {
  225. new
  226. iColor = YSI_g_sColours[i][0];
  227. if (iColor == hash || iColor == 0)
  228. {
  229. YSI_g_sColours[i][0] = hash;
  230. YSI_g_sColours[i][1] = color;
  231. return; // i;
  232. }
  233. }
  234. return; // -1;
  235. }
  236. // This now uses REVERSE bernstein hash for switch compatibility.
  237. #define GetColor GetColour
  238. stock GetColour(const name[], alpha = 0xAA)
  239. {
  240. P:3("GetColour called: \"%s\", %i", name, alpha);
  241. //if (name[0] | 0x20 == 'x' && name[1] == '1' && name[2] == '2')
  242. #if _YSI_USE_X11
  243. if (!strcmp(name, "X11", true, 3))
  244. {
  245. if ((YSI_g_sCheckSpaces && name[3] == ' ') || name[3] == '_')
  246. {
  247. return GetColourHash(COLOUR_NAME_HASH(name[4]), alpha);
  248. }
  249. else
  250. {
  251. return GetColourHash(COLOUR_NAME_HASH(name[3]), alpha);
  252. }
  253. }
  254. else
  255. #endif
  256. {
  257. return GetColourHash(COLOUR_NAME_HASH(name), alpha);
  258. }
  259. }
  260. #define GetColorStream GetColourStream
  261. stock GetColourStream(const str[], &idx, alpha = 0xAA)
  262. {
  263. P:3("GetColourStream called: \"%s\", %i, %i", str, idx, alpha);
  264. // This doesn't work because the hash is backwards, not forwards. You can't
  265. // do a reverse hash incrementally. Actually you can, but not well.
  266. new
  267. pos = idx,
  268. ret = -1;
  269. #if _YSI_USE_X11
  270. //new
  271. // bool:checkSpace = YSI_g_sCheckSpaces;
  272. if (!strcmp(str[pos], "X11", true, 3))
  273. {
  274. pos += 3;
  275. if ((YSI_g_sCheckSpaces && str[pos] == ' ') || str[pos] == '_')
  276. {
  277. ++pos;
  278. }
  279. }
  280. ret = Colours_DoHashParse(str[pos], pos, YSI_g_sCheckSpaces);
  281. if (ret != -1)
  282. {
  283. idx = pos;
  284. return ret | alpha;
  285. }
  286. #endif
  287. // Do the incremental hash. This *should* be "idx = pos + 16;", but it
  288. // isn't as you shouldn't prefix custom colours with "X11" as they're not.
  289. new
  290. test = min(strlen(str[pos]), 16);
  291. while (test)
  292. {
  293. // This is a greedy test, and restricts custom text to 16 characters.
  294. ret = YHash(str[pos], false, hash_bernstein, test);
  295. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  296. {
  297. new
  298. iColor = YSI_g_sColours[i][0];
  299. if (iColor == ret)
  300. {
  301. idx = pos + test;
  302. return alpha | YSI_g_sColours[i][1];
  303. }
  304. else if (iColor == 0)
  305. {
  306. break;
  307. }
  308. }
  309. --test;
  310. }
  311. return 0;
  312. }
  313. #define GetColorHash GetColourHash
  314. stock GetColourHash(hash, alpha = 0xAA)
  315. {
  316. P:3("GetColourHash called: %i, %i", hash, alpha);
  317. alpha &= 0xFF;
  318. // Do the REVERSE hash from YHash
  319. #if _YSI_USE_X11
  320. #include "internal\y_x11switch"
  321. #endif
  322. // Do the default code here.
  323. for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  324. {
  325. new
  326. iColor = YSI_g_sColours[i][0];
  327. if (iColor == hash)
  328. {
  329. return alpha | YSI_g_sColours[i][1];
  330. }
  331. else if (iColor == 0)
  332. {
  333. return 0;
  334. }
  335. }
  336. return 0;
  337. }
  338. #if _YSI_USE_X11
  339. static stock Colours_DoHashParse(const str[], &idx, bool:checkSpace)
  340. {
  341. // You can't use custom colours with the "#COLOR" format, but you can
  342. // with the "{COLOR}" format, this is to save on memory. It is in
  343. // theory possible to get it working with the hash format using an
  344. // incremental hash and compare. NOW SUPPORTED!
  345. #include "internal\y_x11parse"
  346. #pragma tabsize 4
  347. return -1;
  348. }
  349. #endif