Questions about interfaces and PApplet

I’m just contemplating using interfaces to help with themes in my library. I’ve never used one before and am unsure what the best way to achieve this.

The main issue is that processing colors are implemented using the color() method, therefore I cannot just declare them in the interface but they do need to be initialized.

Should I just initialize them all as zero and just handle color assignment after, or is there a way to pass a PApplet to an interface. It seems like something that would be logically impossible with my understanding of java but again I’m aware there are many things I dont know so I could also be wrong.

Thanks

An Interface is not like a class because you can’t instantiate objects from rather you create classes that implement the interface. An interface may contain

  • constants (static final attributes)
  • default methods
  • abstract methods

it cannot contain fields (instance attributes)

You can use other classes such as PApplet if they are passed as parameters - bit like this

import processing.core.PApplet;

public interface MMyInterface {

	PApplet papp = null;
	int me = 45;
	
	default float argb(PApplet p, int a, int r, int g, int b) {
		return p.color(r,g,b,a);
	}
}

Can say much more because it is not clear what you are hoping to achieve.

1 Like

im looking to improve how my libraries handles themes. I have many classes which all have color variables for things such as border color fill color etc. Currently I have a function in each of these classes that handles color logic, and it seems inefficient to have to reuse code so much.

I just didnt know how to structure the interface so that it can make use of the PApplet. The classes are currently bound to PApplet as they need to make use of processing functions and the sketch window. And color absolutely requires it unless I’m using simple integer values.

The other problem, is that I’m currently using booleans to switch between color profiles and again I have to use these variables in all themed classes.

Thanks

The reason .color() is an instance method (not static) is to accommodate how the colormode can change during runtime (and therefore the change behaviour of the input arguments).

If you need a static RGB-only method, use these.

	/**
	 * @param red   ∈[0, 255]
	 * @param green ∈[0, 255]
	 * @param blue  ∈[0, 255]
	 * @param alpha ∈[0, 255] (where 0 is transparent; 255 is opaque)
	 * @return
	 */
	public static int composeColor(final int red, final int green, final int blue, final int alpha) {
		return alpha << 24 | red << 16 | green << 8 | blue;
	}

	/**
	 * @param red   ∈[0, 255]
	 * @param green ∈[0, 255]
	 * @param blue  ∈[0, 255]
	 * @return
	 */
	public static int composeColor(final int red, final int green, final int blue) {
		return -16777216 | red << 16 | green << 8 | blue;
	}
2 Likes

thanks for the info, I’m still not sure what scenario calls for interfaces. So far I am coding using only classes. In the end I created yet another class, and just stored all my variables this way. As the theme class is instanced by the main parent class I can then share it to required classes.

Again though after reading about interfaces, I immediately thought this would be an ideal use case to test it out, but I think theres something I dont quite understand.