Lowering a decimal RGB (hex) value

ShelLuser's icon

Hi guys,

I have a decimal number which represents an RGB value and I want to lower the value in order to darken the color. To that end I convert the decimal value to hex, I split it up in 3 parts which I then individually convert back to a decimal value, I subtract the number I need and then I convert them back to their hex counterpart.

My problem starts when I try to join all 3 parts back together (using 'join 3') in order to recreate the complete hex value again: I end up with "BC BC BC" instead of "BCBCBC" which is what I need so that I can create the right decimal value.

I read about 'tosymbol' and how it should be able to remove the spaces from a list if you use the message separator without arguments (so 'tosymbol @') but that doesn't work for me, the spaces in my symbol remain.

Is there a better or maybe easier way to accomplish this? I already tried checking up on jitter because I assume that it might have something to perform RGB type calculations but so far I've found nothing.

Max Patch
Copy patch and select New From Clipboard in Max.

This is the patch I'm working on, and thanks in advance for any comments!

Floating Point's icon
Max Patch
Copy patch and select New From Clipboard in Max.

almost there-- just needed to simplify it a little:

(but there may be a better approach without filling up the symbol table)

ShelLuser's icon

Thanks a bunch, that's even better! I /really/ need to study that sprintf object some more, that's for sure!

In the mean time I also came up with an alternative solution with 'regexp' but I don't really like it because it gets bloated: "regexp ^([A-Z0-9]{2})\\s([A-Z0-9]{2})\\s([A-Z0-9]{2})$ @substitute %1%2%3".

Roman Thilenius's icon

make it a list:

[fromsymbol]
[sprintf %s%s%s]

metamax's icon

You can convert directly from decimal integer to a list of 3 ints (0-255) or a list of 6 ints (0-15). That way you can tweak the channels without messing with symbols and it makes it easier to convert to a symbol format when you need it. Just iter the base 16 list into a coll that translates 0-16 to 0-F and remove spaces with regexp.

Max Patch
Copy patch and select New From Clipboard in Max.

florian1947's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Bit-shift / modulo would be more efficient.