Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The MIT License (MIT)
- Copyright (c) 2015 IllidanS4
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- //Version 1.0
- //Documentation: http://forum.sa-mp.com/showthread.php?p=3576792#post3576792
- //Do not distribute without linking to the SA-MP Forums thread.
- #include <core>
- #include <string>
- #include <float>
- /*Color components*/
- stock CreateColorComponentsArray(color)
- {
- new components[4 char];
- components[0] = color;
- return components;
- }
- stock GetColorComponentsArray(color, components[4 char])
- {
- components[0] = color;
- }
- stock CreateColor(components[4 char])
- {
- return components[0];
- }
- stock GetColorComponents(color, &r=0, &g=0, &b=0, &a=0)
- {
- new c[4 char];
- c[0] = color;
- r = c{0};
- g = c{1};
- b = c{2};
- a = c{3};
- return c;
- }
- stock CreateColorRGBA(r, g, b, a=0xFF)
- {
- return (r << 24) | (g << 16) | (b << 8) | a;
- }
- /*Hex tools*/
- stock ishex(c)
- {
- return '0' <= c <= '9' || 'A' <= toupper(c) <= 'F';
- }
- stock hexval(string[])
- {
- new val;
- for(new i = 0, c; (c = string[i]) != 0; i++)
- {
- if('0' <= c <= '9')
- {
- val = (val << 4) | (c-'0');
- }else if('A' <= toupper(c) <= 'F')
- {
- val = (val << 4) | (0xA+c-'A');
- }else break;
- }
- return val;
- }
- stock hexchar(i)
- {
- i = i & 0xF;
- if(0 <= i <= 9) return i+'0';
- if(10 <= i <= 15) return i-10+'A';
- return '-';
- }
- /* Color strings */
- stock InsertColorString(string[], col)
- {
- col = col >> 8;
- for(new i = 0; i <= 5; i++)
- {
- string[5-i] = hexchar((col & (0xF << (i*4))) >>> (i*4));
- }
- }
- stock GetColorCode(col)
- {
- new str[9] = "{ }";
- InsertColorString(str[1], col);
- return str;
- }
- stock ReformatColorTags(color, string[], size = sizeof(string))
- {
- if(string[0] == 1 && string[1] == 0) return 0;
- if(size == -1) size = strlen(string)+1;
- new i, c;
- for(i = 0; (c = string[i]) != 0 && i < size; i++)
- {
- if(c == '{' && IsColorCode(string[i+1])) //{RRGGBB{
- {
- if(string[i+7] == '{')
- {
- new col = hexval(string[i+1]) << 8;
- if(CompareColorsRGB(color, col)) //set to already applied color
- {
- strdel(string, i, i+8);
- }else if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //immediate color change
- {
- strdel(string, i-8, i);
- string[i-1] = '}';
- }else{
- string[i+7] = '}';
- i += 8;
- }
- i += ReformatColorTags(col, string[i], size-i);
- c = string[i];
- if(c == '}' && string[i+1] == '}')
- {
- strdel(string, i, i+2);
- if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //a color code before, use it
- {
- InsertColorString(string[i-7], color);
- }else if(string[i] == '{' && IsColorCode(string[i+1]) && string[i+8] == '}') //a color code after, use it
- {
- InsertColorString(string[i+1], color);
- i += 8;
- }else if(string[i] != 0) //don't add color to the end of the string
- {
- strins(string, GetColorCode(color), i, size);
- i += 8;
- }
- }
- i -= 1;
- }else if(string[i+7] == '}') //{RRGGBB}
- {
- new col = hexval(string[i+1]) << 8;
- if(CompareColorsRGB(color, col)) //set to already applied color
- {
- strdel(string, i, i+8);
- }else if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //immediate color change
- {
- strdel(string, i-8, i);
- color = col;
- }else{
- color = col;
- i += 8;
- }
- i -= 1;
- }
- }else if(c == '}' && string[i+1] == '}') //}}
- {
- return i;
- }
- }
- if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //color at the end of the string
- {
- strdel(string, i-8, i);
- }
- return i;
- }
- stock RemoveRedundantColors(color, string[])
- {
- if(string[0] == 1 && string[1] == 0) return color;
- new i, c;
- for(i = 0; (c = string[i]) != 0; i++)
- {
- if(c == '{')
- {
- if(IsColorCode(string[i+1]) && string[i+7] == '}')
- {
- new col = hexval(string[i+1]) << 8;
- if(CompareColorsRGB(color, col)) //set to already applied color
- {
- strdel(string, i, i+8);
- i -= 1;
- }else if(string[i+8] == '{' && IsColorCode(string[i+9]) && string[i+15] == '}') //immediate color change
- {
- color = hexval(string[i+9]);
- strdel(string, i, i+8);
- i -= 1;
- }else{
- color = col;
- }
- }
- }
- }
- if(i >= 8 && string[i-8] == '{' && IsColorCode(string[i-7]) && string[i-1] == '}') //color at the end of the string
- {
- color = hexval(string[i-7]);
- strdel(string, i-8, i);
- }
- return color;
- }
- stock ReplaceColorParentheses(string[], left='[', right=']')
- {
- for(new i = 0, c; (c = string[i]) != 0; i++)
- {
- if(c == left && IsColorCode(string[i+1]) && string[i+7] == right)
- {
- string[i] = '{';
- string[i+7] = '}';
- i += 7;
- }
- }
- }
- stock IsColorCode(string[])
- {
- for(new i = 0; i < 6; i++) if(!ishex(string[i])) return false;
- return true;
- }
- /* Color operations */
- stock RGBAtoARGB(col)
- {
- return ((col & 0xFFFFFF00) >> 8) | ((col & 0xFF) << 24);
- }
- stock ARGBtoRGBA(col)
- {
- return ((col & 0x00FFFFFF) << 8) | ((col & 0xFF000000) >> 24);
- }
- stock CompareColorsRGB(col1, col2)
- {
- return col1 >> 8 == col2 >> 8;
- }
- stock BlendColors(col1, col2, Float:t)
- {
- new c1[4 char], c2[4 char];
- GetColorComponentsArray(col1, c1);
- GetColorComponentsArray(col2, c2);
- if(c2{3} == 0)
- {
- return CreateColorRGBA(
- c1{0}, c1{1}, c1{2},
- BlendAlphaComponent(c1{3}, c2{3}, t)
- );
- }else{
- return CreateColorRGBA(
- BlendColorComponent(c1{0}, c2{0}, t),
- BlendColorComponent(c1{1}, c2{1}, t),
- BlendColorComponent(c1{2}, c2{2}, t),
- BlendAlphaComponent(c1{3}, c2{3}, t)
- );
- }
- }
- static stock BlendColorComponent(c1, c2, Float:t)
- {
- return floatround(floatsqroot((1.0-t)*c1*c1 + t*c2*c2));
- }
- static stock BlendAlphaComponent(a1, a2, Float:t)
- {
- return floatround((1.0-t)*a1 + t*a2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement