Extract an RGB channel of an image
JavaScript (ES6), 29 bytes
a=>n=>a.map(b=>b.map(c=>c&n))
Input is a 2D array of 24-bit integers (e.g. [[0x0000ff,0x00ff00],[0xff0000,0xffffff]]
) and 16711680
for red, 65280
for green, 255
for blue. If this isn't valid, try this instead:
JavaScript (ES6), 48 bytes
a=>n=>a.map(b=>b.map(c=>c.map((d,i)=>i==n?d:0)))
Input is a 3D array of color values and 0
for red, 1
for green, 2
for blue.
Mathematica, 13 bytes
ImageMultiply
This should be the legitimate version of JungHwan Min's answer. This function takes the image and one of Red
, Green
, Blue
as input. For example:
As a fun-fact, there's also ColorSeparate
which gives you individual channels, but it returns them as single-channel/greyscale images, so you'd need to multiply them by the colour afterwards anyway.
Bash + ImageMagick, 35 32 27 bytes
mogrify -channel $1 -fx 0 i
Assumes the image is in the file i
and the script takes one of RG
, BG
, BR
for blue, red, and green respectively; outputs to the file i
.