# Using reflection to determine color datatype versus int

**URL:** https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283
**Category:** Beginners
**Created:** [April 11, 2021, 7:38pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283 "2021-04-11T19:38:21Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Aleksi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/aleksi/32/11065_2.png) [@Aleksi](https://discourse.processing.org/u/Aleksi)
#### Post date: [April 11, 2021, 7:38pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/1 "2021-04-11T19:38:21Z")

</div>

Hello again!

Is there a way, using reflection, to determine if a given class field is declared as a `color` field as distinct from an `int` field?

I figure there must be a way, but the following returns both fields as `int`:

```auto
import java.lang.reflect.*;

void setup(){
  A a = new A();
  Test(a);
}

class A {
  int i = 1;
  color c = #ffff00;
}

<T> void Test(T obj){
  Class someClass = obj.getClass();
  Field[] allFields = someClass.getDeclaredFields();
  
  for (Field field : allFields){
    Class type = field.getType();
    
    println("\nfield:",field,
            "\n\tfield.getName:",field.getName(),
            "\n\ttype:",type,
            "\n\tgetName:",type.getName(),
            "\n\tgetCanonicalName:",type.getCanonicalName(),
            "\n\tgetSimpleName:",type.getSimpleName(),
            "\n\tgetTypeName:",type.getTypeName(),
            "\n\ttoGenericString",type.toGenericString(),
            "\n\ttoString:",type.toString());
  }
}

```

Thank you!

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 11, 2021, 8:25pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/2 "2021-04-11T20:25:47Z")

</div>

Hello Aleksi,

I don’t know if you want to do is doable. The reason is that the `color` datatype is actually a simple `int` type.

When you have a look at the source code for the `color()` function, you can see it actually return an `int` not a `color` datatype:

> <https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L10364>

---

<div class="post-metadata">

### Author: ![Aleksi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/aleksi/32/11065_2.png) [@Aleksi](https://discourse.processing.org/u/Aleksi)
#### Post date: [April 11, 2021, 8:49pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/3 "2021-04-11T20:49:47Z")

</div>

Hi jb4x, thanks for the reply.

Yes, I knew that already. Hence my post: I was hoping someone knew some magical way to differentiate between the two.

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [April 11, 2021, 9:50pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/4 "2021-04-11T21:50:55Z")

</div>

I guess it really depends on what you’re trying to do. Again, int and color are the same thing and there’s no way to distinguish or I would say there’s no sense of distinction. But for example if you are using only opaque colors, you can test if the top 8 bits are 1 or not because it’s unlikely that they are all 1 when you use integer (it’s a negative number) - but of course, it may not happen when you count apples in real life, but if you count generative apples it might happen. There’s also a funny trick that `background(-1);` makes white background 🙂  
[https://processing.org/reference/color\_datatype.html](https://processing.org/reference/color_datatype.html)

---

<div class="post-metadata">

### Author: ![Aleksi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/aleksi/32/11065_2.png) [@Aleksi](https://discourse.processing.org/u/Aleksi)
#### Post date: [April 11, 2021, 10:42pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/5 "2021-04-11T22:42:47Z")

</div>

Hi micaut, thanks for the reply.

What I’m attempting to do is genericize json parsing, and I’m mostly there. In my classes I use int fields for integers, and color fields for colors. I like being able to distinguish them in such a way.

It would have been nice and convenient if I could ascertain both fields from their types, or even by somehow otherwise parsing the strings used to specify the type, but I guess it’s not to be.

Fortunately there are a variety of workarounds I can implement. I was just hoping I wouldn’t have to jump through such hoops; but it appears I must.

Oh well…

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [April 12, 2021, 2:45pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/6 "2021-04-12T14:45:41Z")

</div>

The `color` dataype is _syntactic sugar_, existing only in the Processing editor (.pde files). As has been mentioned already, it’s simply an alias for `int`.

When a sketch is run from the Processing editor, .pde files are converted to .java/.class files – during this conversion any uses of `color` are replaced with `int` by the Processing _preprocessor_.

So during runtime `int` is indeed the type of these variables, just as you’ve found via reflection.

The only way to differentiate between the two (if you _really_ want to, not that you need to, since both can be treated as `int`) is to **look at the .pde source file**.

---

<div class="post-metadata">

### Author: ![Aleksi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/aleksi/32/11065_2.png) [@Aleksi](https://discourse.processing.org/u/Aleksi)
#### Post date: [April 12, 2021, 2:48pm UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/7 "2021-04-12T14:48:25Z")

</div>

Thank you for that micycle. Most helpful.

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [April 13, 2021, 7:06am UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/8 "2021-04-13T07:06:24Z")

</div>

I checked how (pg.)background determines the value from int and color, and it does check the higher bits as I suggested

> <https://github.com/processing/processing/blob/master/core/src/processing/core/PGraphics.java#L7644>

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [April 13, 2021, 7:51am UTC](https://discourse.processing.org/t/using-reflection-to-determine-color-datatype-versus-int/29283/9 "2021-04-13T07:51:13Z")

</div>

> [@micuat](#):
>
> it does check the higher bits as I suggested

I’d be careful of your assumption there! The check for higher bits is to determine whether the calculations need to take account of alpha values. A fully opaque colour can (probably will) have the higher bits as zero. In fact, that’s the exact issue mentioned about transparent black - the color “type” really is a horrible and buggy hack!
