Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Created June 23, 2014 18:13
Show Gist options
  • Save johnbaums/c0b90ab1d6a29599359a to your computer and use it in GitHub Desktop.
Save johnbaums/c0b90ab1d6a29599359a to your computer and use it in GitHub Desktop.
Generating identicons for fun and profit
# PHP source at http://scott.sherrillmix.com/blog/blogger/wp_identicon/
# http://www.gravatar.com/avatar/b1554c62bf1d05a4a9c48754a6619c17.png?d=identicon
# Related SO post: http://stackoverflow.com/q/24347608/489704
# Download and read in PHP source for shape creation
download.file('http://downloads.wordpress.org/plugin/wp-identicon.zip',
destfile=f <- tempfile())
unzip(f, exdir=tempdir())
txt <- readLines(file.path(tempdir(), 'wp-identicon', 'wp_identicon.php'),
warn=FALSE)[55:98]
# Clean up and convert PHP nested arrays to R nested lists
txt <- gsub('\\$this->', '',
gsub('array', 'list',
gsub('array\\((?!array)', 'c(',
paste('list(', paste(sub(',//.*', '', txt),
collapse=','), ')'),
perl=TRUE)))
identicon.units <- list(quarter=0.25, half=0.5, diagonal=sqrt(2 * 0.5^2),
halfdiag=sqrt(2 * 0.5^2)/2)
# Create a list of angles and distance representing polygons
# (object dput at bottom of this gist)
identicon.shapes <-
lapply(eval(parse(text=txt), envir=identicon.units), function(x) {
lapply(x, function(y) {
do.call(rbind, y)
})
})
# Function to get coordinates given angle and distance
get.coords <- function(a, d, flip='n') {
if (!flip %in% c('h', 'v', 'n'))
stop('flip must be "n" (none), "h" (horizontal) or "v" (vertical)')
d2 <- d
if (flip=='h') d <- -d
if (flip=='v') d2 <- -d2
cbind(-d*cos((-a+90) * pi/180), -d2*sin((-a+90) * pi/180))
}
# Function to plot a single block
identiblock <- function(shp, col='black', bg='white', rotate=0, flip='n',
invert=FALSE) {
if (!rotate %in% c(0, 90, 180, 270))
stop('Rotation must be 0, 90, 180 or 270')
plot.new()
plot.window(xlim=c(-0.5, 0.5), ylim=c(-0.5, 0.5), xaxs='i', yaxs='i', asp=1)
rect(-0.5, -0.5, 0.5, 0.5, col=ifelse(isTRUE(invert), col, bg), border=NA)
invisible(sapply(identicon.shapes[[shp]], function(x) {
x[, 1] <- x[, 1] + rotate
polygon(get.coords(x[, 1], x[, 2], flip=flip),
col=ifelse(isTRUE(invert), bg, col), border=NA)
}))
}
identiblock(7)
identiblock(7, flip='h')
identiblock(7, rotate=90)
identiblock(7, invert=TRUE)
# All standard identicon shapes
png(f <- tempfile(fileext='.png'), width=525, height=105, type='cairo')
par(mfrow=c(3, 15), mar=rep(0.1, 4))
invisible(sapply(seq_along(identicon.shapes), function(i) {
identiblock(i)
}))
dev.off()
file.show(f)
# Function to create an identicon
# In reality randomisation is more complicated
identicon <- function(seed) {
if(!is.numeric(seed)) {
require(digest)
seed <- eval(parse(text=paste0('0x', substr(digest(seed, 'sha1'), 1, 7))))
}
set.seed(seed)
shp <- c(sample(length(identicon.shapes), 2),
sample(c(1, 4, 8, 25, 26, 30, 34) + 1, 1))
invert <- as.logical(rbinom(3, 1, 0.5))
col <- paste0('#', as.hexmode(sample(0xFFFFFF, 1)))
png(f <- tempfile(fileext='.png'), height=128, width=128, type='cairo')
par(mfrow=c(4, 4), mar=rep(0, 4))
identiblock(shp[1], col=col, invert=invert[1])
identiblock(shp[2], col=col, invert=invert[2])
identiblock(shp[2], rotate=90, col=col, invert=invert[2])
identiblock(shp[1], rotate=90, col=col, invert=invert[1])
identiblock(shp[2], col=col, invert=invert[2])#, flip='h', rotate=270)
identiblock(shp[3], col=col, invert=invert[3])
identiblock(shp[3], col=col, invert=invert[3])
identiblock(shp[2], rotate=270, col=col, invert=invert[2])
identiblock(shp[2], rotate=270, col=col, invert=invert[2])
identiblock(shp[3], col=col, invert=invert[3])
identiblock(shp[3], col=col, invert=invert[3])
identiblock(shp[2], col=col, invert=invert[2])#, flip='h', rotate=270)
identiblock(shp[1], rotate=270, col=col, invert=invert[1])
identiblock(shp[2], rotate=90, col=col, invert=invert[2])
identiblock(shp[2], col=col, invert=invert[2])
identiblock(shp[1], rotate=180, col=col, invert=invert[1])
dev.off()
file.show(f)
}
identicon('someones@email.com')
# For safe-keeping (in case PHP source disappears):
# > dput(identicon.shapes)
# list(list(structure(c(90, 135, 225, 270, 0.5, 0.707106781186548,
# 0.707106781186548, 0.5), .Dim = c(4L, 2L))), list(structure(c(45,
# 135, 225, 315, 0.707106781186548, 0.707106781186548, 0.707106781186548,
# 0.707106781186548), .Dim = c(4L, 2L))), list(structure(c(45,
# 135, 225, 0.707106781186548, 0.707106781186548, 0.707106781186548
# ), .Dim = c(3L, 2L))), list(structure(c(90, 225, 315, 0.5, 0.707106781186548,
# 0.707106781186548), .Dim = c(3L, 2L))), list(structure(c(0, 90,
# 180, 270, 0.5, 0.5, 0.5, 0.5), .Dim = c(4L, 2L))), list(structure(c(0,
# 135, 270, 315, 0.5, 0.707106781186548, 0.5, 0.707106781186548
# ), .Dim = c(4L, 2L))), list(structure(c(0, 90, 180, 0.25, 0.5,
# 0.25), .Dim = c(3L, 2L)), structure(c(0, 315, 270, 0.25, 0.707106781186548,
# 0.5), .Dim = c(3L, 2L)), structure(c(270, 180, 225, 0.5, 0.25,
# 0.707106781186548), .Dim = c(3L, 2L))), list(structure(c(0, 135,
# 270, 0.5, 0.707106781186548, 0.5), .Dim = c(3L, 2L))), list(structure(c(45,
# 135, 225, 315, 0.353553390593274, 0.353553390593274, 0.353553390593274,
# 0.353553390593274), .Dim = c(4L, 2L))), list(structure(c(180,
# 225, 0, 0.5, 0.707106781186548, 0), .Dim = c(3L, 2L)), structure(c(45,
# 90, 0, 0.707106781186548, 0.5, 0), .Dim = c(3L, 2L))), list(structure(c(90,
# 135, 180, 0, 0.5, 0.707106781186548, 0.5, 0), .Dim = c(4L, 2L
# ))), list(structure(c(0, 180, 270, 0.5, 0.5, 0.5), .Dim = c(3L,
# 2L))), list(structure(c(315, 225, 0, 0.707106781186548, 0.707106781186548,
# 0), .Dim = c(3L, 2L))), list(structure(c(90, 180, 0, 0.5, 0.5,
# 0), .Dim = c(3L, 2L))), list(structure(c(90, 135, 180, 0.5, 0.707106781186548,
# 0.5), .Dim = c(3L, 2L))), list(structure(c(90, 135, 180, 0, 0.5,
# 0.707106781186548, 0.5, 0), .Dim = c(4L, 2L)), structure(c(0,
# 315, 270, 0, 0.5, 0.707106781186548, 0.5, 0), .Dim = c(4L, 2L
# ))), list(structure(c(315, 225, 0, 0.707106781186548, 0.707106781186548,
# 0), .Dim = c(3L, 2L)), structure(c(45, 135, 0, 0.707106781186548,
# 0.707106781186548, 0), .Dim = c(3L, 2L))), list(structure(c(90,
# 135, 225, 0.5, 0.707106781186548, 0.707106781186548), .Dim = c(3L,
# 2L))), list(structure(c(90, 135, 225, 0.5, 0.707106781186548,
# 0.707106781186548), .Dim = c(3L, 2L)), structure(c(45, 90, 270,
# 0.707106781186548, 0.5, 0.5), .Dim = c(3L, 2L))), list(structure(c(90,
# 135, 225, 0.5, 0.707106781186548, 0.707106781186548), .Dim = c(3L,
# 2L)), structure(c(45, 90, 0, 0.707106781186548, 0.5, 0), .Dim = c(3L,
# 2L))), list(structure(c(135, 270, 315, 0.707106781186548, 0.5,
# 0.707106781186548), .Dim = c(3L, 2L))), list(structure(c(180,
# 225, 0, 0.5, 0.707106781186548, 0), .Dim = c(3L, 2L)), structure(c(45,
# 90, 0, 0.707106781186548, 0.5, 0), .Dim = c(3L, 2L)), structure(c(0,
# 0, 270, 0.5, 0, 0.5), .Dim = c(3L, 2L))), list(structure(c(0,
# 315, 270, 0.25, 0.707106781186548, 0.5), .Dim = c(3L, 2L)), structure(c(270,
# 180, 225, 0.5, 0.25, 0.707106781186548), .Dim = c(3L, 2L))),
# list(structure(c(0, 45, 315, 0.25, 0.707106781186548, 0.707106781186548
# ), .Dim = c(3L, 2L)), structure(c(180, 135, 225, 0.25, 0.707106781186548,
# 0.707106781186548), .Dim = c(3L, 2L))), list(structure(c(0,
# 45, 315, 0.25, 0.707106781186548, 0.707106781186548), .Dim = c(3L,
# 2L)), structure(c(180, 135, 225, 0.25, 0.707106781186548,
# 0.707106781186548), .Dim = c(3L, 2L)), structure(c(180, 90,
# 0, 270, 0.25, 0.5, 0.25, 0.5), .Dim = c(4L, 2L))), list(structure(c(0,
# 90, 180, 270, 0.25, 0.25, 0.25, 0.25), .Dim = c(4L, 2L))),
# list(structure(c(0, 45, 315, 0.25, 0.707106781186548, 0.707106781186548
# ), .Dim = c(3L, 2L)), structure(c(180, 135, 225, 0.25, 0.707106781186548,
# 0.707106781186548), .Dim = c(3L, 2L)), structure(c(270, 225,
# 315, 0.25, 0.707106781186548, 0.707106781186548), .Dim = c(3L,
# 2L)), structure(c(90, 135, 45, 0.25, 0.707106781186548, 0.707106781186548
# ), .Dim = c(3L, 2L))), list(structure(c(315, 225, 0, 0.707106781186548,
# 0.707106781186548, 0), .Dim = c(3L, 2L)), structure(c(0,
# 90, 180, 0.5, 0.5, 0.5), .Dim = c(3L, 2L))), list(structure(c(135,
# 270, 315, 0.707106781186548, 0.5, 0.707106781186548), .Dim = c(3L,
# 2L)), structure(c(225, 90, 45, 0.707106781186548, 0.5, 0.707106781186548
# ), .Dim = c(3L, 2L))), list(structure(c(90, 135, 225, 0.5,
# 0.707106781186548, 0.707106781186548), .Dim = c(3L, 2L)),
# structure(c(315, 45, 270, 0.707106781186548, 0.707106781186548,
# 0.5), .Dim = c(3L, 2L))), list(structure(c(0, 45, 315,
# 0.25, 0.707106781186548, 0.707106781186548), .Dim = c(3L,
# 2L)), structure(c(180, 135, 225, 0.25, 0.707106781186548,
# 0.707106781186548), .Dim = c(3L, 2L)), structure(c(270, 225,
# 315, 0.25, 0.707106781186548, 0.707106781186548), .Dim = c(3L,
# 2L)), structure(c(90, 135, 45, 0.25, 0.707106781186548, 0.707106781186548
# ), .Dim = c(3L, 2L)), structure(c(0, 90, 180, 270, 0.25,
# 0.25, 0.25, 0.25), .Dim = c(4L, 2L))), list(structure(c(0,
# 90, 180, 270, 270, 180, 90, 0, 0.5, 0.5, 0.5, 0.5, 0.25,
# 0.25, 0.25, 0.25), .Dim = c(8L, 2L))), list(structure(c(0,
# 90, 180, 270, 0.25, 0.5, 0.25, 0.5), .Dim = c(4L, 2L))),
# list(structure(c(180, 225, 0, 0.5, 0.707106781186548, 0), .Dim = c(3L,
# 2L)), structure(c(45, 90, 0, 0.707106781186548, 0.5, 0), .Dim = c(3L,
# 2L)), structure(c(0, 0, 270, 0.5, 0, 0.5), .Dim = c(3L, 2L
# )), structure(c(90, 135, 180, 0.5, 0.707106781186548, 0.5
# ), .Dim = c(3L, 2L))), list(structure(c(0, 90, 180, 270,
# 0, 0, 270, 180, 90, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.25, 0.25,
# 0.25, 0.25, 0.25), .Dim = c(10L, 2L))), list(structure(c(90,
# 45, 0, 0.5, 0.707106781186548, 0.25), .Dim = c(3L, 2L)),
# structure(c(0, 315, 270, 0.5, 0.707106781186548, 0.25
# ), .Dim = c(3L, 2L)), structure(c(270, 225, 180, 0.5,
# 0.707106781186548, 0.25), .Dim = c(3L, 2L))), list(structure(c(90,
# 45, 0, 0.5, 0.707106781186548, 0.25), .Dim = c(3L, 2L)),
# structure(c(0, 315, 270, 0.5, 0.707106781186548, 0.25
# ), .Dim = c(3L, 2L))), list(structure(c(90, 45, 0, 0.5,
# 0.707106781186548, 0.25), .Dim = c(3L, 2L)), structure(c(270,
# 225, 180, 0.5, 0.707106781186548, 0.25), .Dim = c(3L, 2L))),
# list(structure(c(90, 225, 0, 315, 0.5, 0.707106781186548,
# 0, 0.707106781186548), .Dim = c(4L, 2L))), list(structure(c(90,
# 225, 0, 315, 225, 225, 315, 0.5, 0.707106781186548, 0, 0.353553390593274,
# 0.353553390593274, 0.707106781186548, 0.707106781186548), .Dim = c(7L,
# 2L))), list(structure(c(90, 135, 180, 0.5, 0.707106781186548,
# 0.5), .Dim = c(3L, 2L)), structure(c(270, 315, 0, 0.5, 0.707106781186548,
# 0.5), .Dim = c(3L, 2L))), list(structure(c(90, 135, 180,
# 180, 0.5, 0.707106781186548, 0.5, 0.25), .Dim = c(4L, 2L)),
# structure(c(270, 315, 0, 0, 0.5, 0.707106781186548, 0.5,
# 0.25), .Dim = c(4L, 2L))), list(structure(c(0, 45, 0,
# 315, 0.5, 0.353553390593274, 0, 0.353553390593274), .Dim = c(4L,
# 2L)), structure(c(180, 135, 0, 225, 0.5, 0.353553390593274,
# 0, 0.353553390593274), .Dim = c(4L, 2L))), list(structure(c(0,
# 45, 0, 315, 0.5, 0.707106781186548, 0, 0.353553390593274), .Dim = c(4L,
# 2L)), structure(c(180, 135, 0, 225, 0.5, 0.353553390593274,
# 0, 0.707106781186548), .Dim = c(4L, 2L))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment