Set White Pixels to Transparent

I’ve been looking into the simpleColorKey example which turns purple pixels to black. In my case I’d like to turn white pixels to transparent (I’m using a B&W JPG with no grey tones). The end result doesn’t work as expected, and I can see the color change, but no transparency. Am I missing something simple?

#include “ofApp.h”

void ofApp::setup(){
ofBackground(0, 0, 255);


ofPixelsRef pixels = source.getPixels();
for (uint y = 0; y < source.getHeight(); y ++) {
    for (uint x = 0; x < source.getWidth(); x ++) {
        ofColor color = source.getColor(x, y);
        int threshold = 150;
        if (color.r > threshold && color.g > threshold && color.b > threshold) {
            pixels.setColor(x, y, ofColor(255, 0, 0, 0));


void ofApp::draw(){
keyed.draw(0, 0);

Hi there!

Since you’re loading a JPG, you have no alpha channel. So when you manipulate your pixels, you’re only doing it in three channels, because OF, by default, reverts whatever value you are setting to 255.

For your code to work, after you load your image, do:


And that’s it! :slight_smile:

Ah < facepalm />. Thanks!