c++ - Swap two colors using color matrix -
how can swap 2 colors using color matrix? instance swapping red , blue easy. matrix like:
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
so how can swap 2 colors in general? example, there color1 r1, g1, b1 , color2 r2, g2, b2.
edit: swap mean color1 translate color2 , color2 translate color1. looks need reflection transformation. how calculate it?
gimp reference removed. sorry confusion.
this appears section of color-exchange.c file in gimp source cycles through pixels , if pixel meets chosen criteria(which can range of colors), swaps chosen color:
(y = y1; y < y2; y++) { gimp_pixel_rgn_get_row (&srcpr, src_row, x1, y, width); (x = 0; x < width; x++) { guchar pixel_red, pixel_green, pixel_blue; guchar new_red, new_green, new_blue; guint idx; /* 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 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 dest */ gimp_pixel_rgn_set_row (&destpr, dest_row, x1, y, width); /* , tell user we're doing */ if (!preview && (y % 10) == 0) gimp_progress_update ((gdouble) y / (gdouble) height); }
edit/addition
another way have transformed red blue matrix:
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
the values matter bottom ones in matrix.
this same saying subtract 255 red, keep green same, , add 255 blue. cut alpha in half so:
-1 0 1 -0.5 1
so (just gimp source) need find difference between current color , target color, each channel, , apply difference. instead of channel values 0 255 use values 0 1.
you have changed red green so:
-1 1 0 0 1
see here info:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms533875%28v=vs.85%29.aspx
good luck.
Comments
Post a Comment