Hi, How would you calculate the "mean colour" of several colours, for example c("#FF7C00","#00BF40","#FFFF00")? Yours, Atte Tenkanen
?strtoi You'll have to remove the "#" first, e.g. via substring() -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Apr 16, 2016 at 5:47 AM, Atte Tenkanen <attenka at utu.fi> wrote:> Hi, > > How would you calculate the "mean colour" of several colours, for example > c("#FF7C00","#00BF40","#FFFF00")? > > Yours, > > Atte Tenkanen > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
... and if you need to convert back: ?as.hexmode -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Apr 16, 2016 at 8:20 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:> ?strtoi > > You'll have to remove the "#" first, e.g. via substring() > > -- Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Sat, Apr 16, 2016 at 5:47 AM, Atte Tenkanen <attenka at utu.fi> wrote: >> Hi, >> >> How would you calculate the "mean colour" of several colours, for example >> c("#FF7C00","#00BF40","#FFFF00")? >> >> Yours, >> >> Atte Tenkanen >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code.
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
Hm..., Should these two versions produce the same solution? Unfortunately and shame to confess, I don't know much about the colors in R: myColors <- c("#FF7C00","#00BF40","#FFFF00") Colors=rgb2hsv(col2rgb(myColors)) apply(Colors,1,mean) h s v 0.2122974 1.0000000 0.9163399 * * * * * # 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] rgb2hsv(col2rgb(colorRampPalette(c(x, "#FFFF00"), space = "Lab")(4)[2])) [,1] h 0.1597633 s 0.8407960 v 0.7882353 Atte T. 16.4.2016, 19.03, Duncan Murdoch kirjoitti:> 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
grDevices has `convertColor()` and the `colorspace` has other functions that can convert from RBG to Lab space. You should convert the RGB colors to Lab and average them that way (or us other functions to convert to HSL or HSV). It all depends on what you are trying to accomplish with the "average" color determination. -Bob On Sat, Apr 16, 2016 at 12:03 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> 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 > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.