且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C中的调色板数组查找最接近的RGB值

更新时间:2022-06-17 23:50:59

要找到光学"最接近的颜色,您必须估算要比较的两种颜色的颜色分量之间的差异.

To find the "optical" closest color you have to estimate the difference between the color components of the two color you're comparing.

为此,您必须将24位值拆分为8位颜色分量r,g,b.

In order to do that you have to split the 24 bit value into 8 bit color components r, g, b.

然后比较组件.

一个简单的方法就是将各个分量之差的绝对值求和.

A naive method to do that is just sum the absolute value of the difference of the respective components.

我猜可以找到更准确的公式.

More accurate formulas can be found googling, I guess.

// two colors to compare

int c1;
int c2;

// split c1 and c2 into their respective color components

r1 = c1 / 0x010000;
g1 = (c1 % 0x010000) / 0x00100;
b1 = c1 % 0x000100;

r2 = c2 / 0x010000;
g2 = (c2 % 0x010000) / 0x00100;
b2 = c2 % 0x000100;

// color "distance"

diff = abs( r1 - r2 ) + abs( g1 - g2 ) + abs ( b1 - b2 );