Extracting colours from an Image with Alteryx and R.

Here I will give a quick overview of how you can use Alteryx to firstly obtain the RGB colours within an image and then convert these RGB values into hex codes which you can then pass into the XML within a Tableau workbook as highlighted in this blog.

This post comes about as the result of some fiddling around that I was doing with the Alteryx R tool, where I was looking to analyse all the colours that exist within a photograph.

The code used looked something like below and was adapted from this post from Rbloggers.


This code will return us a three column dataset to our [1] output and will look something like this…


These values represent the depth of each colour, they are returned as a % rather than a numeric value, so the first thing we need to do is convert these RGB values from percentages and into their numeric form.

RGB values are given on a scale of 0-255,as a result we just need to simply multiply each column by 255 to return the appropriate numeric value. We can do this in one hit using the multi-field formula tool.

Now we have our RGB values in their more traditional form we are at the stage where we need to convert them into hex values.

This wiki page is great for understanding how hexadecimals work, but essentially they are a method of representing numeric values with a smaller sized field.

In Alteryx this conversion process is simple…we again use a multi-field formula tool, select each of our three fields, and wrap our [_CurrentField_] within the IntToHex() function (Interger to hexadecimal).

Given that hexadecimals are strings that may contain both letters and numbers we need too be sure we change the output type as appropriate.


One problem, Alteryx automatically removes 0’s from the beginning of our v_string fields. As a result the hexadecimal is just a single character for some of our colours (no hexadecimal colour contains less than 6 characters).


As a result we need to write a simple formula which assesses the length of the string, and where it equals 1, add ‘0’ to the front.


Now we simply need to concatenate these hexadecimals into a single string and add a ‘#’ to the front. Possible with a simple formula.


And now we have successfully converted all the RGB values given from our R script into hex colours which can now be implemented into Tableau using this technique from one of my previous posts, or perhaps we could convert them into a colour palette (this will be my next step, creating a macro within Alteryx that automatically generates a colour palette from a given picture and places them into your Tableau preferences file).

It is worth noting that this code is very sensitive during it’s colour extraction process and you can end up with thousands of unique but quite similar colours. If you wish to avoid this issue then it is perhaps worth using K-means clustering (as per the Rbloggers post), to group the colours to say a maximum of 50 unique colours.

This implementation is quite simple and can be seen in the code below.