Or how it should be called? I will change color programmatically. Sorry for my English.

  • 1
    \$\begingroup\$ Are you trying to make these black & white versions in an image editing program like Photoship/GIMP? Or do you want to process the image via code to mask each colour region, on the CPU during texture import or in a pixel shader? The more context you can give us, the better your chances of getting answers that are useful for your application. \$\endgroup\$
    – DMGregory
    Sep 10, 2018 at 12:24
  • \$\begingroup\$ what is stopping u from grabbing a single color in an image editing tool, remove all other colors, and save the image? \$\endgroup\$ Sep 11, 2018 at 12:00

1 Answer 1


As far as I understand you, you want to split your colored image into several black–white images, of which each indicates the pixels of one specific color. And that in an automated fashion.

As TomTsagk already commented, you might just use an image manipulation program. For instance, I know that GIMP has a scripting facility where you can write scripts to automated image processing. Or you use more specialized tools like convert as part of ImageMagick, which is included in many Linux distributions. For example, the following command creates a black–white image out.png which highlights the color with the hex triplet #abcdef from the image in.png:

convert "in.png" -fill black +opaque "#abcdef" -fill white -opaque "#abcdef" "out.png"

If you want instead to create these images at runtime, you can write a simple function given that you can access the pixels of your image like an array. In this case, you might just allocate a second array for your output image, and use something like the following C++ code to generate the black–white image:

void highlight(Color theColor, Color* in, Color* out, size_t size) {
  for (int i = 0; i < size; i++) {
    if (in[i] == theColor) {
      out[i] = Color::White;
    } else {
      out[i] = Color::Black;

Finally, if you just want to use this in an (e.g. OpenGL) shader to alternate your output, (e.g. draw all but the specified color) you might use something like the following GLSL Fragment Shader code:

uniform sampler2D image; // The input image
uniform vec3 theColor;   // The color to highligh/discard, given as RGB color vector
in vec2 tc;              // The texture coordinate calculated in the Vertex shader
out vec4 outColor;       // The output color of this Fragment shader

void main() {
  vec3 color = texture(image, textureCoordinate).rgb;
  if (color == theColor) {
    discard; // Do not output anything
  } else {
    outColor = color; // output the color from the image

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .