• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Library] Similar hexes
#1
This is more of a snippet than a library. However, it holds these functions (they all depend on each other):

has_alpha(color)
Checks if HEX (read: integer) input is RRGGBBAA. Does not work properly for other formats (except RRGGBB).
For example, AARRGG would return 0/false.
PHP Code:
if (has_alpha(0xFF0990AA))
{
??? print(
"I am alpha!");
}
else
{
??? print(
"I am merely a beta");
}
//Would print: I am alpha! 

strip_alpha(color)
Strips alpha from HEX if it has any (relies on has_alpha()). Like with has_alpha, inputting AARRGGBB would output AARRGG.
PHP Code:
new const colors[] = {
    
0xAFAFAF,
    
0x124356FF,
    
0xFF0990AA,
    
0x000001
};
for (new 
isizeof(colors); i)
{
    
printf("%x"strip_alpha(colors[i]));
}
/* Would output:
AFAFAF
124356
FF0990
1 //! No bits to shift and check for R and G in 0x000001. 

similar_hexes(hex_a, hex_b, difference = 150)
This checks if two hexes are alike color-wise.
Difference parameter can be used to increase or decrease the difference check in both colors.
Some colors might return false while they are pretty similar and some might return true while they aren't quite similar. Most of the tests I did turned out pretty okay, though.

PHP Code:
#define COLOR_RED 0xFF0000FF
if (similar_hexes(GetPlayerColor(playerid), COLOR_RED))
{
    print(
"Why are you so red?"); //Note that 0x880000FF (darkred) would return false


The functions:
PHP Code:
bool:has_alpha(color)
    return (
color >> 24 0x000000FF) ? true false;

strip_alpha(color)
    return (
has_alpha(color) ? ((((color >>> 24) & 0xFF) << 16) | (((color >>> 16) & 0xFF) << 8) | (((color >>> 8) & 0xFF))) : color);
    
similar_hexes(hex_ahex_bdifference 152)
{
    
hex_a strip_alpha(hex_a);
    
hex_b strip_alpha(hex_b);
    
    new 
R[2], G[2], B[2], diff[3];
    
R[0] = (hex_a >>> 16) & 0xFF;
    
G[0] = (hex_a >>> 8) & 0xFF;
    
B[0] = (hex_a) & 0xFF;
    
R[1] = (hex_b >>> 16) & 0xFF;
    
G[1] = (hex_b >>> 8) & 0xFF;
    
B[1] = (hex_b) & 0xFF;

    
diff[0] = floatround(difference 0.14);
    
diff[1] = floatround(difference 0.2);
    
diff[2] = floatround(difference 0.48);

    return
    (
        (
R[0] - diff[0] <= R[1] && R[1] <= R[0]  diff[0]) &&
        (
G[0] - diff[1] <= G[1] && G[1] <= G[0]  diff[1]) &&
        (
B[0] - diff[2] <= B[1] && B[1] <= B[0]  diff[2])
    );

  Reply


Forum Jump: