Exploit
Exploit

Reputation: 6386

Determine the closest hex color against a list of existing hex values

I have two arrays, one array contains the hex color values that will be used whereas the other array contains redundant hex values and need to be matched against the reference array and its value updated to the index of the reference.

For example, here is a sample (shortened version) of the reference I'll be checking against.

$ref_array = array(4 => '000000', 66 => 'C31AOC', 162 => 'AD2823');

And here is the array that needs to be match as close as possible to the reference.

$orig_array = array('1' => '2be944', 2 => '2f3136', 3 => '88110d');

They arent orig_array is not in order against the ref_array.

I'm not sure if this is possible in PHP but let's say the first element in the orig_array is a greenish color and in the reference array the closest color is 2be944 so a new array would be created like this for that example:

$new_array('2be944' => 4) 

It holds the index value of orig_array

How would I do this or something similar?

Upvotes: 4

Views: 4755

Answers (3)

minhaz1
minhaz1

Reputation: 743

There are a few ways to do this. You need a function to find the difference in values between the 2 hex values. You can implement that however you want. It might be easier to just convert it to decimal for finding the difference. If your array isn't sorted already based on the hex values then what you can do is grab each value of the array and compare it and store the difference and index in 2 variables. Iterate through the array and compare the value against each value in the array and whenever you find a difference smaller than what's stored in your variable, you should update the temporary index and diff variables with the new smaller difference. This isn't very efficient as it requires you to go through the entire array. What may be more efficient is implementing a sorted list and then comparing your hex value to the ones in the list and finding its proper place in the sorted list.

For example, in this list: 0 1 4 8 13 19 if you want to find which number is closest in value to the number 10 then all you need to do is find where 10 fits into that list and then compare it to the element before and after and you'll have your answers.

Upvotes: 2

Marty McVry
Marty McVry

Reputation: 2856

Aside from the scientific approach to the matter...

Remember that the hex color code is composed by three 2-digit hex numbers (RGB-values)! So you can't do it all at once, because the first two digits will otherwise increase your decimal value too much)...

Here's how I would do it.

You can split your color code into 3 parts:

  • R = 2B --> 43 decimal
  • G = E9 --> 233 decimal
  • B = 44 --> 68 decimal

You can then compare each color to the references (split them up into R/G/B as well).

If you add the differences, the one with the lowest combined difference should provide a close color resemblance...

Although, as duskwuff already mentioned, color perception is a subjective matter.

Upvotes: 4

user149341
user149341

Reputation:

There are a number of ways to measure the "difference" of two colors. Which ones are more "correct" than others is kind of a subjective matter.

Wikipedia has an article on the matter under Color difference. Prepare to have your head spin a bit.

Upvotes: 0

Related Questions