[R] Mean of hexadecimal numbers
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Apr 16 18:03:56 CEST 2016
On 16/04/2016 8:47 AM, Atte Tenkanen wrote:
> Hi,
>
> How would you calculate the "mean colour" of several colours, for
> example c("#FF7C00","#00BF40","#FFFF00")?
>
Bert answered your subject line question. Your text is asking something
else: if those are colours, you don't want to treat each of them as a
single integer.
A simple-minded approach would split them into 3 hex numbers, and
average those (using Bert's solution).
A more sophisticated approach would take into account that they are
really colours. You could probably put together something using the
colorRamp or colorRampPalette functions to average in perception space.
For example,
# Average the 1st two by taking the middle colour of a 3 colour palette
x <- colorRampPalette(c("#FF7C00","#00BF40"), space = "Lab")(3)[2]
# Average in the third by taking the 2nd of a 4 colour palette, so x
# gets twice the weight
colorRampPalette(c(x, "#FFFF00"), space = "Lab")(4)[2]
Duncan Murdoch
More information about the R-help
mailing list