We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › calling color() statically
Page Index Toggle Pages: 1
calling color() statically (Read 340 times)
calling color() statically
Apr 22nd, 2006, 3:47am
 
This is for programming processing in Java mode (i'm using eclipse), please ignore if you are programming it in processing mode.

---
This could be useful to artists who are used to photoshop color wheels/ranges.  Is it because of some design issues so we cannot call PApplet color() method statically?  I extracted the method and created a Color object, it contains two static methods to encode colors according to the photoshop color wheel ranges:

R 0-255
G 0-255
B 0-255
A 0-100

H 0-359
S 0-100
B 0-100
A 0-100

It would be nice if someone can add CMYK or LAB color modes as well, and possibly add method signatures for int inputs in addition to float inputs.

So you can now encode colors statically to an int from an non PApplet extended object, which is rather wasteful if you create thousands of node, vertices and inherit all methods from PApplet via extends.

Comments are welcome, maybe there's a static method I don't know about. Ben, feel free adding these static methods to the Processing codebase if you find it useful.

before:
Code:

myobject extends PApplet{
int mycolor;
myobject(){
mycolor = this.color(100,100,100,100);
}
}


after:
Code:

myobject{
int mycolor;
myobject(){
mycolor = Color.encodePhotoshopRGB(100,100,100,100);
}
}


the Color object:
Code:

public class Color {
public static int encodePhotoshopRGB(float x, float y, float z, float a){
/** Max value for red (or hue) set by colorMode */
float colorModeX = 255;
/** Max value for green (or saturation) set by colorMode */
float colorModeY = 255;
/** Max value for blue (or value) set by colorMode */
float colorModeZ = 255;
/** Max value for alpha set by colorMode */
float colorModeA = 100;

// internal color for setting/calculating
float calcR, calcG, calcB, calcA;
int calcRi, calcGi, calcBi, calcAi;
int calcColor;
/** True if colors are not in the range 0..1 */
boolean colorScale = true;

if(colorScale){
calcR = x / colorModeX;
calcG = y / colorModeY;
calcB = z / colorModeZ;
calcA = a / colorModeA;
}else{
calcR = x; calcG = y; calcB = z; calcA = a;
}

calcRi = (int)(255*calcR); calcGi = (int)(255*calcG);
calcBi = (int)(255*calcB); calcAi = (int)(255*calcA);
calcColor = (calcAi << 24) | (calcRi << 16) | (calcGi << 8) | calcBi;

return calcColor;
}
public static int encodePhotoshopHSB(float x, float y, float z, float a){
/** Max value for red (or hue) set by colorMode */
float colorModeX = 360;
/** Max value for green (or saturation) set by colorMode */
float colorModeY = 100;
/** Max value for blue (or value) set by colorMode */
float colorModeZ = 100;
/** Max value for alpha set by colorMode */
float colorModeA = 100;

// internal color for setting/calculating
float calcR = 0f, calcG = 0f, calcB = 0f, calcA = 0f;
int calcRi, calcGi, calcBi, calcAi;
int calcColor;
/** True if colors are not in the range 0..1 */
boolean colorScale = true;

x /= colorModeX; // h
y /= colorModeY; // s
z /= colorModeZ; // b
calcA = colorScale ? (a/colorModeA) : a;
if(y == 0){ // saturation == 0
calcR = z;
calcG = z;
calcB = z;
}else{
float which = (x - (int)x) * 6.0f;
float f = which - (int)which;
float p = z * (1.0f - y);
float q = z * (1.0f - y * f);
float t = z * (1.0f - (y * (1.0f - f)));
switch ((int)which) {
case 0: calcR = z; calcG = t; calcB = p; break;
case 1: calcR = q; calcG = z; calcB = p; break;
case 2: calcR = p; calcG = z; calcB = t; break;
case 3: calcR = p; calcG = q; calcB = z; break;
case 4: calcR = t; calcG = p; calcB = z; break;
case 5: calcR = z; calcG = p; calcB = q; break;
}
}

calcRi = (int)(255*calcR); calcGi = (int)(255*calcG);
calcBi = (int)(255*calcB); calcAi = (int)(255*calcA);
calcColor = (calcAi << 24) | (calcRi << 16) | (calcGi << 8) | calcBi;

return calcColor;
}
}
Page Index Toggle Pages: 1