# What does "Error on int" mean?

**URL:** <https://discourse.processing.org/t/what-does-error-on-int-mean/42264>\
**Category:** Beginners\
**Created:** [June 21, 2023, 7:46pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264 "2023-06-21T19:46:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Random\_Ghost](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/random_ghost/32/16609_2.png) [@Random\_Ghost](https://discourse.processing.org/u/Random_Ghost)\
**Post date:** [June 21, 2023, 7:46pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/1 "2023-06-21T19:46:31Z")

</div>

''Syntax error on token “int”, invalid ClassType  
‘’  
I have a static class C3 and I’m trying to create a color method in it. Here’s the code,

```auto
static class C3 {
  int r, g, b;
  /*
  public C3(color c){
    r = c;
    g = c;
    b = c;
  }
  */
  public C3(int r, int g, int b){
    this.r = r;
    this.g = g;
    this.b = b;
  }

  public C3 add(C3 o){
    return new C3(r + o.r, g + o.g, b + o.b);
  }

  public int clamp(int c){
    return Math.max(0, Math.min(255, c));
  }

  public int diff(C3 o){
    int Rdiff = o.r - r;
    int Gdiff = o.g - g;
    int Bdiff = o.b - b;
    int distanceSquared = Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff;
    return distanceSquared;
  }

  public C3 mul(double d){
    return new C3((int) (d * r), (int) (d * g), (int) (d * b));
  }

  public C3 sub(C3 o){
    return new C3(r - o.r, g - o.g, b - o.b);
  }

  public color toColor(){
    return new color(clamp(r), clamp(g), clamp(b));
  }

  public int toRGB(){
    return toColor().getRGB();
  }
}

private static C3 findClosestPaletteColor(C3 c, C3[] palette){
  C3 closest = palette[0];

  for (C3 n : palette) {
    if (n.diff(c) < closest.diff(c)) {
      closest = n;
    }
  }

  return closest;
}

```

The error is in the toColor method.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 21, 2023, 8:15pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/2 "2023-06-21T20:15:29Z")

</div>

> [@Random\_Ghost](#):
>
> ```auto
> public color toColor(){
> return new color(clamp(r), clamp(g), clamp(b));
> }
> 
> ```

> [@Random\_Ghost](#):
>
> ''Syntax error on token “int”, invalid ClassType"

In Processing, keyword `color` is alias to Java’s keyword `int`:

> **[color / Reference](https://processing.org/reference/color_datatype.html)**
>
> Datatype for storing color values. Colors may be assigned with get() and color() or they may be specified directly using hexadecimal notation such as #FFCC00 or 0xFFFFCC00.…

> **[int / Reference](https://processing.org/reference/int.html)**
>
> Datatype for integers, numbers without a decimal point. Integers can be as large as 2,147,483,647 and as low as -2,147,483,648. They are stored as 32 bits of information. The first time a variable is …

Keywords aren’t classes in Java; and therefore we can’t use operator `new` on them:

> **[new / Reference](https://processing.org/reference/new.html)**
>
> Creates a "new" object. The keyword new is typically used similarly to the applications in the above example. In this example, a new object "h1" of the datatype "HLine" is created. On the follo…

Besides `color` being a keyword, it’s also a function in Processing:

> **[color() / Reference](https://processing.org/reference/color_.html)**
>
> Creates colors for storing in variables of the color datatype. The parameters are interpreted as RGB or HSB values depending on the current colorMode(). The default mode is RGB values fr…

```auto
  public color toColor(){
    return color(clamp(r), clamp(g), clamp(b));
  }

```

---

<div class="post-metadata">

**Author:** ![Random\_Ghost](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/random_ghost/32/16609_2.png) [@Random\_Ghost](https://discourse.processing.org/u/Random_Ghost)\
**Post date:** [June 21, 2023, 8:30pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/3 "2023-06-21T20:30:07Z")

</div>

But I put the new there as I cannot make a static reference to the non-static method. Is there any way to implement this?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 21, 2023, 8:35pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/4 "2023-06-21T20:35:44Z")

</div>

I didn’t pay attention you had a `static class`: 🙈

> [@Random\_Ghost](#):
>
> `static class C3 {`

You can simply get rid of keyword `static` or request a PApplet parameter from your **toColor()** method:

```auto
  public color toColor(final PApplet p) {
    return p.color(clamp(r), clamp(g), clamp(b));
  }

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 21, 2023, 10:16pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/6 "2023-06-21T22:16:15Z")

</div>

My previous answer doesn’t work! At least on PDE v3.5.4.

Due to the dual-nature of `color`, being both a keyword & a method, I can’t invoke it using the dot `.` operator: ~~`p.color(r, g, b);`~~

Maybe on Processing 4 they’ve fixed that; and that’s why you’ve marked my post as the “solution”. 😃

Anyways, I’ve crafted a “hack” to directly access method **color()** by extending class PGraphics and creating a method **getColor()**, which then invokes **color()**:

```auto
static final class PGraph extends PGraphics {
  PGraph() {
    checkSettings();
  }

  color getColor(color r, color g, color b) {
    return color(r, g, b);
  }
}

```

After that, I’ve refactored your class C3 to rely on subclass PGraph:

```auto
static class C3 {
  static final PGraph pg = new PGraph();

  color r, g, b;

  C3(color x, color y, color z) {
    r = x;
    g = y;
    b = z;
  }

  C3 add(final C3 c) {
    return new C3(r + c.r, g + c.g, b + c.b);
  }

  C3 sub(final C3 c) {
    return new C3(r - c.r, g - c.g, b - c.b);
  }

  C3 mul(final int d) {
    return new C3(d * r, d * g, d * b);
  }

  C3 mul(final float d) {
    return new C3((int) (d * r), (int) (d * g), (int) (d * b));
  }

  C3 clamp() {
    r = constrain(r, 0, 255);
    g = constrain(g, 0, 255);
    b = constrain(b, 0, 255);

    return this;
  }

  color toColor() {
    return clamp().pg.getColor(r, g, b);
  }

  int diff(final C3 c) {
    final color
      rDiff = c.r - r, 
      gDiff = c.g - g, 
      bDiff = c.b - b;

    return rDiff * rDiff + gDiff * gDiff + bDiff * bDiff;
  }

  static final C3 findClosestPaletteColor(final C3 c, final C3... palette) {
    C3 closest = null;
    int dif = MIN_INT;

    for (final C3 n : palette) if (n.diff(c) < dif) dif = (closest = n).diff(c);

    return closest;
  }

  String toString() {
    return "[" + r + ", " + g + ", " + b + "]";
  }
}

```

---

<div class="post-metadata">

**Author:** ![Random\_Ghost](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/random_ghost/32/16609_2.png) [@Random\_Ghost](https://discourse.processing.org/u/Random_Ghost)\
**Post date:** [June 21, 2023, 10:42pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/7 "2023-06-21T22:42:49Z")

</div>

I just removed the static keyword. I’m not really a programmer so I do not really understand what they mean. I was trying to make some image processing algorithms. They work now though, and I guess that would do. I’d try implementing your solution though.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 21, 2023, 11:16pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/8 "2023-06-21T23:16:15Z")

</div>

> [@Random\_Ghost](#):
>
> I’m not really a programmer so I do not really understand what they mean.

Keyword `static` is for declaring shared members of a class which can be accessed w/o instantiating their class:

> **[static / Reference](https://processing.org/reference/static.html)**
>
> Keyword used to define a variable as a "class variable" and a method as a "class method." When a variable is declared with the static keyword, all instances of that class share the same variabl…

I believe you did so b/c you wanted your helper method **findClosestPaletteColor()** not to belong to a particular instance of C3.

If you prefer, you can create a separate `static class` only for `static` members.

Then create a non-`static` subclass which `extends` that `static class`:

```auto
static final class PGraph extends PGraphics {
  PGraph() {
    checkSettings();
  }

  color getColor(color r, color g, color b) {
    return color(r, g, b);
  }
}

static abstract class C3Base {
  static final PGraph pg = new PGraph();

  static final C3 findClosestPaletteColor(final C3 c, final C3... palette) {
    C3 closest = null;
    int dif = MAX_INT;

    for (final C3 n : palette) if (n.diff(c) < dif) dif = (closest = n).diff(c);

    return closest;
  }
}

class C3 extends C3Base {
  color r, g, b;

  C3(color x, color y, color z) {
    r = x;
    g = y;
    b = z;
  }

  C3 add(final C3 c) {
    return new C3(r + c.r, g + c.g, b + c.b);
  }

  C3 sub(final C3 c) {
    return new C3(r - c.r, g - c.g, b - c.b);
  }

  C3 mul(final int d) {
    return new C3(d * r, d * g, d * b);
  }

  C3 mul(final float d) {
    return new C3((int) (d * r), (int) (d * g), (int) (d * b));
  }

  C3 clamp() {
    r = constrain(r, 0, 255);
    g = constrain(g, 0, 255);
    b = constrain(b, 0, 255);

    return this;
  }

  color toColor() {
    return clamp().pg.getColor(r, g, b);
  }

  int diff(final C3 c) {
    final color
      rDiff = c.r - r, 
      gDiff = c.g - g, 
      bDiff = c.b - b;

    return rDiff * rDiff + gDiff * gDiff + bDiff * bDiff;
  }

  String toString() {
    return "[" + r + ", " + g + ", " + b + "]";
  }
}

```

Now this split `class C3 extends C3Base` is equivalent to previous unified `static class C3`.

> Edit: Added keyword `abstract` to `static class C3` so folks won’t inadvertently instantiate it instead of C3. 😉

> Edit2: wrong MIN\_MAX to correct MAX\_INT for variable _dif_!

---

<div class="post-metadata">

**Author:** ![Random\_Ghost](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/random_ghost/32/16609_2.png) [@Random\_Ghost](https://discourse.processing.org/u/Random_Ghost)\
**Post date:** [June 24, 2023, 11:50am UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/9 "2023-06-24T11:50:03Z")

</div>

Thanks. It does work. I made findClosestPaletteColor a C3 method since there’s not point to it not being one. What’s left is making a PImage inheritance for my other class.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 24, 2023, 12:34pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/10 "2023-06-24T12:34:04Z")

</div>

> [@Random\_Ghost](#):
>
> I made **findClosestPaletteColor()** a C3 method since there’s no point to it not being one.

Almost any `static` method can be converted to an instance 1.  
It’s a matter of how we prefer to organize the logic of our code.

Your method **findClosestPaletteColor()** is about iterating all C3 instances; and therefore it doesn’t represent a single C3 instance.

That’s why it fits as a `static` method. Even though we can just leave it as non-`static`.

As an instance method **findClosestPaletteColor()**, you’d need to invoke it as something weird like this:  
`palette[0].findClosestPaletteColor(palette[5], palette);`

While as `static` it makes more sense:  
`C3.findClosestPaletteColor(palette[5], palette);`

I’ve already left for you the “trick” example post about creating a parent class dedicated for `static` members, where your regular class w/ instance members can extend from.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 24, 2023, 12:37pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/11 "2023-06-24T12:37:43Z")

</div>

> [@Random\_Ghost](#):
>
> What’s left is making a PImage inheritance for my other class.

Or you can just have a PImage instance field and request it from your class’ constructor.

---

<div class="post-metadata">

**Author:** ![Random\_Ghost](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/random_ghost/32/16609_2.png) [@Random\_Ghost](https://discourse.processing.org/u/Random_Ghost)\
**Post date:** [June 24, 2023, 4:12pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/12 "2023-06-24T16:12:58Z")

</div>

Here’s the new class:

```auto
// These methods are faster to calculate. Explanations are on the Processing website

int r(color c){
  return c >> 16 & 0xFF;
}  

int g(color c){
  return c >> 8 & 0xFF;
}  

int b(color c){
  return c & 0xFF;
}  

static final class PGraph extends PGraphics {
  PGraph() {
    checkSettings();
  }

  color getColor(color r, color g, color b) {
    return color(r, g, b);
  }
}

static abstract class C3Base {
  static final PGraph pg = new PGraph();
}

class C3 extends C3Base {
  color r, g, b;

  C3(color c) {
    r = r(c);
    g = g(c);
    b = b(c);
  }
  
  C3(int r, int g, int b) {
    this.r = r;
    this.g = g;
    this.b = b;
  }

  C3 add(final C3 c) {
    return new C3(r + c.r, g + c.g, b + c.b);
  }

  C3 sub(final C3 c) {
    return new C3(r - c.r, g - c.g, b - c.b);
  }

  C3 mul(final int d) {
    return new C3(d * r, d * g, d * b);
  }

  C3 mul(final float d) {
    return new C3((int) (d * r), (int) (d * g), (int) (d * b));
  }

  C3 clamp() {
    r = constrain(r, 0, 255);
    g = constrain(g, 0, 255);
    b = constrain(b, 0, 255);

    return this;
  }

  color toColor() {
    return clamp().pg.getColor(r, g, b);
  }

  int diff(final C3 c) {
    final color
      rDiff = c.r - r, 
      gDiff = c.g - g, 
      bDiff = c.b - b;

    return rDiff * rDiff + gDiff * gDiff + bDiff * bDiff;
  }

  String toString() {
    return "[" + r + ", " + g + ", " + b + "]";
  }
  
  C3 findClosestPaletteColor(C3[] palette) {
    C3 closest = palette[0];
    C3 c = new C3(r, g, b);

    for (C3 n : palette) {
      if (n.diff(c) < closest.diff(c)) {
        closest = n;
      }
    }

    return closest;
  }
}

```

I am trying to add some methods to the PImage class. One way I can try is extending the class, currently the only method I know of. There’s some dithering methods, and a couple of filters.  
Do you have any recommendations?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 24, 2023, 6:11pm UTC](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/13 "2023-06-24T18:11:43Z")

</div>

> [@Random\_Ghost](#):
>
> `C3 findClosestPaletteColor(C3[] palette) {`

Now I understand why you wanted **findClosestPaletteColor()** to be a regular instance method: You’ve got rid of its 1st parameter!

Instead the previous parameter became a clone of the current C3 instance.

Just so you know, you can keep both `static` & non-`static` versions for **findClosestPaletteColor()**.

And those new **r()**, **g()** & **b()** helper functions can be converted to `static` inside C3Base:

```auto
static abstract class C3Base {
  static final color r(final color c) {
    return c >> 020 & 0xff;
  }

  static final color g(final color c) {
    return c >> 010 & 0xff;
  }

  static final color b(final color c) {
    return c & 0xff;
  }

  static final C3 findClosestPaletteColor(final C3 c, final C3[] palette) {
    C3 closest = null;
    int dif = MAX_INT;

    for (final C3 n : palette) if (n.diff(c) < dif) dif = (closest = n).diff(c);

    return closest;
  }
}

```

Notice now that I’ve got rid of class PGraph & its corresponding `static` _pg_ field in C3Base.

In its place I’m using this formula: `b | g << 010 | r << 020 | PImage.ALPHA_MASK`.

Here’s latest version for class C3 w/ even more cool features: 🧙

```auto
class C3 extends C3Base implements Comparable<C3>, Cloneable {
  color r, g, b;

  C3(final color c) {
    r = r(c);
    g = g(c);
    b = b(c);
  }

  C3(final color r, final color g, final color b) {
    set(r, g, b);
  }

  C3 set(final color x, final color y, final color z) {
    r = x;
    g = y;
    b = z;

    return clamp();
  }

  C3 add(final C3 c) {
    return new C3(r + c.r, g + c.g, b + c.b);
  }

  C3 sub(final C3 c) {
    return new C3(r - c.r, g - c.g, b - c.b);
  }

  C3 mul(final int d) {
    return new C3(d * r, d * g, d * b);
  }

  C3 mul(final float d) {
    return new C3((int) (d * r), (int) (d * g), (int) (d * b));
  }

  int diff(final C3 c) {
    final color
      rDiff = c.r - r, 
      gDiff = c.g - g, 
      bDiff = c.b - b;

    return rDiff * rDiff + gDiff * gDiff + bDiff * bDiff;
  }

  final C3 findClosestPaletteColor(final C3... palette) {
    return findClosestPaletteColor(this, palette);
  }

  C3 clone() {
    try {
      return (C3) super.clone();
    }
    catch (final CloneNotSupportedException e) {
      throw new RuntimeException(e);
    }
  }

  C3 clamp() {
    r = constrain(r, 0, 255);
    g = constrain(g, 0, 255);
    b = constrain(b, 0, 255);

    return this;
  }

  color toColor() {
    return clamp().hashCode();
  }

  int hashCode() {
    return b | g << 010 | r << 020 | PImage.ALPHA_MASK;
  }

  boolean equals(final Object o) {
    return o instanceof C3? o.hashCode() == hashCode() : false;
  }

  int compareTo(final C3 c) {
    return
      (hashCode() & ~PImage.ALPHA_MASK) -
      (c.hashCode() & ~PImage.ALPHA_MASK);
  }

  String toString() {
    return '#' + hex(hashCode(), 6) + ": [" + r + ", " + g + ", " + b + "]";
  }
}

```

* * *

> Edit 1: changed: `return C3.findClosestPaletteColor(clone(), palette);`  
> to just: `return C3.findClosestPaletteColor(this, palette);`

> No need to use **clone()** for this operation, given neither passed object arguments are mutated in anyway inside `static` method **findClosestPaletteColor()**.

* * *

> Edit 2: Created a **set()** method and changed 2nd constructor to use that:

```auto
  C3(final color r, final color g, final color b) {
    set(r, g, b);
  }

  C3 set(final color x, final color y, final color z) {
    r = x;
    g = y;
    b = z;

    return clamp();
  }

```

* * *

Edit 3: No need to prefix w/ C3 at `return C3.findClosestPaletteColor(this, palette);`:  
`return findClosestPaletteColor(this, palette);`

* * *
