Converting RGB values to Hexadecimal?

kindofblue272's icon

I am working on a patch to control a string of RGB LEDs via music input. Currently I have been able to use a filter bank to generate R G B values (either on a scale of 0 to 255 or 0 to 1.0) from a color look-up table. I am having trouble figuring out how to convert these values to hex format for output to the LEDs though. I have found several javascript examples, but haven't been able to get them to work within a Max patch, anybody know how to do this?

cap10subtext's icon

You can use the Lhex object along with the regexp in the patch below:

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

I wanted to be able to do this without installing any externals. An alternative is to feed number boxes into [sprintf]. To get HTML color codes, for example, use

sprintf #%x%x%x

Liam Joy Niemeijer's icon

if you use sprintf %02x%02x%02x you consistently get 8 numbers

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

I don't know if this might be useful to someone in the future. here is a patch that converts RGB to hex. Open RGBtoHex subpatch to understand the logic behind this! Cheers

ghost from the past's icon

why all that many objects for what single sprintf can do ?

Roman Thilenius's icon

if coll, then one should fill it up with all values, so that it is the most lean solution.

TFL's icon

@ROMAN I tried to build such a coll with three [uzi 256] in cascade, feeding a [join 3] going into a [expr $i1 * 65536 + $i2 * 256 + $i3] to generate the index and the [sprintf] seen above to generate the hexadecimal.

Turns out Max hangs forever generating the 16 777 216 possible results :( Had to force it to quit every time.

Roman Thilenius's icon

haha, no of course only for single bytes.

coll tosymbol sprintf, then wrapped in iter zl.len zl.group to support arbitrary list lenghts.