#!/usr/bin/env Rscript

# Load libraries
library(RColorBrewer)

# Set parameters
set.seed(20110625)
s <- 5 # Size of each block in pixels
height <- 150 # Height of plot; set to Wordpress standard
width <- 945 # Width of plot; set to Wordpress standard

p <- 5          # Number of treatments
n <- width / s  # Number of blocks
m <- height / s # Replicates per block

# Utility functions

# Draw a maximally-balanced sample
balancedSample <- function(n, m) {
    # n is the total number of items to sample from
    # m is the maximal size of each sample
    s <- rep(seq(m), floor(n/m))
    s <- sample(s)
    s <- c(s, sample(seq(m), n - m*floor(n/m)))
    return(s)
}

# Generate design
d <- sapply(1:n, function(i) balancedSample(m, p))

# Build plot
png("images/header.png", width=width, height=height)
#
palette(brewer.pal(n=5, name='Reds'))
#
par(mar=c(0,0,0,0), oma=c(0,0,0,0))
plot(c(0,width), c(0,height), type='n', axes=FALSE, ann=FALSE,
     asp=1)
# 
j <- 1:m - 1
for (i in 1:n - 1) {
    rect(xleft=i*s, xright=(i+1)*s,
         ybottom=j*s, ytop=(j+1)*s,
         col=d[,i+1])
}
#
dev.off()

