Inverting Colors

Using a basic slitscanner code, I am trying to get the slitscan image to change and invert colors once a key is pressed. I uploaded the ofxOpenCV library, and tried to use .invert, but that didn’t work.

Here is the code that I thought would try and change the colors of the slit scan, but it is not doing anything.
`void ofApp::keyPressed(int key){

// if (key == 32){

cout << "Key Pressed";
    ofImage slitScanInvert = slitScanImage;
    ofPixels ssPixels = slitScanInvert.getPixels();
    // 1. get pixels from copied ofImage
    // 2. get the color of each pixels
    // 3. invert the color from eac pixel
    // 4. write new color back to the image
    for (int col = 0; col < slitScanInvert.getWidth(); col++) {
        for (int row = 0; row < slitScanInvert.getHeight(); row++) {
            ofColor c = ssPixels.getColor(col, row);
            ssPixels.setColor(col, row, c.invert());
    slitScanInvert.draw(200, camHeight+40);


you can try to do:

c.r = 255 - c.r;
c.g = 255 - c.g;
c.b = 255 - c.b;

to be honest, you should try to use a shader to do this, as it seems like you are doing it on the whole image at once.
Try using this addon : ofxKsmrFragmentFx .

Try modifying the example by using :
in ofApp.cpp::update():

	fx.getfxUnit(KSMR_FRAGFX_INVERT)->bEnable		= ofGetKeyPressed(" ");

Hope this helps,



1 Like

as an alternative, you could draw it to an fbo and then invert it using ofx::dotfrag::InvertStrobe from my ofxDotFrag addon:

1 Like

hey @npisanti, thanks for that suggestion to use ofxDotFrag.

I am building an “oled screen emulator” and It’s working nice to invert a selected text line on the screen.

I have an isolated class/addon for this that works fine on a clean ofApp. But when including into the main app I am building, I am getting in trouble with alpha transparency alphablending, drawings modes, arb texture or I don’t know… but the rectangle that I am inverting is just full filled black or not drawed.

I tried several combinations of enabling alpha blendings, disabling all other draws into draw() method, to used push/pop Style… without success. Maybe some of the other addons that I am using has some drawing/GL/alpha mode…)

Could be the way that I am cleaning / updating the zones in the fbo (I am really newbie with them) …
I tried too to use not alpha fbo but not worked neither…)

I put some code here to ask you or someone if there’s something strange:

ofFbo rgbaFbo; // the full lcd-screen rectangle
ofFbo fbo2; // the inverted line or pixels zone
ofx::dotfrag::InvertStrobe frag;

void ofxOled::setup()
	rgbaFbo.allocate(128, 64, GL_RGBA);
	ofClear(255,255,255, 0);//white

	fbo2.allocate(128, 9, GL_RGBA);
	frag.allocate( fbo2 ); = true;

// add/draw just a text line into the lcd-screen fbo
// there are other lines that I don't want to erase
// that's the reason why I don't make a ofClear( ) after the begin()
void ofxOled::gPrintln(int x, int y, int color, string txt)

    font.drawString(txt, x, y);



// invert a rectangle zone when called
void ofxOled::gInvertArea(int x, int y, int w, int h)
    // apply shader to invert area into a 2nd fbo
    fbo2.allocate(w, h, GL_RGBA);

    // get the subsection we want to invert from the lcd-screen fbo
    rgbaFbo.getTexture().drawSubsection(0, 0, w, h, x ,y, w, h);


    // 1. apply ofxDotFrag inverter shader
    frag.apply( fbo2 );


    // 2. draw inverted zone upon in front of lcd-screen fbo
    rgbaFbo.begin(); // don't want to clear all, just add upfront
    fbo2.draw( x, y );

// main draw
void ofxOled::draw()

    // draw lcd-screen with the inverted rectangle included 
    rgbaFbo.draw(oLed_x, oLed_y);

    // preview inverted rectangle line only, displaced 200px to the right
    fbo2.draw( oLed_x + 200, oLed_y );


try not allocating fbo2 again in gInvertArea, i don’t know if that is executed each frame but sometimes i have problems if i allocate the fbos too often

well, the thing is that is not drawing every frame.
The testing app is working fine with the same drawing calls frequency than in the bigger app where is failing…

I tried to disabling the allocate on the method, allocating only into setup, but do not change at all: black rectangle.

Not sure if frag allocate must be done too when fbo size changes and is re-allocated… but not helps.

//    frag.allocate( fbo_inverter );
// = true;

I suspect could be something related to drawing modes. So maybe I need to ‘force’ some blend mode when drawing the inverted-fbo into the main oled-fbo…

 //    /// \brief Blend mode is disabled.
 //    ofEnableBlendMode(OF_BLENDMODE_DISABLED);
 //    /// \brief Blend mode used for alpha blending.
 //    ofEnableBlendMode(OF_BLENDMODE_ALPHA);
 //    /// \brief Blend mode used for additive blending.
 //    ofEnableBlendMode(OF_BLENDMODE_ADD);
 //    /// \brief Blend mode used for subtractive blending.
 //    ofEnableBlendMode(OF_BLENDMODE_SUBTRACT);
 //    /// \brief Blend mode used for multiplicative blending.
 //    ofEnableBlendMode(OF_BLENDMODE_MULTIPLY);
 //    /// \brief Blend mode used for screen blending.
 //    ofEnableBlendMode(OF_BLENDMODE_SCREEN);


frag should reallocate itself automatically when fbo size changes, so that won’t be a problem, i understand that blending mode could give some issues with fbo or shader alphas, but i don’t know what is specifically going wrong.

i know it could be time consuming, but could you please try to set up an ofApp with the minimum amount of code to reproduce the bad behavior?

sorry to re-up this.
but I found that my problem was that ofxDotFrag disables the default OF mode ofEnableArbTex().

So the solution when combining some shaders/fx addons seems to enable the respective required arb mode before allocating the fbo for any addon.
If not, you can experience some black drawings of fbo’s/addons.

In this case, after allocating ofxDotFrag, I must enable arb again to avoid breaking the other allocating shaders/addons/fbo’s that requires arb tex enabled.