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.
IndexProcessing DevelopmentLibraries,  Tool Development › Use of color(r,g,b) in a library (fails)
Page Index Toggle Pages: 1
Use of color(r,g,b) in a library (fails) (Read 1087 times)
Use of color(r,g,b) in a library (fails)
Aug 10th, 2009, 2:15pm
 
Hi, I've just started using Processing and am using it for basic data graphing and representation.  I'm to the point where I'm tired of rewriting or copy/pasting/altering the same basic algorithms over and over, so I've started delving into library creation.

As a simple test, I did created the java class below.  The original was much more complicated (and actually did something of use), but I've slimmed this down for simplicity.

The problem I'm seeing is that regardless of what values I use for red, green and blue, I always get the value 0 back in my sketch, where I call this class' method.  Is there something I'm doing wrong here?

Here's the class:

import processing.core.PGraphics;

public class SimpleTest extends PGraphics {
     
     public SimpleTest() {}
     
     public int getColor(int red, int green, int blue) {
           return color(red,green,blue);
     }

}



Here's a Processing sketch that illustrates the problem.  This also compares the external class use vs. an internal method use.  If the above was working properly, there should be two sets of rectangles same-colored side-by-side.


import simpleGraph.*;

void setup() {
   
 size(140,250);
 background(0);
 noLoop();
 
}

color localColor(int r, int g, int b) {
  return color(r,g,b);
}


void draw() {
 
 color cl = color(0,0,0);
 
 
 // Using external class
 
 SimpleTest st = new SimpleTest();
 
 cl = st.getColor(255,255,255);
 println("color 1: " + cl);
 fill(cl);
 rect(10,10,50,50);
 
 cl = st.getColor(0,255,0);
 println("color 2: " + cl);
 fill(cl);
 rect(10,70,50,50);
 
 cl = st.getColor(255,0,0);
 println("color 3: " + cl);
 fill(cl);
 rect(10,130,50,50);
 
 cl = st.getColor(0,0,255);
 println("color 4: " + cl);
 fill(cl);
 rect(10,190,50,50);
 
 // using local method

 cl = localColor(255,255,255);
 println("color 5: " + cl);
 fill(cl);
 rect(70,10,50,50);
 
 cl = localColor(0,255,0);
 println("color 6: " + cl);
 fill(cl);
 rect(70,70,50,50);
 
 cl = localColor(255,0,0);
 println("color 7: " + cl);
 fill(cl);
 rect(70,130,50,50);
 
 cl = localColor(0,0,255);
 println("color 8: " + cl);
 fill(cl);
 rect(70,190,50,50);
 
 
 
}



Re: Use of color(r,g,b) in a library (fails)
Reply #1 - Aug 16th, 2009, 3:26am
 
color() is defined in PApplet, not in PGraphics. Use the instance of PApplet to call it.
Page Index Toggle Pages: 1