如何使用颜色矩阵交换两种颜色?例如交换红色和蓝色很容易。矩阵看起来像:
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 引用已删除。抱歉造成混淆。
最佳答案
这似乎是 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);
}
编辑/添加
将红色转换为蓝色的另一种方法是使用此矩阵:
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 源代码一样)您只需要为每个 channel 找到当前颜色和目标颜色之间的差异,然后应用差异。您可以使用 0 到 1 之间的值,而不是 0 到 255 之间的 channel 值。
你可以像这样把它从红色变成绿色:
-1 1 0 0 1
在这里查看一些有用的信息:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms533875%28v=vs.85%29.aspx
祝你好运。
关于c++ - 使用颜色矩阵交换两种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17978285/