0

I have two values (between 0 and 255), from audio sources. I'm trying to display two different RGB colours, a rectangle split by the middle, the colors from these two changing numbers, the colors changing as the numbers.

thank for you help, I know it's simple, but i'm really stuck.

Have a nice day.

JohnH
  • 2,713
  • 12
  • 21
ZACK F.
  • 23
  • 5

1 Answers1

0

To show a “split rectangle,” I would suggest using two panel objects side by side.

You can control the panel object’s colour using the bgcolor (or bgfillcolor) message followed by a list of four numbers, e.g.: bgcolor 1 0 0 0.5

The numbers correspond to Red, Green, Blue, and Alpha (opacity) and are all in the range of 0–1. So bgcolor 1 0 0 0.5 would set the colour of the panel to 100% red and 50% transparent.

In your case, you have values 0–255 so you need to scale those down to 0–1 and then decide how exactly you want to map them. For example, if you wanted each panel to be white when the value is 255, black when it is 0, and some shade of grey in between, you could do something like this:

value → [scale 0 255 0. 1.] → (bgcolor $1 $1 $1 1) → [panel]

Max patch showing a value 0–255 scaled to 0–1 controlling the colour of a panel object using a bgcolor message

Some notes:

  • The last two arguments of scale must be floats, otherwise it will just output integers 0 and 1.

  • The $1 syntax of the message box gets replaced by input. You could decide for example to only control the green part of the colour: bgcolor 0 $1 0 1.

  • If your value changes very frequently, you might want to limit how often it updates the colour. Most screens won’t update faster than 60 times per second, so changing the colour every millisecond for example is a waste of resources. If you’re using snapshot~ to get the value from audio, you could use an argument of 17 or higher, to limit the values to less than 60 times per second (1000ms / 60 = 16.6…). Alternatively, you could use the speedlim object to limit how often you recalculate your colour.

swithinbank
  • 1,127
  • 1
  • 6
  • 15