can we add:
ofColor color;
ofSetColor(color);
can we add:
ofColor color;
ofSetColor(color);
I just wrote this for a project I’m working on. It’s backwards compatible with ofColor, but is a lot more powerful:
Static colorMode and colorRange to which everything is sensitive
Create as (float, float, float) or unsigned int/hex representation and export color asHex() in an unsigned int/hex representation (unlike Processing, either RGB or HSB)
red, green, blue, hue, saturation, brightness all stored publicly, allowing you to access and update them directly, and then call update() to sync the other model (or sync a specific model by passing an argument to update())
Distance between two colors in the current color model, or a specified model
<, ==, and > have been overridden to allow sorting functions to sort ofxColors about a single dimension specified by ofxColor::setColorSortMode()
ofxColor.ofSetColor(), as per Todd’s request
If someone wants to add blending modes or other color models, that’d be awesome.
Here’s an overview:
#define OF_COLOR_RGB 0
#define OF_COLOR_HSV 1
#define OF_COLOR_SORT_RED 0
#define OF_COLOR_SORT_GREEN 1
#define OF_COLOR_SORT_BLUE 2
#define OF_COLOR_SORT_HUE 3
#define OF_COLOR_SORT_SATURATION 4
#define OF_COLOR_SORT_VALUE 5
#define OF_COLOR_SORT_ALPHA 6
class ofxColor {
public:
static void setColorMode(int colorMode);
static int getColorMode();
static void setColorRange(float colorRange);
static float getColorRange();
static void setColorSortMode(int colorSortMode);
static int getColorSortMode();
float
alpha,
red, green, blue,
hue, saturation, value;
ofxColor();
ofxColor(const ofxColor& x);
ofxColor(unsigned int asHex);
ofxColor(float x, float y, float z);
ofxColor& operator=(const ofxColor& x);
ofxColor operator+(const ofxColor& x) const;
int operator==(const ofxColor& x) const;
bool operator<(const ofxColor& x) const;
bool operator>(const ofxColor& x) const;
float distance(const ofxColor& x, int colorMode = ofxColor::colorMode) const;
unsigned int asHex(int colorMode = ofxColor::colorMode) const;
void update();
void clear();
void updateRgb();
void updateHsv();
void setColor() const;
virtual ~ofxColor();
private:
static int colorMode;
static float colorRange;
static int colorSortMode;
};
void ofSetColor(const ofxColor& color);
I just uploaded a new version that uses the name “HSV” instead of “HSB”. Though HSB is used in Processing, HSV is used many other places (like OpenCV).
I also fixed some consts that didn’t quite make sense and added the global function:
void ofSetColor(const ofxColor& color);
could you post an example of how to use your class? thanks!
Sure, download r2 first (just posted above). There was a bug in how colors were drawn when you called ofSetColor(color) if you had some non-255 color range.
Here’s a demo that displays some randomly generated + sorted colors:
#pragma once
#include "ofMain.h"
#include "ofxColor.h"
class testApp : public ofBaseApp {
public:
static const int
xSize = 4,
ySize = 4;
int cols, rows;
vector<ofxColor> colors;
testApp() {}
void setup() {
cols = ofGetWidth() / xSize;
rows = ofGetHeight() / ySize;
ofxColor::setColorRange(1);
ofxColor::setColorSortMode(OF_COLOR_SORT_VALUE);
}
void update() {
colors.clear();
// make a list of random colors
for(int y = 0; y < rows; y++) {
for(int x = 0; x < cols; x++) {
colors.push_back(
ofxColor(
ofRandomuf(),
ofRandomuf(),
ofRandomuf()));
}
}
// sort the colors by value
std::sort(colors.begin(), colors.end());
}
void draw() {
int i = 0;
for(int y = 0; y < rows; y++) {
for(int x = 0; x < cols; x++) {
ofSetColor(colors[i++]);
ofRect(x * xSize, y * ySize, xSize, ySize);
}
}
}
};
thanks! thats very helpful!