Advertisement
Y_Less

y_colours 2

Jan 4th, 2011
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 21.33 KB | None | 0 0
  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.    
  14.     The contents of this file are subject to the Mozilla Public License Version
  15.     1.1 (the "License"); you may not use this file except in compliance with
  16.     the License. You may obtain a copy of the License at
  17.     http://www.mozilla.org/MPL/
  18.    
  19.     Software distributed under the License is distributed on an "AS IS" basis,
  20.     WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  21.     for the specific language governing rights and limitations under the
  22.     License.
  23.    
  24.     The Original Code is the SA:MP script information include.
  25.    
  26.     The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  27.     Portions created by the Initial Developer are Copyright (C) 2008
  28.     the Initial Developer. All Rights Reserved.
  29.    
  30.     Contributors:
  31.         ZeeX, koolk
  32.    
  33.     Thanks:
  34.         Peter, Cam - Support.
  35.         ZeeX - Very productive conversations.
  36.         g_aSlice - More conversations.
  37.         RyDeR` - Idea for hash colours.
  38.         [FeK]DraKiNs - Idea for end colour.
  39.         koolk - IsPlayerinAreaEx code.
  40.         TheAlpha - Danish translation.
  41.         breadfish - German translation.
  42.         Fireburn - Dutch translation.
  43.         yom - French translation.
  44.         50p - Polish translation.
  45.         Zamaroht - Spanish translation.
  46.         Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  47.             for me to strive to better.
  48.         Pixels^ - Running XScripters where the idea was born.
  49.         Matite - Pestering me to release it and using it.
  50.    
  51.     Very special thanks to:
  52.         Thiadmer - PAWN.
  53.         Kye/Kalcor - SA:MP.
  54.         SA:MP Team past, present and future - SA:MP.
  55. Version:
  56.     1.0
  57. Changelog:
  58.     29/11/10:
  59.         Added the ability to use "X11" prefixes in strings.
  60.         Added colourspace resolution for converting {FF0000} to ~r~.
  61.     25/11/10:
  62.         First version.
  63. -*----------------------------------------------------------------------------*/
  64.  
  65. #include <YSI\internal\y_version>
  66.  
  67. #include <YSI\y_stringhash>
  68. #include <YSI\y_debug>
  69.  
  70. #if !defined MAX_TEXT_COLOURS
  71.     #define MAX_TEXT_COLOURS            (32)
  72. #endif
  73.  
  74. #if !defined NO_X11_COLOURS && !defined NO_X11_COLORS
  75.     #define _YSI_USE_X11 true
  76. #else
  77.     #define _YSI_USE_X11 false
  78. #endif
  79.  
  80. #define MK_RGB(%0,%1,%2)                ((%0)<<24|(%1)<<16|(%2)<<8)
  81.  
  82. #define SAMP_GAME_TEXT                  MK_RGB(144,  98,  16)
  83. #define SAMP_GAME_TEXT_                 MK_RGB(144,  98,  16)
  84. #define SAMP_GAME_TEXT_H                MK_RGB(216, 147,  24)
  85. #define SAMP_GAME_TEXT_HH               MK_RGB(255, 255,  54)
  86.  
  87. #define SAMP_GAME_TEXT_R                MK_RGB(180,  25,  29)
  88. #define SAMP_GAME_TEXT_RH               MK_RGB(255,  37,  43)
  89. #define SAMP_GAME_TEXT_RHH              MK_RGB(255,  55,  64)
  90. #define SAMP_GAME_TEXT_RHHH             MK_RGB(255,  82,  96)
  91. #define SAMP_GAME_TEXT_RHHHH            MK_RGB(255, 123, 144)
  92. #define SAMP_GAME_TEXT_RHHHHH           MK_RGB(255, 184, 216)
  93.  
  94. #define SAMP_GAME_TEXT_G                MK_RGB( 54, 104,  44)
  95. #define SAMP_GAME_TEXT_GH               MK_RGB( 81, 156,  66)
  96. #define SAMP_GAME_TEXT_GHH              MK_RGB(121, 234,  99)
  97. #define SAMP_GAME_TEXT_GHHH             MK_RGB(181, 255, 148)
  98. #define SAMP_GAME_TEXT_GHHHH            MK_RGB(255, 255, 222)
  99.  
  100. #define SAMP_GAME_TEXT_B                MK_RGB( 50,  60, 127)
  101. #define SAMP_GAME_TEXT_BH               MK_RGB( 75,  90, 190)
  102. #define SAMP_GAME_TEXT_BHH              MK_RGB(112, 135, 255)
  103. #define SAMP_GAME_TEXT_BHHH             MK_RGB(168, 202, 255)
  104.  
  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.  
  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.  
  113. #define SAMP_GAME_TEXT_W                MK_RGB(255, 255, 255)
  114.  
  115. #define SAMP_GAME_TEXT_L                MK_RGB(  0,   0,   0)
  116.  
  117. #define COLOUR_NAME_HASH(%0)            YHash((%0),false,hash_bernstein)
  118. #define COLOR_NAME_HASH                 COLOUR_NAME_HASH
  119.  
  120. //#include <YSI\internal\y_colourparse>
  121.  
  122. #if _YSI_USE_X11
  123.     #include <YSI\internal\y_x11def>
  124. #endif
  125.  
  126. enum e_COLOUR_TYPE
  127. {
  128.     colour_samp,
  129.     colour_game_text,
  130.     colour_text_draw,
  131.     colour_text_draw_ex,
  132.     colour_scm,
  133.     colour_scm_pre_3c
  134. }
  135.  
  136. #define e_COLOR_TYPE                    e_COLOUR_TYPE
  137.  
  138. #define color_samp                      colour_samp
  139. #define color_game_text                 colour_game_text
  140. #define color_text_draw                 colour_text_draw
  141. #define color_text_draw_accurate        colour_text_draw_accurate
  142. #define colour_scm                      colour_scm
  143. #define colour_scm_pre_3c               colour_scm_pre_3c
  144.  
  145. #if _YSI_USE_X11
  146.     static stock Colours_DoHashParse(const str[], &idx)
  147.     {
  148.         #include <YSI\internal\y_x11parse>
  149.         #pragma tabsize 4
  150.         return -1;
  151.     }
  152. #endif
  153.  
  154. // This function takes a string input and returns the colour in any format
  155. // following it.
  156. stock Colours_Resolve(const input[], &idx, &bool:fade, &steps, pt = false)
  157. {
  158.     new
  159.         ch = input[idx],
  160.         start = idx,
  161.         delim;
  162.     steps = 1;
  163.     switch (ch)
  164.     {
  165.         case '#':
  166.         {
  167.             // Hash colours introduced by RyDeR`.
  168.             if (fade)
  169.             {
  170.                 fade = false;
  171.             }
  172.             ++start;
  173.             if (input[start] == '\0')
  174.             {
  175.                 ++idx;
  176.                 return -1;
  177.             }
  178.             // Try and find the named colour.
  179.             #if _YSI_USE_X11
  180.                 if (!strcmp(input[start], "X11", true, 3))
  181.                 {
  182.                     start += 3;
  183.                     if (input[start] == ' ' || input[start] == '_')
  184.                     {
  185.                         ++start;
  186.                     }
  187.                 }
  188.                 delim = Colours_DoHashParse(input[start], start);
  189.                 if (delim != -1)
  190.                 {
  191.                     idx = start;
  192.                     return delim;
  193.                 }
  194.                 //return Colours_DoHashParse(input, idx);
  195.             #endif
  196.             ++idx;
  197.             // Need to accept 3 and 6 character hex codes.
  198.             delim = 0;
  199.             //idx = start;
  200.             while ((ch = input[start++] | 0x20))
  201.             {
  202.                 if ('0' <= ch <= '9')
  203.                 {
  204.                     delim = (delim << 4) | (ch - '0');
  205.                 }
  206.                 else if ('a' <= ch <= 'f')
  207.                 {
  208.                     delim = (delim << 4) | (ch - 'a' + 10);
  209.                 }
  210.                 else
  211.                 {
  212.                     if (start - idx == 6)
  213.                     {
  214.                         idx += 6;
  215.                         return delim;
  216.                     }
  217.                     else if (start - idx >= 3)
  218.                     {
  219.                         // Convert to 3 digit hex.
  220.                         // First strip off excess.
  221.                         delim >>>= 4 * (start - idx - 4);
  222.                         idx += 3;
  223.                         // Then dupilcate each value.
  224.                         return ((delim & 0x0F00) * 0x1100) | ((delim & 0x00F0) * 0x0110) | ((delim & 0x000F) * 0x0011);
  225.                     }
  226.                     --idx;
  227.                     return -1;
  228.                 }
  229.                 if (start - idx == 6)
  230.                 {
  231.                     idx += 6;
  232.                     return delim;
  233.                 }
  234.             }
  235.             // Should never be reached.
  236.             return -1;
  237.         }
  238.         case '~': // Game Text style information
  239.         {
  240.             if (fade)
  241.             {
  242.                 // These never start and always close fades.
  243.                 fade = false;
  244.             }
  245.             return Colours_GTToSAMP(input, idx);
  246.         }
  247.         case '(': // Bracket
  248.         {
  249.             if (pt)
  250.             {
  251.                 delim = ')';
  252.             }
  253.             else
  254.             {
  255.                 return -1;
  256.             }
  257.         }
  258.         case '{': // Brace
  259.         {
  260.             delim = '}';
  261.         }
  262.         default:
  263.         {
  264.             return -1;
  265.         }
  266.     }
  267.     switch ((ch = input[++start]))
  268.     {
  269.         case '>':
  270.         {
  271.             // Start of a fade.
  272.             if (!fade)
  273.             {
  274.                 fade = true;
  275.             }
  276.             ch = input[++start];
  277.             if (ch == '[')
  278.             {
  279.                 steps = 0;
  280.                 while ((ch = input[++start]))
  281.                 {
  282.                     // Parse the number of steps to use.
  283.                     if (ch == ']')
  284.                     {
  285.                         ch = input[++start];
  286.                         break;
  287.                     }
  288.                     else if ('0' <= ch <= '9')
  289.                     {
  290.                         steps = (steps * 10) + (ch - '0');
  291.                     }
  292.                     else
  293.                     {
  294.                         return -1;
  295.                     }
  296.                 }
  297.             }
  298.         }
  299.         case '<':
  300.         {
  301.             // End of a fade.
  302.             if (fade)
  303.             {
  304.                 fade = false;
  305.             }
  306.             ch = input[++start];
  307.         }
  308.     }
  309.     // Close colours introduced by [FeK]DraKiNs.
  310.     if (ch == '/')
  311.     {
  312.         // Close colour.
  313.         while ((ch = input[++start]))
  314.         {
  315.             if (ch == delim)
  316.             {
  317.                 break;
  318.             }
  319.         }
  320.         // End of the current colour.
  321.         idx = start + 1;
  322.         return 0xF0000000;
  323.     }
  324.     // Finally we can get the real colour.
  325.     new
  326.         tot = 0,
  327.         bool:num = true,
  328.         cur = start;
  329.     //idx = start;
  330.     while (ch)
  331.     {
  332.         if (ch == delim)
  333.         {
  334.             break;
  335.         }
  336.         new
  337.             ch2 = ch | 0x20;
  338.         if (num)
  339.         {
  340.             // Check if it's still a valid HEX digit and increment.
  341.             if ('0' <= ch <= '9')
  342.             {
  343.                 tot = (tot << 4) | (ch - '0');
  344.             }
  345.             else if ('a' <= ch2 <= 'f')
  346.             {
  347.                 tot = (tot << 4) | (ch2 - 'a' + 10);
  348.             }
  349.             else if (('g' <= ch2 <= 'z') || ch == '_' || ch == ' ')
  350.             {
  351.                 // No, but still a valid identifier.
  352.                 num = false;
  353.             }
  354.             else
  355.             {
  356.                 return -1;
  357.             }
  358.         }
  359.         else if (!(('0' <= ch <= '9') || ('a' <= ch2 <= 'z') || ch == '_' || ch == ' '))
  360.         {
  361.             // Not a valid identifier character.
  362.             idx = start;
  363.             return -1;
  364.         }
  365.         ch = input[++cur];
  366.     }
  367.     //while ((ch = input[++idx]));
  368.     if (ch == '\0')
  369.     {
  370.         // Not a valid end.
  371.         return -1;
  372.     }
  373.     ++cur;
  374.     idx = cur;
  375.     if (cur == start + 1)
  376.     {
  377.         return -1;
  378.     }
  379.     if (num)
  380.     {
  381.         // The input was a number.
  382.         return tot & 0x00FFFFFF;
  383.     }
  384.     // The input was a string.
  385.     //if (input[start] | 0x20 == 'x' && input[start + 1] == '1' && input[start + 2] == '1')
  386.     #if _YSI_USE_X11
  387.         if (!strcmp(input[start], "X11", true, 3))
  388.         {
  389.             if (input[start + 3] == ' ' || input[start + 3] == '_')
  390.             {
  391.                 delim = GetColourHash(YHash(input[start + 4], false, hash_bernstein, cur - start - 5), 1);
  392.             }
  393.             else
  394.             {
  395.                 delim = GetColourHash(YHash(input[start + 3], false, hash_bernstein, cur - start - 4), 1);
  396.             }
  397.         }
  398.         else
  399.     #endif
  400.     {
  401.         delim = GetColourHash(YHash(input[start], false, hash_bernstein, cur - start - 1), 1);
  402.     }
  403.     #pragma tabsize 4
  404.     if (delim)
  405.     {
  406.         return delim >>> 8;
  407.     }
  408.     return -1;
  409.     /*else
  410.     {
  411.         return GetColourHash(YHash(input[start], false, hash_bernstein, cur - start - 1), 0) >>> 8;
  412.     }*/
  413. }
  414.  
  415. stock Colours_AddSAMPChange(output[], &idx, rem, cur, nu)
  416. {
  417.     if (cur == nu)
  418.     {
  419.         return 2;
  420.     }
  421.     #if defined Y_COLOUR_APPROXIMATE || defined Y_COLOR_APPROXIMATE
  422.         if (-0x5 <= ((nu & 0x0FF) - (cur & 0xFF)) <= 0x5)
  423.         {
  424.             if (-0x500 <= ((nu & 0xFF00) - (cur & 0xFF00)) <= 0x500)
  425.             {
  426.                 if (-0x50000 <= ((nu & 0xFF0000) - (cur & 0xFF0000)) <= 0x50000)
  427.                 {
  428.                     // Haven't changed the colour.
  429.                     return 1;
  430.                 }
  431.             }
  432.         }
  433.     #endif
  434.     // Need to add an update.
  435.     if (rem <= 8)
  436.     {
  437.         // Not enough space.
  438.         return -1;
  439.     }
  440.     format(output[idx], rem, "{%06x}", nu);
  441.     idx += 8;
  442.     // Added to output.
  443.     return 0;
  444. }
  445.  
  446. stock bool:Colours_AddGTChange(output[], &idx, rem, cur, nu)
  447. {
  448.     // Add the GT text to change from the current colour to the new colour.
  449.     new
  450.         t = nu & 0xFF,
  451.         o = cur & 0xFF;
  452.     // Get the base colour.
  453.     nu >>>= 8;
  454.     cur >>>= 8;
  455.     if (t == 'x' && o == 'y')
  456.     {
  457.         // Original.  Have to use yellow instead (though red is closer to the
  458.         // plain original colour).  I tried to find a way to replicate the basic
  459.         // orange but there is no way...
  460.         t == 'y';
  461.         nu = 0;
  462.     }
  463.     if (t == o)
  464.     {
  465.         // Stayed the same or will now stay the same.
  466.         if (nu == cur)
  467.         {
  468.             return true;
  469.         }
  470.         if (nu > cur)
  471.         {
  472.             // Same colour, just lighter.
  473.             do
  474.             {
  475.                 if (rem <= 3)
  476.                 {
  477.                     return false;
  478.                 }
  479.                 // Add a fader.
  480.                 //output[idx] = '~';
  481.                 //output[idx + 1] = 'h';
  482.                 //output[idx + 2] = '~';
  483.                 strcat(output, "~h~", rem);
  484.                 rem -= 3;
  485.                 idx += 3;
  486.                 --nu;
  487.             }
  488.             while (nu > cur);
  489.             return true;
  490.         }
  491.         // Same colour but darker - need to reset.
  492.     }
  493.     if (rem <= 3)
  494.     {
  495.         return false;
  496.     }
  497.     // Add the base colour.
  498.     output[idx] = '~';
  499.     output[idx + 1] = t;
  500.     output[idx + 2] = '~';
  501.     rem -= 3;
  502.     idx += 3;
  503.     while (nu)
  504.     {
  505.         if (rem <= 3)
  506.         {
  507.             return false;
  508.         }
  509.         // Add a fader.
  510.         //output[idx] = '~';
  511.         //output[idx + 1] = 'h';
  512.         //output[idx + 2] = '~';
  513.         strcat(output, "~h~", rem);
  514.         rem -= 3;
  515.         idx += 3;
  516.         --nu;
  517.     }
  518.     return true;
  519. }
  520.  
  521. #define _COLOUR_GT_SPACE_DEF_0(%0)      SAMP_GAME_TEXT_%0
  522. #define _COLOUR_GT_SPACE_DEF_1(%0)      SAMP_GAME_TEXT_%0H
  523. #define _COLOUR_GT_SPACE_DEF_2(%0)      SAMP_GAME_TEXT_%0HH
  524. #define _COLOUR_GT_SPACE_DEF_3(%0)      SAMP_GAME_TEXT_%0HHH
  525. #define _COLOUR_GT_SPACE_DEF_4(%0)      SAMP_GAME_TEXT_%0HHHH
  526. #define _COLOUR_GT_SPACE_DEF_5(%0)      SAMP_GAME_TEXT_%0HHHHH
  527.  
  528. #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}
  529. #define _COLOUR_GT_SPACE_N(%1)          {'x'|(%1<<8),_COLOUR_GT_SPACE_DEF_%1()>>>24,_COLOUR_GT_SPACE_DEF_%1()>>16&0xFF,_COLOUR_GT_SPACE_DEF_%1()>>8&0xFF}
  530.  
  531. #define COLOUR_FLOAT_INFINITY           (Float:0x7F800000)
  532.  
  533. static stock Colours_SAMPToGT(colour)
  534. {
  535.     // Find the closest matching game text colour to the given SA:MP colour.
  536.     static const
  537.         sc_aColours[][4] =
  538.         {
  539.             // These can't be used as you can't set text to them.  You can
  540.             // however use them at the start by not changing.
  541.             _COLOUR_GT_SPACE_N(0), _COLOUR_GT_SPACE_N(1),
  542.             _COLOUR_GT_SPACE_N(2),
  543.            
  544.             _COLOUR_GT_SPACE(R,0), _COLOUR_GT_SPACE(R,1),
  545.             _COLOUR_GT_SPACE(R,2), _COLOUR_GT_SPACE(R,3),
  546.             _COLOUR_GT_SPACE(R,4), _COLOUR_GT_SPACE(R,5),
  547.            
  548.             _COLOUR_GT_SPACE(G,0), _COLOUR_GT_SPACE(G,1),
  549.             _COLOUR_GT_SPACE(G,2), _COLOUR_GT_SPACE(G,3),
  550.             //_COLOUR_GT_SPACE(G,4), // Don't want this twice (see Y2).
  551.            
  552.             _COLOUR_GT_SPACE(B,0), _COLOUR_GT_SPACE(B,1),
  553.             _COLOUR_GT_SPACE(B,2), _COLOUR_GT_SPACE(B,3),
  554.            
  555.             _COLOUR_GT_SPACE(Y,0), _COLOUR_GT_SPACE(Y,1),
  556.             _COLOUR_GT_SPACE(Y,2),
  557.            
  558.             _COLOUR_GT_SPACE(P,0), _COLOUR_GT_SPACE(P,1),
  559.             _COLOUR_GT_SPACE(P,2),
  560.            
  561.             _COLOUR_GT_SPACE(W,0),
  562.            
  563.             _COLOUR_GT_SPACE(L,0)
  564.         };
  565.     new
  566.         Float:dist = COLOUR_FLOAT_INFINITY,
  567.         found = '\0',
  568.         r = colour >> 16,
  569.         g = colour >> 8 & 0xFF,
  570.         b = colour & 0xFF,
  571.         Float:tr, Float:tg, Float:tb, Float:cur;
  572.     for (new i = 0; i < sizeof (sc_aColours); ++i)
  573.     {
  574.         tr = r - sc_aColours[i][1];
  575.         tg = g - sc_aColours[i][2];
  576.         tb = b - sc_aColours[i][3];
  577.         cur = (tr * tr) + (tg * tg) + (tb * tb);
  578.         if (cur < dist)
  579.         {
  580.             // This may sometimes give odd results in fades for draw results.
  581.             dist = cur;
  582.             found = sc_aColours[i][0];
  583.         }
  584.     }
  585.     return found;
  586. }
  587.  
  588. static stock Colours_GTToSAMP(const str[], &idx)
  589. {
  590.     new
  591.         cur = idx;
  592.     if (str[cur] == '~')
  593.     {
  594.         // Check the colour is correctly ended.
  595.         new
  596.             t = str[++cur];
  597.         if (str[++cur] != '~')
  598.         {
  599.             return -1;
  600.         }
  601.         ++cur;
  602.         // Get the number of ~h~s after the colour.
  603.         new
  604.             c = 0;
  605.         while (str[cur] == '~')
  606.         {
  607.             if (str[cur + 1] | 0x20 == 'h' && str[cur + 2] == '~')
  608.             {
  609.                 ++c;
  610.                 cur += 3;
  611.             }
  612.         }
  613.         idx = cur;
  614.         // Return the correct colour for the given string.
  615.         switch (t)
  616.         {
  617.             case 'b':
  618.             {
  619.                 switch (c)
  620.                 {
  621.                     case 0:
  622.                         return SAMP_GAME_TEXT_B >>> 8;
  623.                     case 1:
  624.                         return SAMP_GAME_TEXT_BH >>> 8;
  625.                     case 2:
  626.                         return SAMP_GAME_TEXT_BHH >>> 8;
  627.                     case 3:
  628.                         return SAMP_GAME_TEXT_BHHH >>> 8;
  629.                     default:
  630.                         return SAMP_GAME_TEXT_W >>> 8;
  631.                 }
  632.             }
  633.             case 'g':
  634.             {
  635.                 switch (c)
  636.                 {
  637.                     case 0:
  638.                         return SAMP_GAME_TEXT_G >>> 8;
  639.                     case 1:
  640.                         return SAMP_GAME_TEXT_GH >>> 8;
  641.                     case 2:
  642.                         return SAMP_GAME_TEXT_GHH >>> 8;
  643.                     case 3:
  644.                         return SAMP_GAME_TEXT_GHHH >>> 8;
  645.                     case 4:
  646.                         return SAMP_GAME_TEXT_GHHHH >>> 8;
  647.                     default:
  648.                         return SAMP_GAME_TEXT_W >>> 8;
  649.                 }
  650.             }
  651.             case 'h':
  652.             {
  653.                 switch (c)
  654.                 {
  655.                     case 0:
  656.                         return SAMP_GAME_TEXT_H >>> 8;
  657.                     case 1:
  658.                         return SAMP_GAME_TEXT_HH >>> 8;
  659.                     default:
  660.                         return SAMP_GAME_TEXT_W >>> 8;
  661.                 }
  662.             }
  663.             case 'l':
  664.             {
  665.                 return SAMP_GAME_TEXT_L >>> 8;
  666.             }
  667.             case 'p':
  668.             {
  669.                 switch (c)
  670.                 {
  671.                     case 0:
  672.                         return SAMP_GAME_TEXT_P >>> 8;
  673.                     case 1:
  674.                         return SAMP_GAME_TEXT_PH >>> 8;
  675.                     case 2:
  676.                         return SAMP_GAME_TEXT_PHH >>> 8;
  677.                     default:
  678.                         return SAMP_GAME_TEXT_W >>> 8;
  679.                 }
  680.             }
  681.             case 'r':
  682.             {
  683.                 switch (c)
  684.                 {
  685.                     case 0:
  686.                         return SAMP_GAME_TEXT_R >>> 8;
  687.                     case 1:
  688.                         return SAMP_GAME_TEXT_RH >>> 8;
  689.                     case 2:
  690.                         return SAMP_GAME_TEXT_RHH >>> 8;
  691.                     case 3:
  692.                         return SAMP_GAME_TEXT_RHHH >>> 8;
  693.                     case 4:
  694.                         return SAMP_GAME_TEXT_RHHHH >>> 8;
  695.                     case 5:
  696.                         return SAMP_GAME_TEXT_RHHHHH >>> 8;
  697.                     default:
  698.                         return SAMP_GAME_TEXT_W >>> 8;
  699.                 }
  700.             }
  701.             case 'w':
  702.             {
  703.                 return SAMP_GAME_TEXT_W >>> 8;
  704.             }
  705.             case 'y':
  706.             {
  707.                 switch (c)
  708.                 {
  709.                     case 0:
  710.                         return SAMP_GAME_TEXT_Y >>> 8;
  711.                     case 1:
  712.                         return SAMP_GAME_TEXT_YH >>> 8;
  713.                     case 2:
  714.                         return SAMP_GAME_TEXT_YHH >>> 8;
  715.                     default:
  716.                         return SAMP_GAME_TEXT_W >>> 8;
  717.                 }
  718.             }
  719.             default:
  720.             {
  721.                 // Return the fact there is a different GT sequence but keep a
  722.                 // record of the number of hs which came after it to avoid
  723.                 // excess processing.
  724.                 //idx -= (c + 1) * 3;
  725.                 return 0x0F000000 | t | (c << 8);
  726.             }
  727.         }
  728.     }
  729.     return -1;
  730. }
  731.  
  732. /*static stock Colours_DoTextSAMP(const input[], output[], offset, size)
  733. {
  734.     if (offset == 0)
  735.     {
  736.         // Get the next possible start point.
  737.         new
  738.             lastspace = -1,
  739.             startc = strfind(input, "("),
  740.             starts = strfind(input, "{"),
  741.             startg = strfind(input, "~"),
  742.             idx = 0;
  743.         if (startc == -1) startc = cellmax;
  744.         if (starts == -1) starts = cellmax;
  745.         if (startg == -1) startg = cellmax;
  746.         while (idx < size)
  747.         {
  748.             if (startc < startg)
  749.             {
  750.                 if (startc < starts)
  751.                 {
  752.                     // startc lowest.  This is client style brackets.
  753.                     // We need to check for valid format.
  754.                     new
  755.                         curc,
  756.                         idx = startc + 1;
  757.                     if (input[idx] == '>')
  758.                     {
  759.                         // The start of a fade.
  760.                         ++idx;
  761.                         curc = Colour_Resolve(input, ')', idx);
  762.                     }
  763.                     else
  764.                     {
  765.                         curc = Colour_Resolve(input, ')', idx);
  766.                     }
  767.                     continue;
  768.                 }
  769.             }
  770.             else if (starts < startg)
  771.             {
  772.                 // starts lowest.  This is SA:MP style braces.
  773.                 continue;
  774.             }
  775.             // startg lowest.  This is game text style tildas.
  776.             switch (input[startg + 1])
  777.             {
  778.                 case 'b':
  779.                 case 'g':
  780.                 case 'h':
  781.                 case 'l':
  782.                 case 'r':
  783.                 case 'p':
  784.                 case 'w':
  785.                 case 'y':
  786.                     // Colours.
  787.                 case 'n':
  788.                     // Need to handle this.
  789.                 case 'a':
  790.                 case 'd':
  791.                 case 'k':
  792.                 case 'u':
  793.                 case '<':
  794.                 case '>':
  795.                 case '1':
  796.                 case ']':
  797.                     // Symbols.
  798.                 default:
  799.                     return -1;
  800.             }
  801.         }
  802.     }
  803.     //else
  804.     //{
  805.     //}
  806. }
  807.  
  808. #define Colors_DoText                   Colours_DoText
  809. stock Colours_DoText(const input[], output[], offset = -1, e_COLOUR_TYPE:type = colour_samp, size = sizeof (output))
  810. {
  811.     switch ()
  812.     {
  813.         case colour_samp:
  814.             return Colours_DoTextSAMP(input, output, offset, size);
  815.         case colour_game_text:
  816.             return Colours_DoTextGT(input, output, offset, size);
  817.         case colour_text_draw:
  818.             return Colours_DoTextTD(input, output, offset, size);
  819.         //case colour_text_draw_ex:
  820.         //  return Colours_DoTextTDX();
  821.         default:
  822.             return -1;
  823.     }
  824. }*/
  825.  
  826. #if _YSI_USE_X11
  827.     #include <YSI\internal\y_x11def>
  828. #endif
  829.  
  830. static stock
  831.     YSI_g_sColours[MAX_TEXT_COLOURS][2];
  832.  
  833. #define SetColor                        SetColour
  834. stock SetColour(const name[], color)
  835. {
  836.     #if _YSI_USE_X11
  837.         //if (name[0] | 0x20 == 'x' && name[1] == '1' && name[2] == '1')
  838.         if (!strcmp(name, "X11", true, 3))
  839.         {
  840.             if (name[3] == ' ' || name[3] == '_')
  841.             {
  842.                 return SetColourHash(COLOUR_NAME_HASH(name[4]), color);
  843.             }
  844.             else
  845.             {
  846.                 return SetColourHash(COLOUR_NAME_HASH(name[3]), color);
  847.             }
  848.         }
  849.         else
  850.     #endif
  851.     {
  852.         return SetColourHash(COLOUR_NAME_HASH(name), color);
  853.     }
  854. }
  855.  
  856. #define SetColorHash                    SetColourHash
  857. stock SetColourHash(hash, color)
  858. {
  859.     color &= 0xFFFFFF00;
  860.     #if _YSI_USE_X11
  861.         new
  862.             id = GetColourHash(hash);
  863.         if (id != 0)
  864.         {
  865.             for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  866.             {
  867.                 new
  868.                     iColor = YSI_g_sColours[i][0];
  869.                 if (iColor == hash)
  870.                 {
  871.                     YSI_g_sColours[i][1] = color;
  872.                     return i;
  873.                 }
  874.                 else if (iColor == 0)
  875.                 {
  876.                     // Tried to rename an X11 colour.
  877.                     return -1;
  878.                 }
  879.             }
  880.         }
  881.     #endif
  882.     for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  883.     {
  884.         new
  885.             iColor = YSI_g_sColours[i][0];
  886.         if (iColor == hash || iColor == 0)
  887.         {
  888.             YSI_g_sColours[i][0] = hash;
  889.             YSI_g_sColours[i][1] = color;
  890.             return i;
  891.         }
  892.     }
  893.     return -1;
  894. }
  895.  
  896. // This now uses REVERSE bernstein hash for switch compatibility.
  897.  
  898. #define GetColor GetColour
  899. stock GetColour(const name[], alpha = 0xAA)
  900. {
  901.     //if (name[0] | 0x20 == 'x' && name[1] == '1' && name[2] == '2')
  902.     #if _YSI_USE_X11
  903.         if (!strcmp(name, "X11", true, 3))
  904.         {
  905.             if (name[3] == ' ' || name[3] == '_')
  906.             {
  907.                 return GetColourHash(COLOUR_NAME_HASH(name[4]), alpha);
  908.             }
  909.             else
  910.             {
  911.                 return GetColourHash(COLOUR_NAME_HASH(name[3]), alpha);
  912.             }
  913.         }
  914.         else
  915.     #endif
  916.     {
  917.         return GetColourHash(COLOUR_NAME_HASH(name), alpha);
  918.     }
  919. }
  920.  
  921. #define GetColorHash GetColourHash
  922. stock GetColourHash(hash, alpha = 0xAA)
  923. {
  924.     alpha &= 0xFF;
  925.     // Do the REVERSE hash from YHash
  926.     #if _YSI_USE_X11
  927.         #include <YSI\internal\y_x11switch>
  928.     #endif
  929.     // Do the default code here.
  930.     for (new i = 0; i != MAX_TEXT_COLOURS; ++i)
  931.     {
  932.         new
  933.             iColor = YSI_g_sColours[i][0];
  934.         if (iColor == hash)
  935.         {
  936.             return alpha | YSI_g_sColours[i][1];
  937.         }
  938.         else if (iColor == 0)
  939.         {
  940.             return 0;
  941.         }
  942.     }
  943.     return 0;
  944. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement