Skip to content

Instantly share code, notes, and snippets.

@prisonerjohn
Last active August 27, 2019 18:46
Show Gist options
  • Save prisonerjohn/283262100c7d748a437f255a9a2a1415 to your computer and use it in GitHub Desktop.
Save prisonerjohn/283262100c7d748a437f255a9a2a1415 to your computer and use it in GitHub Desktop.
Get pixel color from an ofPixels object.
#include "ofMain.h"
#include "ofApp.h"
int main()
{
ofSetupOpenGL(512, 512, OF_WINDOW);
ofRunApp(new ofApp());
}
#include "ofApp.h"
void ofApp::setup()
{
// Load the shapes image.
shapesImg.load("shapes.png");
// Set the window size to match the image.
ofSetWindowShape(shapesImg.getWidth(), shapesImg.getHeight());
}
void ofApp::draw()
{
// Draw the image as the background.
ofSetColor(255);
shapesImg.draw(0, 0);
// Get a reference to the image pixels.
ofPixels shapesPix = shapesImg.getPixels();
// Get the color value under the mouse.
ofColor color = shapesPix.getColor(mouseX, mouseY);
// Draw a rectangle under the mouse using the pixel color.
ofFill();
ofSetColor(color);
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50);
// Add an outline so we can see the rectangle better.
ofNoFill();
ofSetColor(0);
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50);
}
#include "ofApp.h"
void ofApp::setup()
{
// Load the shapes image.
shapesImg.load("shapes.png");
// Set the window size to match the image.
ofSetWindowShape(shapesImg.getWidth(), shapesImg.getHeight());
}
void ofApp::draw()
{
// Draw the image as the background.
ofSetColor(255);
shapesImg.draw(0, 0);
// Get a reference to the image pixel data.
unsigned char* shapesData = shapesImg.getPixels().getData();
// Get the color value under the mouse.
int numChannels = shapesImg.getPixels().getNumChannels();
int pixelIndex = mouseY * shapesImg.getWidth() + mouseX;
ofColor color = ofColor(
shapesData[pixelIndex * numChannels + 0], // R
shapesData[pixelIndex * numChannels + 1], // G
shapesData[pixelIndex * numChannels + 2] // B
);
// Draw a rectangle under the mouse using the pixel color.
ofFill();
ofSetColor(color);
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50);
// Add an outline so we can see the rectangle better.
ofNoFill();
ofSetColor(0);
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50);
}
#include "ofApp.h"
void ofApp::setup()
{
// Load the shapes image.
shapesImg.load("shapes.png");
// Set the window size to match the image.
ofSetWindowShape(shapesImg.getWidth(), shapesImg.getHeight());
}
void ofApp::draw()
{
// Draw the image as the background.
ofSetColor(255);
shapesImg.draw(0, 0);
int shapeWidth = shapesImg.getWidth();
int shapeHeight = shapesImg.getHeight();
int frameIndex = ofGetFrameNum() % (shapeWidth * shapeHeight);
int x = frameIndex % shapeWidth;
int y = frameIndex / shapeWidth;
// Get a reference to the image pixels.
ofPixels shapesPix = shapesImg.getPixels();
// Get the color value for the frame.
int pixelIndex = frameIndex * shapesPix.getNumChannels();
ofColor color = shapesPix.getColor(pixelIndex);
// Draw a rectangle at the sampled pixel position.
ofFill();
ofSetColor(color);
ofDrawRectangle(x - 25, y - 25, 50, 50);
// Add an outline so we can see the rectangle better.
ofNoFill();
ofSetColor(0);
ofDrawRectangle(x - 25, y - 25, 50, 50);
}
#include "ofApp.h"
void ofApp::setup()
{
// Load the shapes image.
shapesImg.load("shapes.png");
// Set the window size to match the image.
ofSetWindowShape(shapesImg.getWidth(), shapesImg.getHeight());
}
void ofApp::draw()
{
// Draw the image as the background.
ofSetColor(255);
shapesImg.draw(0, 0);
// Get a reference to the image pixels.
ofPixels shapesPix = shapesImg.getPixels();
// Get the color value under the mouse.
int index = (mouseY * shapesImg.getWidth() + mouseX) * shapesPix.getNumChannels();
ofColor color = shapesPix.getColor(index);
// Draw a rectangle under the mouse using the pixel color.
ofFill();
ofSetColor(color);
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50);
// Add an outline so we can see the rectangle better.
ofNoFill();
ofSetColor(0);
ofDrawRectangle(mouseX - 25, mouseY - 25, 50, 50);
}
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void draw();
ofImage shapesImg;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment