-
Notifications
You must be signed in to change notification settings - Fork 1
/
iWantHue.R
62 lines (60 loc) · 2.37 KB
/
iWantHue.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# from https://gist.github.com/johnbaums/45b49da5e260a9fc1cd7
# which I found via SO:
# https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r
swatch <- function(x) {
# x: a vector of colours (hex, numeric, or string)
par(mai=c(0.2, max(strwidth(x, "inch") + 0.4, na.rm = TRUE), 0.2, 0.4))
barplot(rep(1, length(x)), col=rev(x), space = 0.1, axes=FALSE,
names.arg=rev(x), cex.names=0.8, horiz=T, las=1)
}
# Example:
# swatch(colours()[1:10])
# swatch(iwanthue(5))
# swatch(1:4)
iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
plot=FALSE, random=FALSE) {
# Presently doesn't allow hmax > hmin (H is circular)
# n: number of colours
# hmin: lower bound of hue (0-360)
# hmax: upper bound of hue (0-360)
# cmin: lower bound of chroma (0-180)
# cmax: upper bound of chroma (0-180)
# lmin: lower bound of luminance (0-100)
# lmax: upper bound of luminance (0-100)
# plot: plot a colour swatch?
# random: should clustering be random? (if FALSE, seed will be set to 1,
# and the RNG state will be restored on exit.)
require(colorspace)
stopifnot(hmin >= 0, cmin >= 0, lmin >= 0,
hmax <= 360, cmax <= 180, lmax <= 100,
hmin <= hmax, cmin <= cmax, lmin <= lmax,
n > 0)
if(!random) {
if (exists(".Random.seed", .GlobalEnv)) {
old_seed <- .GlobalEnv$.Random.seed
on.exit(.GlobalEnv$.Random.seed <- old_seed)
} else {
on.exit(rm(".Random.seed", envir = .GlobalEnv))
}
set.seed(1)
}
lab <- LAB(as.matrix(expand.grid(seq(0, 100, 1),
seq(-100, 100, 5),
seq(-110, 100, 5))))
if (any((hmin != 0 || cmin != 0 || lmin != 0 ||
hmax != 360 || cmax != 180 || lmax != 100))) {
hcl <- as(lab, 'polarLUV')
hcl_coords <- coords(hcl)
hcl <- hcl[which(hcl_coords[, 'H'] <= hmax & hcl_coords[, 'H'] >= hmin &
hcl_coords[, 'C'] <= cmax & hcl_coords[, 'C'] >= cmin &
hcl_coords[, 'L'] <= lmax & hcl_coords[, 'L'] >= lmin), ]
#hcl <- hcl[-which(is.na(coords(hcl)[, 2]))]
lab <- as(hcl, 'LAB')
}
lab <- lab[which(!is.na(hex(lab))), ]
clus <- kmeans(coords(lab), n, iter.max=50)
if (isTRUE(plot)) {
swatch(hex(LAB(clus$centers)))
}
hex(LAB(clus$centers))
}