根据现有十六进制值列表确定最接近的十六进制颜色 [英] determine the closest hex color against a list of existing hex values

查看:145
本文介绍了根据现有十六进制值列表确定最接近的十六进制颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组,一个数组包含将使用的十六进制颜色值,而另一个数组包含冗余十六进制值,需要与参考数组匹配,并将其值更新为引用的索引。

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');

它们arent orig_array不是为了反对ref_array。

they arent orig_array is not in order against the ref_array.

我不知道这是否可能在php,但让我们说orig_array中的第一个元素是绿色的,在参考数组中最接近的颜色是2be944将创建一个新数组,例如:

I'm not sure if this is possible in php but lets 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) 

它包含orig_array的索引值

it holds the index value of orig_array

我这样做或类似的东西吗?

how would i do this or something similar?

感谢

推荐答案

有几种方法来做到这一点。您需要一个函数来找出2个十六进制值之间的值的差异。你可以实现你想要的。只是将它转换为十进制以找到差异可能更容易。如果你的数组没有基于十六进制值排序,那么你可以做的是抓取数组的每个值,并比较它,并将差异和索引存储在2个变量中。迭代通过数组,并将值与数组中的每个值进行比较,并且每当发现小于存储在变量中的差异时,您应该使用新的较小差异更新临时索引和差异变量。这不是很有效率,因为它需要你浏览整个数组。更有效的方法是实现排序列表,然后将十六进制值与列表中的值进行比较,并在排序列表中找到它的正确位置。

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.

例如,在此列表中: 0 1 4 8 13 19 如果要查找哪个数字最接近in value to the number 10 那么所有你需要做的是找到10适合该列表,然后将其与元素前后比较,你会有你的答案。

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.

这篇关于根据现有十六进制值列表确定最接近的十六进制颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆