# Inspecting the max value of an element set by colormode (e.g. HSB Alpha) - possible?

**URL:** <https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512>\
**Category:** Beginners\
**Created:** [July 27, 2023, 3:08pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512 "2023-07-27T15:08:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuddyGardener](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/muddygardener/32/1719_2.png) [@MuddyGardener](https://discourse.processing.org/u/MuddyGardener)\
**Post date:** [July 27, 2023, 3:08pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/1 "2023-07-27T15:08:38Z")

</div>

Using colorMode() we can set the max for the range of the different values used to describe colours.

Is there a way to recover this information in code via an expression? (I have searched for it, but haven’t found. _Maybe_ because it doesn’t exist.)

tx

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 27, 2023, 7:24pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/2 "2023-07-27T19:24:30Z")

</div>

Hi and welcome to our forum

Not clear what you want but here is some refernace

[https://funprogramming.org/90-Change-pixel-hue-saturation-and-brightness.html](https://funprogramming.org/90-Change-pixel-hue-saturation-and-brightness.html)

> <https://stackoverflow.com/questions/67093678/hsb-color-picker>

---

<div class="post-metadata">

**Author:** ![MuddyGardener](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/muddygardener/32/1719_2.png) [@MuddyGardener](https://discourse.processing.org/u/MuddyGardener)\
**Post date:** [July 27, 2023, 7:42pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/3 "2023-07-27T19:42:45Z")

</div>

Thanks for the help @jafal . What I’m looking for is whether it’s possible to _retrieve_ the value(s) that you set, when you set colour dimension scale value(s) via colorMode.

You can use the keyword _width_ to get at the width that you define in size. I was wondering if there was an analogous _thing_ (keyword, function, property, or whatever) that you could use to retrieve the value of the upper end scale parameter, in a similar way. Is that clearer?

This is probably somewhere in the reference documentation, but I’m not really friends with it yet…

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [July 27, 2023, 10:11pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/5 "2023-07-27T22:11:01Z")

</div>

Your title mentioning “value of an element” confused people. Here’s what you’re looking for:

```auto
void setup() {
  size( 200, 200 );
  
  colorMode( HSB, 234, 7452, 423, 626 );
  
  println( g.colorModeX );
  println( g.colorModeY );
  println( g.colorModeZ );
  println( g.colorModeA );
}

```

Not very intuitive, but at least those values are public so we can read them.

I tracked those down from the source: [https://github.com/processing/processing4/blob/main/core/src/processing/core/PGraphics.java](https://github.com/processing/processing4/blob/main/core/src/processing/core/PGraphics.java)

---

<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:** [July 27, 2023, 10:27pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/6 "2023-07-27T22:27:01Z")

</div>

> [@scudly](#):
>
> I tracked those down from the source:

A more precise on target link: 🎯

> <https://github.com/benfry/processing4/blob/processing-1293-4.3/core/src/processing/core/PGraphics.java#L312-L325>

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [July 27, 2023, 10:53pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/7 "2023-07-27T22:53:17Z")

</div>

Here’s another perhaps more intuitive method. Colors given by hex codes are always RGB8 independent of colorMode().

```auto
void setup() {
  colorMode( HSB, 456, 823, 562, 2837 );
  println( 2 * hue( color(#00ffff) ) );
  println( saturation( color(#00ffff) ) );
  println( brightness( color(#00ffff) ) );
  println( alpha( color(#00ffff) ) );
}

```

`#00ffff` is halfway around the color hue, so multiply it by 2 to get the full hue range.

Accessing `g.colorModeX` directly is much faster, though, as this method causes Processing to do a bunch of needless math to get to the same result.

---

<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:** [July 28, 2023, 1:40pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/8 "2023-07-28T13:40:26Z")

</div>

I might be missing what you want to achieve.

Isn’t it easier to store the values in variables?

```java
int m1 = 1;
int m2 = 2;
int m3 = 3;

```

And use them to configute the colorMode

```java
void setup()
{
  colorMode(HSV, m1, m2, m3);
}

```

And when you need to know them somewhere else, just use the mX variable like `println("m1 = " + str(m1))`.

Code not tested.

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [July 28, 2023, 3:47pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/9 "2023-07-28T15:47:04Z")

</div>

That would be the ideal if you wrote all of the code. If you’re writing a library, however, you may not know what settings others have used.

Although in this case, you can let Processing store the variables for you and just read `g.colorModeX` instead.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 29, 2023, 9:35am UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/10 "2023-07-29T09:35:39Z")

</div>

For a well written library both of these statements should be true

- If you are using a 3rd party library then it should be _ **impossible to get any rendering settings** _ unless the author provides functions to retrieve them.

- A 3rd party library should _ **isolate any changes in rendering settings** _ from the library user by using the push()/pop() methods.

So having said that the user should store any settings they in variables if they want to reuse them later, as suggested by @sterretje .

---

<div class="post-metadata">

**Author:** ![MuddyGardener](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/muddygardener/32/1719_2.png) [@MuddyGardener](https://discourse.processing.org/u/MuddyGardener)\
**Post date:** [July 31, 2023, 8:20am UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/11 "2023-07-31T08:20:10Z")

</div>

Re @scudly advice re using g, this is indeed what I was looking for. Thank you. I’ve read somewhere (probably here or in its predecessor) that using g was going offroad and not guaranteed to be stable over updates. What I’m doing at the moment is entirely disposable so it’s not likely to be an issue in practice. But I guess that’s a Good Style thing to consider.

Accepting this answer as it’s what I was asking. Though there is lots of other interesting discussion around Good Style for dealing with this type of thing. And thanks for the supplementary reference info around the same answer, @GoToLoop

Re @scudly advice re using a numerical trick using a hex value halfway round the colour wheel - this is much less intuitive to me but it’s an interesting technique I was unfamiliar with.

---

<div class="post-metadata">

**Author:** ![MuddyGardener](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/muddygardener/32/1719_2.png) [@MuddyGardener](https://discourse.processing.org/u/MuddyGardener)\
**Post date:** [July 31, 2023, 8:24am UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/13 "2023-07-31T08:24:25Z")

</div>

Re @sterretje suggesting I just assign the value via a variable, and reference the variable when I need it, yes indeed this is indeed what I am doing. But style-wise, I prefer not to proliferate global/environment variables if I can avoid it - especially if they are already built in.

---

<div class="post-metadata">

**Author:** ![MuddyGardener](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/muddygardener/32/1719_2.png) [@MuddyGardener](https://discourse.processing.org/u/MuddyGardener)\
**Post date:** [July 31, 2023, 8:33am UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/14 "2023-07-31T08:33:38Z")

</div>

Interesting point re Good Style for 3rd party libraries @quark . Thank you. At the moment I am just writing my own code - which I may reuse in a number of different contexts, as part of the explorations I am doing, and I wanted to minimise dependencies between different aspects of it.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 31, 2023, 1:04pm UTC](https://discourse.processing.org/t/inspecting-the-max-value-of-an-element-set-by-colormode-e-g-hsb-alpha-possible/42512/15 "2023-07-31T13:04:01Z")

</div>

> [@MuddyGardener](#):
>
> advice re using g, this is indeed what I was looking for. Thank you. I’ve read somewhere (probably here or in its predecessor) that using g was going offroad and not guaranteed to be stable over updates.

When I first started using Processing (2009) using `g` was strongly discouraged but that doesn’t seem the case now.

Having said that I prefer not to use `g` directly. I always use `getGraphics()` which returns the same value as `g` - try this

```auto
println(g);
println(getGraphics());

```

and you see they refer to the same object.

Even if Processing ever makes `g` a `private` or `protected` member, `getGraphics()` will always be `public`

So instead of `g.colorModeX ` you can use `getGraphics().colorModeX`. It’s a little more verbose but there is no real loss in efficiency due to JVM optimization.
