I am converting an image to use a specific colour palette using PHP. My code is successfully looping through each colour in the source image, and comparing it to the colour palette, and finding the closets colour. However, sometimes I get some odd matches.
I have found many colour distance equations on StackOverflow (and other resources) but none are returning better matches.
Here is the palette I'm using:
Here is the palette n a PHP array:
$colours = array(
'#B3D7D1',
'#DD982E',
'#AD6140',
'#c01111',
'#F47B30',
'#E0E0E0',
'#184632',
'#A0BCAC',
'#923978',
'#F785B1',
'#61AFFF'
);
And here is a testing image I am converting:
However, a few of the greens are being matched to the dark purple, here is my result:
And here is the equation I'm using to calculate colour distance:
$distance = sqrt($delta_r * $delta_r + $delta_g * $delta_g + $delta_b * $delta_b);
Here is the RGB of the colour I'm trying to find a match for and the one my code matches it with:
Array ( [0] => 116 [1] => 136 [2] => 115 )
Array ( [0] => 146 [1] => 57 [2] => 120 )
Which returns a distance of 85.
Just a note, the green that I was expecting to get matched gets a distance of 88.
Mathematically the dark purple is the closet colour. But visually it's not. Does anyone know of a better equation I can use? Maybe one that considers the overall colour instead of just the individual parts?
Full code is available here: /
I am converting an image to use a specific colour palette using PHP. My code is successfully looping through each colour in the source image, and comparing it to the colour palette, and finding the closets colour. However, sometimes I get some odd matches.
I have found many colour distance equations on StackOverflow (and other resources) but none are returning better matches.
Here is the palette I'm using:
Here is the palette n a PHP array:
$colours = array(
'#B3D7D1',
'#DD982E',
'#AD6140',
'#c01111',
'#F47B30',
'#E0E0E0',
'#184632',
'#A0BCAC',
'#923978',
'#F785B1',
'#61AFFF'
);
And here is a testing image I am converting:
However, a few of the greens are being matched to the dark purple, here is my result:
And here is the equation I'm using to calculate colour distance:
$distance = sqrt($delta_r * $delta_r + $delta_g * $delta_g + $delta_b * $delta_b);
Here is the RGB of the colour I'm trying to find a match for and the one my code matches it with:
Array ( [0] => 116 [1] => 136 [2] => 115 )
Array ( [0] => 146 [1] => 57 [2] => 120 )
Which returns a distance of 85.
Just a note, the green that I was expecting to get matched gets a distance of 88.
Mathematically the dark purple is the closet colour. But visually it's not. Does anyone know of a better equation I can use? Maybe one that considers the overall colour instead of just the individual parts?
Full code is available here: https://github/codeadamca/php-colour-palette/
Share Improve this question edited Feb 16 at 1:23 Adam asked Feb 15 at 22:58 AdamAdam 4331 gold badge6 silver badges17 bronze badges 3- 2 I tried to compute a simple 3D distance, which is exactly what you seem to be doing, but I get a better matching color. I think you must have made a mistake somewhere? Can I be sure of that? No. Why? Because your question doesn't contain a minimal reproducible example. You could add one to your question, to prove me wrong...