Skip to content
loading...

re: Daily Challenge #236 - RGB to Hex Conversion VIEW POST

FULL DISCUSSION
 

A similar solution in R

rgb <- function(r, g, b) {
  h <- function(x) toupper(format(as.hexmode(min(255, max(0, x))), width=2))
  paste0(h(r), h(g), h(b))
}

Tests

> rgb(255, 255, 255)
[1] "FFFFFF"
> rgb(255, 255, 300)
[1] "FFFFFF"
> rgb(0, 0, 0)
[1] "000000"
> rgb(148, 0, 211)
[1] "9400D3"
> rgb(-20, 275, 125)
[1] "00FF7D"
> rgb(255, 255, 255)
[1] "FFFFFF"
code of conduct - report abuse