How to change color of ofImage with ofPixels?

I have a white image with a transparent background and I want to control the colors of it. Here’s my code in ofApp.cpp:

void ofApp::setup(){
  image.load("rest4_white.png");
  image.setImageType(OF_IMAGE_COLOR_ALPHA);
}

//--------------------------------------------------------------
void ofApp::update(){
  ofPixels data;
  data.allocate(image.getWidth(), image.getHeight(), 4);
  data = image.getPixels();
  int components = image.getPixels().getBitsPerPixel() / 8;
  for (int y = 0; y < image.getHeight(); y++) {
    for (int x = 0; x < image.getWidth(); x++) {
      int indexLocal = components * (x + image.getWidth() * y);
      ofColor c = data[indexLocal];
      c.r = 255;
      c.g = 0;
      c.b = 0;
      data.setColor(indexLocal, c);
    }
  }
  image.update();
}

I got this technique by combining stuff I found in this forum and from the “Mastering openFrameworks” book (though that uses an older version of OF and deprecated methods).
In the code above I want to display the image in red, but nothing changes.
The initial image was black (it’s a quarter note rest for music notation), but I thought that black has all its RGB components set to 0, so I turned it to white instead, still no luck. The image is below, for reference. Any suggestions?
rest4_white

I think this could work.

The main issue is you are pulling the pixels out of the image but not putting it back in.
There is also an easier way of accessing the pixel color.

void ofApp::setup(){
  image.load("rest4_white.png");
}

//--------------------------------------------------------------
void ofApp::update(){

  ofPixels & pixels = image.getPixelsRef(); 

  for (int y = 0; y < pixels.getHeight(); y++) {
    for (int x = 0; x < pixels.getWidth(); x++) {
     
      auto & c = pixels.getColor(x,y); 
      c.r = 255;
      c.g = 0;
      c.b = 0;
      pixels.setColor(x, y, c);
    }
  }
  //if this doesn't work you can uncomment the line below. 
  //image.setFromPixels(pixels); 
  image.update();
}

That’s great! The only caveat is the ampersand symbol in the auto definition which has to be removed, as it produced the following error:

cannot bind non-const lvalue reference of type ‘ofColor_<unsigned char>&’ to an rvalue of type ‘ofColor_<unsigned char>’

So the line changes to this:
auto c = pixels.getColor(x,y);
Also, this works with the original version of the image, which is black. No need to turn it to white (I did that with Gimp).

Thanks for the solution.

BTW, I was getting a warning that image.getPixelsRef() is deprecated and that I should use image.getPixels() instead, which works exactly the same way, so I changed it to image.getPixels().