Jump to content

Color Manipulation


kjanceski

Recommended Posts

Hi, I have a problem and I'm hoping that someone will help me.

I have an Array with colors in it RGB and want to find value in that Array that is most simular to other predefined color(RGB).

$array[1]="R255 G121 B169";
$array[2]="R212 G100 B123";
.
.
$array[n]="R123 G200 B123";

$color="R123 G124 B100";

 

I want to find color in $array[] that is most similar to $color. I am new to GD2 graphic functions and if u have any suggest please reply. If there is no function in GD2 that can do the job just give me idea for solving the problem.

 

Link to comment
https://forums.phpfreaks.com/topic/74558-color-manipulation/
Share on other sites

Well you could always explode the arrays and then check the difference between them.

 

eg:

 

$newarray=explode(' ',$array[1]);
$array[1][r]=str_replace("R", "", $newarray[0]);
$array[1][g]=str_replace("G", "", $newarray[1]);
$array[1][b]=str_replace("B", "", $newarray[3]);
$array[1][total]=$array[1][r]+$array[1][g]+$array[1][b];

 

All in a loop of some kind to isolate the values.  Then you could take the total of the differences of r, g, and b for each color.  I'd go on but I have a feeling there is a better way then comparing 1 to 2, 1 to 3, 1 to n, 2 to 1, 2 to 3, etc then finding the smallest difference.  You could do it that way but it would be a massive loop and I have a feeling that someone may know a better way.

 

If not then take the total of r g and b and then run it through a loop to find which difference of the total is the smallest making sure that a color can't be compared to itself (I believe that would be the most accurate way).  Again it would work but I feel there is probably a better and easier function to do this that I can't think of at the moment.

Link to comment
https://forums.phpfreaks.com/topic/74558-color-manipulation/#findComment-376843
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.