1
0

y_colours.inc 12 KB

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