# Simple function for color wave based on unique text values

I wanted to share a quick-and-dirty function I wrote to help with situations where you may want to color-code points in a plot based on a text wave.

For example, a scatter-plot of I dunno, say reaction length and yield but also want to color-code the points by the name of the experimentalist. Since the lattermost is probably going to be a text wave, I've not found a built-in way to use that as the color wave for the plot, which reasonably expects numerical values.

The following function takes the name of a text wave in the current data folder as its only argument and produces two waves: ColorWaveUnscaled and ColorWave. ColorWave is normalized to one which for some reason I prefer. Regardless, the values of the color wave are CRCs of the text in each row, meaning any cells with the same text will get the same numerical value, with as many unique values as there are unique strings. I'm sure there are some edge-cases where this can break somehow but that's why it's a quick-and-dirty method.

Use the output color wave as the color-scale for the scatter-plot. Anyway, I hope someone else finds this useful and doesn't have to reinvent the wheel.

Function TextToColorWave(Wave/T InputWave)

Variable waveSize = DimSize(InputWave, 0)
Make/D/O/N=(waveSize) ColorWaveUnscaled = StringCRC(0, InputWave[p])
Make/D/O/N=(waveSize) ColorWave = ColorWaveUnscaled / WaveMax(ColorWaveUnscaled)

End

Hashing the names to produce a color index wave is quite creative!

Are you aware that you can create a three- or four-column wave with a point for each data point to directly set the color of data points in the graph? We call that "color as f(z)". Then the tricky part comes down to choosing a color for each author and then setting the colors in the f(z) color wave to the appropriate color for the author associated with a given data point. No hashing required and total control over the colors you choose.

Here is a function to turn unique hashes into unique integers

function hash2integer(wave w)
duplicate/free w wIndex
wIndex = p
sort w w, wIndex
int i = 1
w = p==numpnts(w)-1 || w==w[p+1] ? i : i++
sort wIndex w
end

you could do something similar without hashing the strings by sorting and comparing strings. that way the integers can correspond to alphabetically ordered strings.

I do know about the f(z) coloring function and use it a lot. It's extremely handy.

This hash2integer function is terrific, it's like you read my mind. I'd actually been thinking about a way to convert the output from my original function into equally-spaced values. I hadn't come up with anything obvious yet. Thank you!