如何使用颜色矩阵交换两种颜色?例如,交换红色和蓝色很容易。矩阵看起来像:
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
0 0 0 1 0
0 0 0 0 1
那么我怎样才能交换任何两种颜色呢?例如,Color1具有R1,G1,B1和Color2具有R2,G2,B2。
编辑:通过交换,我的意思是Color1将转换为color2,color2将转换为color1。看起来我需要一个反射转换。如何计算?
删除了GIMP参考。抱歉混淆。
答案 0 :(得分:1)
这似乎是GIMP源中color-exchange.c文件的一部分,它循环遍历所有像素,如果一个像素符合所选标准(可以是一系列颜色),则将其与所选像素交换颜色:
for (y = y1; y < y2; y++)
{
gimp_pixel_rgn_get_row (&srcPR, src_row, x1, y, width);
for (x = 0; x < width; x++)
{
guchar pixel_red, pixel_green, pixel_blue;
guchar new_red, new_green, new_blue;
guint idx;
/* get current pixel-values */
pixel_red = src_row[x * bpp];
pixel_green = src_row[x * bpp + 1];
pixel_blue = src_row[x * bpp + 2];
idx = x * bpp;
/* want this pixel? */
if (pixel_red >= min_red &&
pixel_red <= max_red &&
pixel_green >= min_green &&
pixel_green <= max_green &&
pixel_blue >= min_blue &&
pixel_blue <= max_blue)
{
guchar red_delta, green_delta, blue_delta;
red_delta = pixel_red > from_red ?
pixel_red - from_red : from_red - pixel_red;
green_delta = pixel_green > from_green ?
pixel_green - from_green : from_green - pixel_green;
blue_delta = pixel_blue > from_blue ?
pixel_blue - from_blue : from_blue - pixel_blue;
new_red = CLAMP (to_red + red_delta, 0, 255);
new_green = CLAMP (to_green + green_delta, 0, 255);
new_blue = CLAMP (to_blue + blue_delta, 0, 255);
}
else
{
new_red = pixel_red;
new_green = pixel_green;
new_blue = pixel_blue;
}
/* fill buffer */
dest_row[idx + 0] = new_red;
dest_row[idx + 1] = new_green;
dest_row[idx + 2] = new_blue;
/* copy alpha-channel */
if (has_alpha)
dest_row[idx + 3] = src_row[x * bpp + 3];
}
/* store the dest */
gimp_pixel_rgn_set_row (&destPR, dest_row, x1, y, width);
/* and tell the user what we're doing */
if (!preview && (y % 10) == 0)
gimp_progress_update ((gdouble) y / (gdouble) height);
}
修改/ ADDITION 强>
另一种可以将红色转换为蓝色的方法是使用此矩阵:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
-1 0 1 0 1
真正重要的唯一值是此矩阵中的底部值。
这与从红色减去255,保持绿色相同,然后将255添加到蓝色相同。你可以像这样将alpha减半:
-1 0 1 -0.5 1
所以(就像gimp源一样)你只需找到每个通道的当前颜色和目标颜色之间的差异,然后应用差异。您可以使用0到1之间的值来代替0到255之间的通道值。
您可以将其从红色更改为绿色,如下所示:
-1 1 0 0 1
请看这里获取一些好消息:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms533875%28v=vs.85%29.aspx
祝你好运。答案 1 :(得分:0)
我通过D3DXMatrixReflect
使用垂直于矢量AB并与AB的中点相交的平面创建反射矩阵来解决它。
D3DXVECTOR3 AB( colorA.r-colorB.r, colorA.g-colorB.g, colorA.b-colorB.b );
D3DXPLANE plane( AB.x, AB.y, AB.z, -AB.x*midpoint.x-AB.y*midpoint.y-AB.z*midpoint.z );
D3DXMatrixReflect