View Single Post
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 02-28-2018 , 18:49   Re: Snippet: Setting RGB/RGBA values via a command
Reply With Quote #8

In SM 1.10 and 1.9 soon(tm) You can use MatchAll to make the regex even easier. Although, using args is much better. But, since i decided to test with this type of an example... hf

PHP Code:
#pragma newdecls required
 
#include <sourcemod>
#include <regex>
 
public void OnPluginStart()
{
    
int buff[4];
    
    
char test_strings[][] = {"128 233 255""123 232 22 156""126 130 300""257 220 300 100"};
    
    for(
int i 0sizeof(test_strings); i++)
    {
        
int ret CheckRGBA(test_strings[i], buff, (2) ? true false);
        
        if(
ret == 3)
        {
            
PrintToServer("RGB values %i %i %i"buff[0], buff[1], buff[2]);
        }
        else if(
ret == 4)
        {
            
PrintToServer("RGBA values %i %i %i %i"buff[0], buff[1], buff[2], buff[3]);
        }
        else
        {
            
PrintToServer("Invalid RGB(A) values passed");
        }
    }
}

int CheckRGBA(const char [] szRGBint values[4], bool hasAlpha)
{
    
Regex re =  new Regex("\\b([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\b");
    
int ret = -1;
    
int matches re.MatchAll(szRGB);

    if((
matches == && !hasAlpha) || (matches == && hasAlpha))
    {
        
ret matches;
        for(
int i 0matchesi++)
        {
            
char temp[4];
            
re.GetSubString(1tempsizeof(temp), i);
            
values[i] = StringToInt(temp);
        }
    }
    
delete re;
    return 
ret;

And the output

Code:
RGB values 128 233 255
RGBA values 123 232 22 156
Invalid RGB(A) values passed
Invalid RGB(A) values passed
I wouldnt pay much attention to the fiasco in onpluginstart though... im hella lazy.

Last edited by Dr!fter; 02-28-2018 at 18:52. Reason: Fix trademark
Dr!fter is offline