Dear r-helpers, I'm interested in locating the named colour that's "closest" to an arbitrary RGB colour. The best that I've been able to come up is the following, which uses HSV colours for the comparison: r2c <- function(){ hexnumerals <- 0:15 names(hexnumerals) <- c(0:9, LETTERS[1:6]) hex2decimal <- function(hexnums){ hexnums <- strsplit(hexnums, "") decimals <- matrix(0, 3, length(hexnums)) decimals[1, ] <- sapply(hexnums, function(x) sum(hexnumerals[x[1:2]] * c(16, 1))) decimals[2, ] <- sapply(hexnums, function(x) sum(hexnumerals[x[3:4]] * c(16, 1))) decimals[3, ] <- sapply(hexnums, function(x) sum(hexnumerals[x[5:6]] * c(16, 1))) decimals } colors <- colors() hsv <- rgb2hsv(col2rgb(colors)) function(cols){ cols <- sub("^#", "", toupper(cols)) dec.cols <- rgb2hsv(hex2decimal(cols)) colors[apply(dec.cols, 2, function(dec.col) which.min(colSums((hsv - dec.col)^2)))] } } rgb2col <- r2c() I've programmed this with a closure so that hsv gets computed only once. Examples:> rgb2col(c("AA0000", "002200", "000099", "333300", "BB00BB", "#005555"))[1] "darkred" "darkgreen" "blue4" "darkgreen" "magenta3" "darkgreen"> rgb2col(c("AAAA00", "#00AAAA"))[1] "darkgoldenrod" "cyan4" Some of these colour matches, e.g., "#005555" -> "darkgreen" seem poor to me. Even if the approach is sound, I'd like to be able to detect that there is no sufficiently close match in the vector of named colours. That is, can I establish a maximum acceptable distance in the HSV (or some other) colour space? I vaguely recall a paper or discussion concerning colour representation in R but can't locate it. Any suggestions would be appreciated. John ------------------------------------------------ John Fox Sen. William McMaster Prof. of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/
On 05/30/2013 10:13 PM, John Fox wrote:> Dear r-helpers, > > I'm interested in locating the named colour that's "closest" to an arbitrary RGB colour. The best that I've been able to come up is the following, which uses HSV colours for the comparison: > > r2c<- function(){ > hexnumerals<- 0:15 > names(hexnumerals)<- c(0:9, LETTERS[1:6]) > hex2decimal<- function(hexnums){ > hexnums<- strsplit(hexnums, "") > decimals<- matrix(0, 3, length(hexnums)) > decimals[1, ]<- sapply(hexnums, function(x) > sum(hexnumerals[x[1:2]] * c(16, 1))) > decimals[2, ]<- sapply(hexnums, function(x) > sum(hexnumerals[x[3:4]] * c(16, 1))) > decimals[3, ]<- sapply(hexnums, function(x) > sum(hexnumerals[x[5:6]] * c(16, 1))) > decimals > } > colors<- colors() > hsv<- rgb2hsv(col2rgb(colors)) > function(cols){ > cols<- sub("^#", "", toupper(cols)) > dec.cols<- rgb2hsv(hex2decimal(cols)) > colors[apply(dec.cols, 2, function(dec.col) > which.min(colSums((hsv - dec.col)^2)))] > } > } > > rgb2col<- r2c() > > I've programmed this with a closure so that hsv gets computed only once. > > Examples: > >> rgb2col(c("AA0000", "002200", "000099", "333300", "BB00BB", "#005555")) > [1] "darkred" "darkgreen" "blue4" "darkgreen" "magenta3" "darkgreen" >> rgb2col(c("AAAA00", "#00AAAA")) > [1] "darkgoldenrod" "cyan4" > > Some of these colour matches, e.g., "#005555" -> "darkgreen" seem poor to me. Even if the approach is sound, I'd like to be able to detect that there is no sufficiently close match in the vector of named colours. That is, can I establish a maximum acceptable distance in the HSV (or some other) colour space? > > I vaguely recall a paper or discussion concerning colour representation in R but can't locate it. > > Any suggestions would be appreciated. > > John >Hi John, Ben Bolker contributed a function (color.id) to the plotrix package that does something like this. Although it uses RGB colorspace, it might be useful: color.id<-function (col) { c2 <- col2rgb(col) coltab <- col2rgb(colors()) cdist <- apply(coltab, 2, function(z) sum((z - c2)^2)) colors()[which(cdist == min(cdist))] } Jim
John Fox <jfox <at> mcmaster.ca> writes:> I'm interested in locating the named colour that's "closest" to anarbitrary RGB colour. The best that I've> been able to come up is the following, which usesHSV colours for the comparison:> > r2c <- function(){ > hexnumerals <- 0:15 > names(hexnumerals) <- c(0:9, LETTERS[1:6]) > hex2decimal <- function(hexnums){ > hexnums <- strsplit(hexnums, "") > decimals <- matrix(0, 3, length(hexnums)) > decimals[1, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[1:2]] * c(16, 1))) > decimals[2, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[3:4]] * c(16, 1))) > decimals[3, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[5:6]] * c(16, 1))) > decimals > } > colors <- colors() > hsv <- rgb2hsv(col2rgb(colors)) > function(cols){ > cols <- sub("^#", "", toupper(cols)) > dec.cols <- rgb2hsv(hex2decimal(cols)) > colors[apply(dec.cols, 2, function(dec.col) > which.min(colSums((hsv - dec.col)^2)))] > } > } > > rgb2col <- r2c() > > I've programmed this with a closure so that hsv getscomputed only once.> > > I vaguely recall a paper or discussion concerning colour representation inR but can't locate it.>Dear John, Are you thinking of Zeileis A, Hornik K, Murrell P (2009). ?Escaping RGBland: Selecting Colors for Statistical Graphics.? Computational Statistics & Data Analysis, 53, 3259?3270. doi:10.1016/j. csda.2008.11.033. or the vignette from the colorspace package that covers some of the same territory. How about if you compute color distances in Lab or Luv spaces? These so-called uniform color spaces are based on human discrimination data for small color distances so should be better at finding the nearest color, I would think. Untried though. Don't forget that these are based on a standard color calibration, sRGB, I think in the case of R and your display may or may not be close to that, so expect some error from that as well. best, Ken> Any suggestions would be appreciated. > > John > > ------------------------------------------------ > John Fox > Sen. William McMaster Prof. of Social Statistics > Department of Sociology > McMaster University > Hamilton, Ontario, Canada > http://socserv.mcmaster.ca/jfox/ > >-- Kenneth Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen L?pine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html
Dear John,>>>>> John Fox <jfox at mcmaster.ca> >>>>> on Thu, 30 May 2013 08:13:19 -0400 writes:> Dear r-helpers, > I'm interested in locating the named colour that's "closest" to an arbitrary RGB colour. Hmm, maybe I was not really marketing well enough what I had added for R 3.0.0 : ------------------------------------------------------------------------ r61127 | maechler | 2012-11-17 20:00:58 +0100 (Sat, 17 Nov 2012) | 1 line new option colors(distinct=TRUE); new demo(colors) & demo(hclColors) ------------------------------------------------------------------------ demo(colors) contains a few niceties, some originating from ------------ Marius Hofert, notably for you the function nearRcolor() which has the nice extra that you can specify the color *space* in which to measure nearness. ##' Find close R colors() to a given color {original by Marius Hofert) ##' using Euclidean norm in (HSV / RGB / ...) color space nearRcolor <- function(rgb, cSpace = c("hsv", "rgb255", "Luv", "Lab"), dist = switch(cSpace, "hsv" = 0.10, "rgb255" = 30, "Luv" = 15, "Lab" = 12)) { if(is.character(rgb)) rgb <- col2rgb(rgb) stopifnot(length(rgb <- as.vector(rgb)) == 3) Rcol <- col2rgb(.cc <- colors()) uniqC <- !duplicated(t(Rcol)) # gray9 == grey9 (etc) Rcol <- Rcol[, uniqC] ; .cc <- .cc[uniqC] cSpace <- match.arg(cSpace) convRGB2 <- function(Rgb, to) t(convertColor(t(Rgb), from="sRGB", to=to, scale.in=255)) ## the transformation, rgb{0..255} --> cSpace : TransF <- switch(cSpace, "rgb255" = identity, "hsv" = rgb2hsv, "Luv" = function(RGB) convRGB2(RGB, "Luv"), "Lab" = function(RGB) convRGB2(RGB, "Lab")) d <- sqrt(colSums((TransF(Rcol) - as.vector(TransF(rgb)))^2)) iS <- sort.list(d[near <- d <= dist])# sorted: closest first setNames(.cc[near][iS], format(d[near][iS], digits=3)) } You should use the full demo(colors) or browse https://svn.r-project.org/R/trunk/src/library/grDevices/demo/colors.R to also get a few nice examples.. Martin> The best that I've been able to come up is the following, which uses HSV colours for the comparison:> > r2c <- function(){ > hexnumerals <- 0:15 > names(hexnumerals) <- c(0:9, LETTERS[1:6]) > hex2decimal <- function(hexnums){ > hexnums <- strsplit(hexnums, "") > decimals <- matrix(0, 3, length(hexnums)) > decimals[1, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[1:2]] * c(16, 1))) > decimals[2, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[3:4]] * c(16, 1))) > decimals[3, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[5:6]] * c(16, 1))) > decimals > } > colors <- colors() > hsv <- rgb2hsv(col2rgb(colors)) > function(cols){ > cols <- sub("^#", "", toupper(cols)) > dec.cols <- rgb2hsv(hex2decimal(cols)) > colors[apply(dec.cols, 2, function(dec.col) > which.min(colSums((hsv - dec.col)^2)))] > } > } > > rgb2col <- r2c() > > I've programmed this with a closure so that hsv gets computed only once. > > Examples: > > > rgb2col(c("AA0000", "002200", "000099", "333300", "BB00BB", "#005555")) > [1] "darkred" "darkgreen" "blue4" "darkgreen" "magenta3" "darkgreen" > > rgb2col(c("AAAA00", "#00AAAA")) > [1] "darkgoldenrod" "cyan4" > > Some of these colour matches, e.g., "#005555" -> "darkgreen" seem poor to me. Even if the approach is sound, I'd like to be able to detect that there is no sufficiently close match in the vector of named colours. That is, can I establish a maximum acceptable distance in the HSV (or some other) colour space? > > I vaguely recall a paper or discussion concerning colour representation in R but can't locate it. > > Any suggestions would be appreciated. > > John > > ------------------------------------------------ > John Fox > Sen. William McMaster Prof. of Social Statistics > Department of Sociology > McMaster University > Hamilton, Ontario, Canada > http://socserv.mcmaster.ca/jfox/
Hi John, i would propose a one-liner for the hexcode transformation: hex2dec<-function(hexnums)sapply(strtoi(hexnums,16L),function(x)x%/%256^(2:0)%%256) #instead of hexnumerals <- 0:15 names(hexnumerals) <- c(0:9, LETTERS[1:6]) hex2decimal <- function(hexnums){ hexnums <- strsplit(hexnums, "") decimals <- matrix(0, 3, length(hexnums)) decimals[1, ] <- sapply(hexnums, function(x) sum(hexnumerals[x[1:2]] * c(16, 1))) decimals[2, ] <- sapply(hexnums, function(x) sum(hexnumerals[x[3:4]] * c(16, 1))) decimals[3, ] <- sapply(hexnums, function(x) sum(hexnumerals[x[5:6]] * c(16, 1))) decimals } #some tests cols<-c("AA0000", "002200", "000099", "333300", "BB00BB", "005555") cols<-sub("^#","",toupper(cols)) #actually 'toupper' is not needed for hex2dec #check results hex2decimal(cols) hex2dec(cols) #it is not only shorter ocde, but even faster. cols.test<-sprintf("%06X",sample(0:(256^3),100000)) system.time(hex2decimal(cols.test)) # User System verstrichen # 3.54 0.00 3.61 system.time(hex2dec(cols.test)) # User System verstrichen # 0.53 0.00 0.53 cheers. Am 30.05.2013 14:13, schrieb John Fox:> Dear r-helpers, > > I'm interested in locating the named colour that's "closest" to an arbitrary RGB colour. The best that I've been able to come up is the following, which uses HSV colours for the comparison: > > r2c <- function(){ > hexnumerals <- 0:15 > names(hexnumerals) <- c(0:9, LETTERS[1:6]) > hex2decimal <- function(hexnums){ > hexnums <- strsplit(hexnums, "") > decimals <- matrix(0, 3, length(hexnums)) > decimals[1, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[1:2]] * c(16, 1))) > decimals[2, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[3:4]] * c(16, 1))) > decimals[3, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[5:6]] * c(16, 1))) > decimals > } > colors <- colors() > hsv <- rgb2hsv(col2rgb(colors)) > function(cols){ > cols <- sub("^#", "", toupper(cols)) > dec.cols <- rgb2hsv(hex2decimal(cols)) > colors[apply(dec.cols, 2, function(dec.col) > which.min(colSums((hsv - dec.col)^2)))] > } > } > > rgb2col <- r2c() > > I've programmed this with a closure so that hsv gets computed only once. > > Examples: > >> rgb2col(c("AA0000", "002200", "000099", "333300", "BB00BB", "#005555")) > [1] "darkred" "darkgreen" "blue4" "darkgreen" "magenta3" "darkgreen" >> rgb2col(c("AAAA00", "#00AAAA")) > [1] "darkgoldenrod" "cyan4" > > Some of these colour matches, e.g., "#005555" -> "darkgreen" seem poor to me. Even if the approach is sound, I'd like to be able to detect that there is no sufficiently close match in the vector of named colours. That is, can I establish a maximum acceptable distance in the HSV (or some other) colour space? > > I vaguely recall a paper or discussion concerning colour representation in R but can't locate it. > > Any suggestions would be appreciated. > > John > > ------------------------------------------------ > John Fox > Sen. William McMaster Prof. of Social Statistics > Department of Sociology > McMaster University > Hamilton, Ontario, Canada > http://socserv.mcmaster.ca/jfox/ > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Astrid Lurati (Kommissarisch), Joachim Pr?l?, Matthias Waldmann (Kommissarisch) Bitte erw?gen Sie, ob diese Mail ausgedruckt werden muss - der Umwelt zuliebe. Please consider whether this mail must be printed - please think of the environment.
Dear all, My thanks to everyone who addressed my question. I've incorporated Eik Vettorazzi's suggestion for improved conversion of hexadecimal RGB colours to decimal numbers, and Martin Maechler's hint to look at demo(colors). I've loosened the default definition of "close enough" from the latter, since the following seems to work well for my purposes. r2c <- function(){ hex2dec <- function(hexnums) { # suggestion of Eik Vettorazzi sapply(strtoi(hexnums, 16L), function(x) x %/% 256^(2:0) %% 256) } findMatch <- function(dec.col) { sq.dist <- colSums((hsv - dec.col)^2) rbind(which.min(sq.dist), min(sq.dist)) } colors <- colors() hsv <- rgb2hsv(col2rgb(colors)) function(cols, near=0.25){ cols <- sub("^#", "", toupper(cols)) dec.cols <- rgb2hsv(hex2dec(cols)) which.col <- apply(dec.cols, 2, findMatch) matches <- colors[which.col[1, ]] unmatched <- which.col[2, ] > near^2 matches[unmatched] <- paste("#", cols[unmatched], sep="") matches } } rgb2col <- r2c() For example,> rgb2col(c("010101", "EEEEEE", "AA0000", "00AA00", "0000AA", "AAAA00","AA00AA", "00AAAA")) [1] "black" "gray93" "darkred" "green4" [5] "blue4" "darkgoldenrod" "darkmagenta" "cyan4"> rgb2col(c("010101", "090909", "090000", "000900", "000009", "090900","090009", "000909")) [1] "black" "gray3" "#090000" "#000900" "#000009" "#090900" [7] "#090009" "#000909" Thanks again, John> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of John Fox > Sent: Thursday, May 30, 2013 8:13 AM > To: r-help at r-project.org > Subject: [R] measuring distances between colours? > > Dear r-helpers, > > I'm interested in locating the named colour that's "closest" to an > arbitrary RGB colour. The best that I've been able to come up is the > following, which uses HSV colours for the comparison: > > r2c <- function(){ > hexnumerals <- 0:15 > names(hexnumerals) <- c(0:9, LETTERS[1:6]) > hex2decimal <- function(hexnums){ > hexnums <- strsplit(hexnums, "") > decimals <- matrix(0, 3, length(hexnums)) > decimals[1, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[1:2]] * c(16, 1))) > decimals[2, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[3:4]] * c(16, 1))) > decimals[3, ] <- sapply(hexnums, function(x) > sum(hexnumerals[x[5:6]] * c(16, 1))) > decimals > } > colors <- colors() > hsv <- rgb2hsv(col2rgb(colors)) > function(cols){ > cols <- sub("^#", "", toupper(cols)) > dec.cols <- rgb2hsv(hex2decimal(cols)) > colors[apply(dec.cols, 2, function(dec.col) > which.min(colSums((hsv - dec.col)^2)))] > } > } > > rgb2col <- r2c() > > I've programmed this with a closure so that hsv gets computed only > once. > > Examples: > > > rgb2col(c("AA0000", "002200", "000099", "333300", "BB00BB", > "#005555")) > [1] "darkred" "darkgreen" "blue4" "darkgreen" "magenta3" > "darkgreen" > > rgb2col(c("AAAA00", "#00AAAA")) > [1] "darkgoldenrod" "cyan4" > > Some of these colour matches, e.g., "#005555" -> "darkgreen" seem poor > to me. Even if the approach is sound, I'd like to be able to detect > that there is no sufficiently close match in the vector of named > colours. That is, can I establish a maximum acceptable distance in the > HSV (or some other) colour space? > > I vaguely recall a paper or discussion concerning colour representation > in R but can't locate it. > > Any suggestions would be appreciated. > > John > > ------------------------------------------------ > John Fox > Sen. William McMaster Prof. of Social Statistics > Department of Sociology > McMaster University > Hamilton, Ontario, Canada > http://socserv.mcmaster.ca/jfox/ > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi John, Out of curiosity and if it is not much trouble, I would be curious if Luv worked any better than Lab. I think that Luv is supposed to be preferred for monitors and Lab for surfaces but they are generally pretty similar. Best, Ken Sent from my iPhone ___ Ken Knoblauch Inserm U846 Stem-Cell and Brain Research Institute 18 av du Doyen L?pine 69500 Bron France Tel : 04 72 91 34 77 Fax : 04 72 91 34 61 portable : 06 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html On 1 juin 2013, at 17:31, "John Fox" <jfox at mcmaster.ca> wrote:> Hi Michael, > > Thanks for the Wikipedia tip -- I'd looked there but didn't find this > article. The article explains that the Lab colour space was formulated to > provide uniform perceptual differences between colours, with a JND of > approximately of 2.3. Ken Knoblauch made a similar point. The article goes > on to describe relatively complicated adjustments meant to improve the LAV > distance metric, which are probably overkill for my application. > > I've programmed Lab colour matching as follows, using Euclidean distances > and adapting Kevin Wright's modification of my original code. I used > convertColor(), which Martin Maechler pointed out to me. > > ----------- snip -------------- > > r2c <- function(){ > all.names <- colors() > all.lab <- t(convertColor(t(col2rgb(all.names)), from="sRGB", to="Lab", > scale.in=255)) > find.near <- function(x.lab) { > sq.dist <- colSums((all.lab - x.lab)^2) > rbind(all.names[which.min(sq.dist)], min(sq.dist)) > } > function(cols.hex, near=2.3){ > cols.lab <- t(convertColor(t(col2rgb(cols.hex)), from="sRGB", > to="Lab", scale.in=255)) > cols.near <- apply(cols.lab, 2, find.near) > ifelse(cols.near[2, ] < near^2, cols.near[1, ], cols.hex) > } > } > > rgb2col <- r2c() > > ----------- snip -------------- > > A bit of experimentation suggests that this works better than using (as I > did previously) direct RGB distances, matching more colours to names and > providing (to my eye, with my monitor) perceptually closer matches, though > sometimes with (again to my eye) perceptible differences. Here's an > illustration, adapting one of Kevin's examples: > > ----------- snip -------------- > > cols <- c("#010101", "#EEEEEE", "#AA0000", "#00AA00", "#0000AA", "#AAAA00", > "#AA00AA", "#00AAAA") > (nms <- rgb2col(cols)) > pie(rep(1, 2*length(cols)), labels=c(cols, nms), col=c(cols, nms)) > > ----------- snip -------------- > > Thanks again to everyone who responded to my original, na?ve, question. > > Best, > John > >> -----Original Message----- >> From: Michael Friendly [mailto:friendly at yorku.ca] >> Sent: Friday, May 31, 2013 10:24 AM >> To: John Fox >> Cc: r-help at r-project.org; 'Martin Maechler' >> Subject: Re: measuring distances between colours? >> >> Hi John >> This has been an interesting discussion. >> Though you have a solution for your needs, you might be interested in >> this javascript implementation that allows you to visually compare >> color >> distances in various color spaces >> >> http://stevehanov.ca/blog/index.php?id=116 >> >> And, all the theory of color distance is described in >> http://en.wikipedia.org/wiki/Color_difference >> >> PS: This is a very handy function. When I last tried >> aplpack::bagplot(), it was annoying that the colors could *only* >> be specified in hex. >> >> -Michael >> >> >> >> On 5/30/2013 5:14 PM, John Fox wrote: >>> Dear all, >>> >>> My thanks to everyone who addressed my question. I've incorporated >> Eik >>> Vettorazzi's suggestion for improved conversion of hexadecimal RGB >> colours >>> to decimal numbers, and Martin Maechler's hint to look at >> demo(colors). I've >>> loosened the default definition of "close enough" from the latter, >> since the >>> following seems to work well for my purposes. >>> >>> r2c <- function(){ >>> hex2dec <- function(hexnums) { >>> # suggestion of Eik Vettorazzi >>> sapply(strtoi(hexnums, 16L), function(x) x %/% 256^(2:0) %% >> 256) >>> } >>> findMatch <- function(dec.col) { >>> sq.dist <- colSums((hsv - dec.col)^2) >>> rbind(which.min(sq.dist), min(sq.dist)) >>> } >>> colors <- colors() >>> hsv <- rgb2hsv(col2rgb(colors)) >>> function(cols, near=0.25){ >>> cols <- sub("^#", "", toupper(cols)) >>> dec.cols <- rgb2hsv(hex2dec(cols)) >>> which.col <- apply(dec.cols, 2, findMatch) >>> matches <- colors[which.col[1, ]] >>> unmatched <- which.col[2, ] > near^2 >>> matches[unmatched] <- paste("#", cols[unmatched], sep="") >>> matches >>> } >>> } >>> >>> rgb2col <- r2c() >>> >>> For example, >>> >>>> rgb2col(c("010101", "EEEEEE", "AA0000", "00AA00", "0000AA", >> "AAAA00", >>> "AA00AA", "00AAAA")) >>> [1] "black" "gray93" "darkred" "green4" >>> [5] "blue4" "darkgoldenrod" "darkmagenta" "cyan4" >>> >>>> rgb2col(c("010101", "090909", "090000", "000900", "000009", >> "090900", >>> "090009", "000909")) >>> [1] "black" "gray3" "#090000" "#000900" "#000009" "#090900" >>> [7] "#090009" "#000909" >>> >>> Thanks again, >>> John >> >> >> -- >> Michael Friendly Email: friendly AT yorku DOT ca >> Professor, Psychology Dept. & Chair, Quantitative Methods >> York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 >> 4700 Keele Street Web: http://www.datavis.ca >> Toronto, ONT M3J 1P3 CANADA >
I'd have to look it up and I'm not home at the moment. Can see later on. I would have thought that it would be normalized to have a jnd equal to 1 but I'm not sure. Ken Sent from my iPhone ___ Ken Knoblauch Inserm U846 Stem-Cell and Brain Research Institute 18 av du Doyen L?pine 69500 Bron France Tel : 04 72 91 34 77 Fax : 04 72 91 34 61 portable : 06 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html On 1 juin 2013, at 18:44, "John Fox" <jfox at mcmaster.ca> wrote:> Hi Ken, > > I just tried that, and with a distance of 15 as the criterion, which seemed to work well for Lav distances, I get fewer matches for the following example that we've been using: > >> cols <- c("#010101", "#EEEEEE", "#AA0000", "#00AA00", "#0000AA", "#AAAA00", "#AA00AA", "#00AAAA") >> (nms <- rgb2col(cols, near=15)) > [1] "black" "gray93" "firebrick" "#00AA00" > [5] "#0000AA" "#AAAA00" "#AA00AA" "lightseagreen" > > Do you know what a JND is supposed to be on the Luv distance scale? The Wikipedia article on CIELUV colours doesn't say. > > Best, > John > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- >> project.org] On Behalf Of Ken Knoblauch >> Sent: Saturday, June 01, 2013 12:18 PM >> To: John Fox >> Cc: <r-help at r-project.org>; Michael Friendly; Martin Maechler >> Subject: Re: [R] measuring distances between colours? >> >> Hi John, >> >> Out of curiosity and if it is not much trouble, I would be curious if >> Luv worked any better than Lab. I think that Luv is supposed to be >> preferred for monitors and Lab for surfaces but they are generally >> pretty similar. >> >> Best, >> >> Ken >> >> Sent from my iPhone >> >> ___