17.05.2020, 21:04
(
Last edited by Kwarde; 18/05/2020 at 01:36 AM.
)
Couldn't find a function for this so figured I created one: similar_hexes.
It compares two hex colors (in 0xRRGGBB format, not any other format such as 0xRRGGBBAA or 0xAARRGGBB) and decides if they are similar colors or not:
It is not always totally accurate (eg. 5566FF and 6655CC don't look alot like each other but it is returned as "similar" anyway -just like eg. E7E7E7 and F0F0B0) but I got the best results with most tests so far with "difference = 150".
Had to tweak differences for R,G and B a bit because otherwise it would return "not similar" if the colors were actually similar (most being 'faulty' in the BB value) - and the R value would often return "similar" while they weren't.
Feel free to improve it.
If there's a better function around I'd like to know aswell
Here's a small (and very poorly scripted) testscript (.html) to test it if you'd like; https://pastebin.com/mhp0pa5g
Insert RRGGBB color (without 0x) in the two boxes and press "Check".
It will show both colors you entered and will print "YES" if they are similar and "NO" if they are not.
It compares two hex colors (in 0xRRGGBB format, not any other format such as 0xRRGGBBAA or 0xAARRGGBB) and decides if they are similar colors or not:
pawn Code:
similar_hexes(hex_a, hex_b, difference = 150)
{
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 //returns 1(true) if they are similar and 0(false) if they are not.
(
(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])
);
}
Had to tweak differences for R,G and B a bit because otherwise it would return "not similar" if the colors were actually similar (most being 'faulty' in the BB value) - and the R value would often return "similar" while they weren't.
Feel free to improve it.
If there's a better function around I'd like to know aswell
Here's a small (and very poorly scripted) testscript (.html) to test it if you'd like; https://pastebin.com/mhp0pa5g
Insert RRGGBB color (without 0x) in the two boxes and press "Check".
It will show both colors you entered and will print "YES" if they are similar and "NO" if they are not.