Search code examples
kdb

kdb - how generate HEX colour code as string or symbol


I would like to create a column on an in-memory table that generates a colour HEX code based on a person's name (another column). A quick google didn't give much so wondered if any pointers can be given here. e.g

update colour: <some code and use username col as input> from table

Solution

  • In kdb+ you can run a function on a column via an update statement but there are slight differences depending on whether the function is vectorised or not. If vectorised:

    update colour:{<some code>}[username] from table
    update colour:someFunction[username] from table
    

    If not vectorised then an iterator like each ' is required

    update colour:{<some code>}'[username] from table
    update colour:someFunction'[username] from table
    

    This function will generate hex codes from the first 3 characters of a string.

    q)hex:{a:i-16*j:(i:`int$3#x)div 16;"0123456789ABCDEF"raze(j-16*j div 16),'a}
    q)hex"Hello"
    "48656C"
    q)update colour:hex'[username] from table