# Class for colours

**URL:** https://discourse.processing.org/t/class-for-colours/42169
**Category:** Beginners
**Created:** [June 9, 2023, 11:56am UTC](https://discourse.processing.org/t/class-for-colours/42169 "2023-06-09T11:56:27Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [June 9, 2023, 11:56am UTC](https://discourse.processing.org/t/class-for-colours/42169/1 "2023-06-09T11:56:27Z")

</div>

Good day

I’m trying to create a class that contains the colours for my application. E.g.

```auto
class Colours
{
  color background = color (0xB0, 0xB0, 0xB0);
  color text = color( 0x00, 0x00, 0x00);
}

```

And I would like to use it without having an instance of the class; e.g.

```auto
background(Colours.background);

```

Trying to use _static_ somewhere in or around the class however fails.

1. Making the class static gives the error (I would expect this to work).

```auto
Cannot make a static reference to the non-static field Colours.background

```

1. Making the field _background_ static gives the error

```auto
Cannot make a static reference to the non-static method color(int, int, int) from the type PApplet

```

Maybe I’m overthinking it and I should just use an instance but I still would like to know if it can be achieved and if yes, how.

Note that, although I consider myself a reasonably seasoned programmer, I have nearly my whole life skipped OOP and only know the very basics. I’m also very new to Processing, I’ve mostly worked in C and C#.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2023, 1:29pm UTC](https://discourse.processing.org/t/class-for-colours/42169/2 "2023-06-09T13:29:33Z")

</div>

Don’t know if it will help you, but found this reference: [https://stackoverflow.com/questions/28798152/create-a-struct-in-java-like-c](https://stackoverflow.com/questions/28798152/create-a-struct-in-java-like-c) . Java doesn’t have structs like C does and the class is their substitute. You would have to create an instance of the class, which is pretty simple to do; not sure why you would want to avoid doing that.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 9, 2023, 1:44pm UTC](https://discourse.processing.org/t/class-for-colours/42169/3 "2023-06-09T13:44:43Z")

</div>

this works

```auto

class Colours
{
  final static color background = 3233;
}

void setup() {
  background(Colours.background);
}

```

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2023, 2:41pm UTC](https://discourse.processing.org/t/class-for-colours/42169/4 "2023-06-09T14:41:39Z")

</div>

What’s the range for the color numbers; I’ve never seen it done that way?

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [June 9, 2023, 2:53pm UTC](https://discourse.processing.org/t/class-for-colours/42169/5 "2023-06-09T14:53:24Z")

</div>

> [@svan](#):
>
> You would have to create an instance of the class

Which I exactly want to prevent 😉

> [@svan](#):
>
> What’s the range for the color numbers; I’ve never seen it done that way?

Any mix of red, green and blue.

> [@Chrisir](#):
>
> this works

I’m not sure if 3233 matches the colour that I described (I have some doubts as the number 3233 is relatively small), I will test it later when the power is back. It might be a bit messed up if I have to convert but I hope it can be done with some bit shifting in a function.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2023, 2:58pm UTC](https://discourse.processing.org/t/class-for-colours/42169/6 "2023-06-09T14:58:03Z")

</div>

I’m up this far and its still going: final static color background = 2100000000;

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 9, 2023, 3:42pm UTC](https://discourse.processing.org/t/class-for-colours/42169/7 "2023-06-09T15:42:44Z")

</div>

3233

I made this number up, it’s not your color

But color in processing is just int.

There is probably a tutorial about this

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2023, 3:49pm UTC](https://discourse.processing.org/t/class-for-colours/42169/8 "2023-06-09T15:49:28Z")

</div>

final static color background = #B0B0B0; should be what he wanted.

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [June 9, 2023, 4:53pm UTC](https://discourse.processing.org/t/class-for-colours/42169/9 "2023-06-09T16:53:24Z")

</div>

I initially marked @Chrisir’s reply #3 as solution but there seems to be a problem that it does not quite work (the background is OK now but other colours not so ☹). More details tomorrow, need to make a small example if I can’t get it solved.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2023, 5:41pm UTC](https://discourse.processing.org/t/class-for-colours/42169/10 "2023-06-09T17:41:50Z")

</div>

The background and text colors work ok on a Mac:

```auto
class Colors {
  final static color background = #B0B0B0;
  final static color txtColor = #DE0707;
}

void setup() {
  size(300, 200);
  background(Colors.background);
  fill(Colors.txtColor);
  textSize(24);
  text("Jeremiah was a Bullfrog.", 20, 60);
}

```

**Output:**  
 ![output](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/f/9fa6b75599ebb0ebf772537c20ba0ae27b087d90.png)

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [June 9, 2023, 6:52pm UTC](https://discourse.processing.org/t/class-for-colours/42169/11 "2023-06-09T18:52:05Z")

</div>

It did not work for me. But I need to play more tomorrow to see where I went wrong

Thanks

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2023, 6:53pm UTC](https://discourse.processing.org/t/class-for-colours/42169/12 "2023-06-09T18:53:49Z")

</div>

Are you using a Windows based system; shouldn’t make any difference but I’m just curious.

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [June 9, 2023, 7:08pm UTC](https://discourse.processing.org/t/class-for-colours/42169/13 "2023-06-09T19:08:19Z")

</div>

Yes, I’m using Win10.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2023, 7:27pm UTC](https://discourse.processing.org/t/class-for-colours/42169/14 "2023-06-09T19:27:40Z")

</div>

I tried it on Windows11 and it works ok there.

---

<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 10, 2023, 12:17am UTC](https://discourse.processing.org/t/class-for-colours/42169/15 "2023-06-10T00:17:19Z")

</div>

Besides `class`, we can also go w/ `interface` or even `enum`:

```auto
// https://Discourse.Processing.org/t/class-for-colours/42169/15
// 2023-Jun-09

void setup() {
  size(300, 200);
  noLoop();

  textAlign(CENTER, CENTER);
  textSize(PaletteEnum.SIZE.v);
  fill(PaletteClass.TXT);
}

void draw() {
  background(PaletteInterface.BG);
  text("Red Palette", width >> 1, height >> 1);
}

class PaletteClass {
  static final int
    BG = 0xD0, 
    TXT = #DE2507, 
    SIZE = 030;
}

interface PaletteInterface {
  int
    BG = 0xD0, 
    TXT = #DE0707, 
    SIZE = 030;
}

enum PaletteEnum {
  BG(0xD0), TXT(#DE0707), SIZE(030);

  final int v;

  PaletteEnum(color val) {
    v = val;
  }
}

```

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [June 10, 2023, 5:15am UTC](https://discourse.processing.org/t/class-for-colours/42169/16 "2023-06-10T05:15:51Z")

</div>

Minimal example

```auto
final static class Colours
{
  final static color background = 0xB0B0B0; // canvas background
  final static color grid = 0x808080; // grid
  final static color stateFill = 0xFFFFFF; // fill colour for state (circle)
  final static color stateCircle = 0x0000FF; // stroke colour for state circle
  final static color stateRect = 0xE0E0E0; // stroke colour for state rectangle (visible when mouse is over)
  final static color stateText = 0x00000; // text color in state
  final static color statePoint = 0x00FF80; // fill colour for connector points
  final static color statePointIn = 0xFF0000; // fill colour for connector points that acts as an input to the state
  final static color statePointOut = 0xFF0000; // fill colour for connector points that acts as an output from the state
}

void setup()
{
  size(1200, 800);

  background(Colours.background); // ok, background in desired colour

  // white
  fill(color(0xFF, 0xFF, 0xFF)); // ok, circle filled with white
  //fill(0xFFFFFF); // does not fill the circle with white
  //fill(Colours.stateFill); // does not fill the circle with white

  // black
  //stroke(color(0x00, 0x00, 0xFF)); // ok, blue around the circle
  stroke(Colours.stateCircle); // does not draw the blue line
  ellipse(350, 50, 30, 30);
}

void draw()
{
}

```

I expect a white circle with a blue outline. If I change the _stateCircle_ colour to 0x000000, I do get a black outline but I suspect that this is more a coincedence.

Where do I go wrong?

> [@GoToLoop](#):
>
> Besides `class`, we can also go w/ `interface` or even `enum`:

An enum will (should ?) not work if various members of the class have the same colour. I will have a look at the interface.

---

<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 10, 2023, 5:28am UTC](https://discourse.processing.org/t/class-for-colours/42169/17 "2023-06-10T05:28:17Z")

</div>

> [@sterretje](#):
>
> An `enum` will (should ?) not work if various members of the class have the same colour.

We can have `enum` constants w/ same value.  
In my example, each `enum` constant (BG, TXT, SIZE) got its own field _v_.

> [@sterretje](#):
>
> I expect a white circle with a blue outline.

> [@sterretje](#):
>
> `final static color stateFill = 0xFFFFFF; `

Your constant _ **stateFill** _ is 100% transparent!  
Use `color` value `#FFFFFF` or `-1` instead for 100% opaque white:  
`final static color stateFill = #FFFFFF; `  
`final static color stateFill = -1; `

> [@sterretje](#):
>
> `final static color stateText = 0x00000;`

For 100% opaque black you can just use value `0` or `#000000`:  
`final static color stateText = 0;`  
`final static color stateText = #000000;`

> **[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.…

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [June 10, 2023, 11:44am UTC](https://discourse.processing.org/t/class-for-colours/42169/18 "2023-06-10T11:44:45Z")

</div>

> [@GoToLoop](#):
>
> Your constant _ **stateFill** _ is 100% _transparent_!

“transparent” was the key, thanks. And yes, I did read the reference a couple of times but it did not click.

Revised class (currently not using transparency)

```auto
final static class Colours
{
  /*
  note:
   do NOT use 0x, it makes 0xFFFFFF transparent
   use # notation (e.g. stateFill = #FFFFFF)
   or
   use 32 bits, first bytes defines transparency (e.g. stateFill = 0x80FFFFFF)
   */
  final static color background = #B0B0B0; // canvas background
  final static color grid = #808080; // grid
  
  final static color stateFill = #FFFFFF; // fill colour for state (circle)
  final static color stateCircle = #000000; // stroke colour for state circle
  final static color stateRect = #E0E0E0; // stroke colour for state rectangle (visible when mouse is over)
  final static color stateText = #000000; // text color in state
  final static color statePoint = #00FF80; // fill colour for connector points
  final static color statePointIn = #FF0000; // fill colour for connector points that act as an input to the state
  final static color statePointOut = #00FF00; // fill colour for connector points that act as an output from the state
  
  final static color formBackground = #FFFFFF;
  final static color formBorder = #000000;
}

```

---

<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 10, 2023, 3:35pm UTC](https://discourse.processing.org/t/class-for-colours/42169/19 "2023-06-10T15:35:07Z")

</div>

> [@sterretje](#):
>
> `use # notation (e.g. stateFill = #FFFFFF)`

There’s another aspect on how Processing treat a `color` value: “grayscale colors”.

I believe that any `color` value less than `256` is considered a 100% opaque grayscale color!

So a 100% opaque black color value `#000000` can also be written as just `0`!

For a 100% opaque white `#FFFFFF` we can go w/ just `0xFF`, `255` or even `-1`!

Other 100% opaque grayscale values like `#808080` can be simplified to just hexadecimal `0x80`, decimal `128` or even octal `0200`!
