Advertisement
IllidanS4

i_colors

Sep 14th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.07 KB | None | 0 0
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2015 IllidanS4
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. //Version 1.0
  21. //Documentation: http://forum.sa-mp.com/showthread.php?p=3576792#post3576792
  22. //Do not distribute without linking to the SA-MP Forums thread.
  23. #include <core>
  24. #include <string>
  25. #include <float>
  26.  
  27. /*Color components*/
  28.  
  29. stock CreateColorComponentsArray(color)
  30. {
  31.     new components[4 char];
  32.     components[0] = color;
  33.     return components;
  34. }
  35.  
  36. stock GetColorComponentsArray(color, components[4 char])
  37. {
  38.     components[0] = color;
  39. }
  40.  
  41. stock CreateColor(components[4 char])
  42. {
  43.     return components[0];
  44. }
  45.  
  46. stock GetColorComponents(color, &r=0, &g=0, &b=0, &a=0)
  47. {
  48.     new c[4 char];
  49.     c[0] = color;
  50.     r = c{0};
  51.     g = c{1};
  52.     b = c{2};
  53.     a = c{3};
  54.     return c;
  55. }
  56.  
  57. stock CreateColorRGBA(r, g, b, a=0xFF)
  58. {
  59.     return (r << 24) | (g << 16) | (b << 8) | a;
  60. }
  61.  
  62. /*Hex tools*/
  63.  
  64. stock ishex(c)
  65. {
  66.     return '0' <= c <= '9' || 'A' <= toupper(c) <= 'F';
  67. }
  68.  
  69. stock hexval(string[])
  70. {
  71.     new val;
  72.     for(new i = 0, c; (c = string[i]) != 0; i++)
  73.     {
  74.         if('0' <= c <= '9')
  75.         {
  76.             val = (val << 4) | (c-'0');
  77.         }else if('A' <= toupper(c) <= 'F')
  78.         {
  79.             val = (val << 4) | (0xA+c-'A');
  80.         }else break;
  81.     }
  82.     return val;
  83. }
  84.  
  85. stock hexchar(i)
  86. {
  87.     i = i & 0xF;
  88.     if(0 <= i <= 9) return i+'0';
  89.     if(10 <= i <= 15) return i-10+'A';
  90.     return '-';
  91. }
  92.  
  93. /* Color strings */
  94.  
  95. stock InsertColorString(string[], col)
  96. {
  97.     col = col >> 8;
  98.     for(new i = 0; i <= 5; i++)
  99.     {
  100.         string[5-i] = hexchar((col & (0xF << (i*4))) >>> (i*4));
  101.     }
  102. }
  103.  
  104. stock GetColorCode(col)
  105. {
  106.     new str[9] = "{      }";
  107.     InsertColorString(str[1], col);
  108.     return str;
  109. }
  110.  
  111. stock ReformatColorTags(color, string[], size = sizeof(string))
  112. {
  113.     if(string[0] == 1 && string[1] == 0) return 0;
  114.     if(size == -1) size = strlen(string)+1;
  115.     new i, c;
  116.     for(i = 0; (c = string[i]) != 0 && i < size; i++)
  117.     {
  118.         if(c == '{' && IsColorCode(string[i+1])) //{RRGGBB{
  119.         {
  120.             if(string[i+7] == '{')
  121.             {
  122.                 new col = hexval(string[i+1]) << 8;
  123.                 if(CompareColorsRGB(color, col)) //set to already applied color
  124.                 {
  125.                     strdel(string, i, i+8);
  126.                 }else if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //immediate color change
  127.                 {
  128.                     strdel(string, i-8, i);
  129.                     string[i-1] = '}';
  130.                 }else{
  131.                     string[i+7] = '}';
  132.                     i += 8;
  133.                 }
  134.                 i += ReformatColorTags(col, string[i], size-i);
  135.                 c = string[i];
  136.                 if(c == '}' && string[i+1] == '}')
  137.                 {
  138.                     strdel(string, i, i+2);
  139.                     if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //a color code before, use it
  140.                     {
  141.                         InsertColorString(string[i-7], color);
  142.                     }else if(string[i] == '{' && IsColorCode(string[i+1]) && string[i+8] == '}') //a color code after, use it
  143.                     {
  144.                         InsertColorString(string[i+1], color);
  145.                         i += 8;
  146.                     }else if(string[i] != 0) //don't add color to the end of the string
  147.                     {
  148.                         strins(string, GetColorCode(color), i, size);
  149.                         i += 8;
  150.                     }
  151.                 }
  152.                 i -= 1;
  153.             }else if(string[i+7] == '}') //{RRGGBB}
  154.             {
  155.                 new col = hexval(string[i+1]) << 8;
  156.                 if(CompareColorsRGB(color, col)) //set to already applied color
  157.                 {
  158.                     strdel(string, i, i+8);
  159.                 }else if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //immediate color change
  160.                 {
  161.                     strdel(string, i-8, i);
  162.                     color = col;
  163.                 }else{
  164.                     color = col;
  165.                     i += 8;
  166.                 }
  167.                 i -= 1;
  168.             }
  169.         }else if(c == '}' && string[i+1] == '}') //}}
  170.         {
  171.             return i;
  172.         }
  173.     }
  174.     if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //color at the end of the string
  175.     {
  176.         strdel(string, i-8, i);
  177.     }
  178.     return i;
  179. }
  180.  
  181. stock RemoveRedundantColors(color, string[])
  182. {
  183.     if(string[0] == 1 && string[1] == 0) return color;
  184.     new i, c;
  185.     for(i = 0; (c = string[i]) != 0; i++)
  186.     {
  187.         if(c == '{')
  188.         {
  189.             if(IsColorCode(string[i+1]) && string[i+7] == '}')
  190.             {
  191.                 new col = hexval(string[i+1]) << 8;
  192.                 if(CompareColorsRGB(color, col)) //set to already applied color
  193.                 {
  194.                     strdel(string, i, i+8);
  195.                     i -= 1;
  196.                 }else if(string[i+8] == '{' && IsColorCode(string[i+9]) && string[i+15] == '}') //immediate color change
  197.                 {
  198.                     color = hexval(string[i+9]);
  199.                     strdel(string, i, i+8);
  200.                     i -= 1;
  201.                 }else{
  202.                     color = col;
  203.                 }
  204.             }
  205.         }
  206.     }
  207.     if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //color at the end of the string
  208.     {
  209.         color = hexval(string[i-7]);
  210.         strdel(string, i-8, i);
  211.     }
  212.     return color;
  213. }
  214.  
  215. stock ReplaceColorParentheses(string[], left='[', right=']')
  216. {
  217.     for(new i = 0, c; (c = string[i]) != 0; i++)
  218.     {
  219.         if(c == left && IsColorCode(string[i+1]) && string[i+7] == right)
  220.         {
  221.             string[i] = '{';
  222.             string[i+7] = '}';
  223.             i += 7;
  224.         }
  225.     }
  226. }
  227.  
  228. stock IsColorCode(string[])
  229. {
  230.     for(new i = 0; i < 6; i++) if(!ishex(string[i])) return false;
  231.     return true;
  232. }
  233.  
  234. /* Color operations */
  235.  
  236. stock RGBAtoARGB(col)
  237. {
  238.     return ((col & 0xFFFFFF00) >> 8) | ((col & 0xFF) << 24);
  239. }
  240.  
  241. stock ARGBtoRGBA(col)
  242. {
  243.     return ((col & 0x00FFFFFF) << 8) | ((col & 0xFF000000) >> 24);
  244. }
  245.  
  246. stock CompareColorsRGB(col1, col2)
  247. {
  248.     return col1 >> 8 == col2 >> 8;
  249. }
  250.  
  251. stock BlendColors(col1, col2, Float:t)
  252. {
  253.     new c1[4 char], c2[4 char];
  254.     GetColorComponentsArray(col1, c1);
  255.     GetColorComponentsArray(col2, c2);
  256.     if(c2{3} == 0)
  257.     {
  258.         return CreateColorRGBA(
  259.             c1{0}, c1{1}, c1{2},
  260.             BlendAlphaComponent(c1{3}, c2{3}, t)
  261.         );
  262.     }else{
  263.         return CreateColorRGBA(
  264.             BlendColorComponent(c1{0}, c2{0}, t),
  265.             BlendColorComponent(c1{1}, c2{1}, t),
  266.             BlendColorComponent(c1{2}, c2{2}, t),
  267.             BlendAlphaComponent(c1{3}, c2{3}, t)
  268.         );
  269.     }
  270. }
  271.  
  272. static stock BlendColorComponent(c1, c2, Float:t)
  273. {
  274.     return floatround(floatsqroot((1.0-t)*c1*c1 + t*c2*c2));
  275. }
  276.  
  277. static stock BlendAlphaComponent(a1, a2, Float:t)
  278. {
  279.     return floatround((1.0-t)*a1 + t*a2);
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement