Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Last active October 24, 2024 19:45

Revisions

  1. johnbaums revised this gist Jun 9, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,15 @@
    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)
  2. johnbaums revised this gist Jun 9, 2015. 1 changed file with 15 additions and 20 deletions.
    35 changes: 15 additions & 20 deletions iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,30 @@
    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) {
    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 lightness (0-100)
    # lmax: upper bound of lightness (0-100)
    # 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))))
    @@ -45,8 +44,4 @@ iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
    swatch(hex(LAB(clus$centers)))
    }
    hex(LAB(clus$centers))
    }

    # Examples:
    # iwanthue(5)
    # iwanthue(5, hmin=200, hmax=300, plot=TRUE)
    }
  3. johnbaums revised this gist Nov 23, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@ swatch <- function(x) {

    iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
    plot=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)
    @@ -22,7 +23,9 @@ iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
    # plot: plot a colour swatch?
    require(colorspace)
    stopifnot(hmin >= 0, cmin >= 0, lmin >= 0,
    hmax <= 360, cmax <= 180, lmax <= 100, n > 0)
    hmax <= 360, cmax <= 180, lmax <= 100,
    hmin <= hmax, cmin <= cmax, lmin <= lmax,
    n > 0)
    lab <- LAB(as.matrix(expand.grid(seq(0, 100, 1),
    seq(-100, 100, 5),
    seq(-110, 100, 5))))
  4. johnbaums revised this gist Nov 23, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -30,11 +30,11 @@ iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
    hmax != 360 || cmax != 180 || lmax != 100))) {
    hcl <- as(lab, 'polarLUV')
    hcl_coords <- coords(hcl)
    hcl <- hcl[-which(is.na(hcl_coords[, 2]))]
    lab <- as(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),],
    'LAB')
    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)
  5. johnbaums revised this gist Nov 23, 2014. 2 changed files with 12 additions and 11 deletions.
    12 changes: 12 additions & 0 deletions iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,15 @@
    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) {
    # n: number of colours
    11 changes: 0 additions & 11 deletions swatch.R
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    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)
  6. johnbaums renamed this gist Nov 23, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. johnbaums revised this gist Nov 23, 2014. 2 changed files with 5 additions and 5 deletions.
    2 changes: 1 addition & 1 deletion iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
    lab <- lab[which(!is.na(hex(lab))), ]
    clus <- kmeans(coords(lab), n, iter.max=50)
    if (isTRUE(plot)) {
    swatches(hex(LAB(clus$centers)))
    swatch(hex(LAB(clus$centers)))
    }
    hex(LAB(clus$centers))
    }
    8 changes: 4 additions & 4 deletions swatches.R
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    swatches <- function(x) {
    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:
    # swatches(colours()[1:10])
    # swatches(iwanthue(5))
    # swatches(1:4)
    # swatch(colours()[1:10])
    # swatch(iwanthue(5))
    # swatch(1:4)
  8. johnbaums revised this gist Nov 23, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion swatches.R
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@ swatches <- function(x) {

    # Example:
    # swatches(colours()[1:10])
    # swatches(iwanthue(5))
    # swatches(iwanthue(5))
    # swatches(1:4)
  9. johnbaums revised this gist Nov 23, 2014. 2 changed files with 14 additions and 5 deletions.
    6 changes: 5 additions & 1 deletion iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,8 @@ iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
    swatches(hex(LAB(clus$centers)))
    }
    hex(LAB(clus$centers))
    }
    }

    # Examples:
    # iwanthue(5)
    # iwanthue(5, hmin=200, hmax=300, plot=TRUE)
    13 changes: 9 additions & 4 deletions swatches.R
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    swatches <- function(x) {
    par(mar=c(1, 5, 1, 1))
    barplot(rep(1, length(x)), col=x, space = 0.1, axes=FALSE, names.arg=x,
    cex.names=0.8, horiz=T, las=1)
    }
    # 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:
    # swatches(colours()[1:10])
    # swatches(iwanthue(5))
  10. johnbaums renamed this gist Nov 23, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. johnbaums created this gist Nov 23, 2014.
    5 changes: 5 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    swatches <- function(x) {
    par(mar=c(1, 5, 1, 1))
    barplot(rep(1, length(x)), col=x, space = 0.1, axes=FALSE, names.arg=x,
    cex.names=0.8, horiz=T, las=1)
    }
    33 changes: 33 additions & 0 deletions iwanthue.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    iwanthue <- function(n, hmin=0, hmax=360, cmin=0, cmax=180, lmin=0, lmax=100,
    plot=FALSE) {
    # 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 lightness (0-100)
    # lmax: upper bound of lightness (0-100)
    # plot: plot a colour swatch?
    require(colorspace)
    stopifnot(hmin >= 0, cmin >= 0, lmin >= 0,
    hmax <= 360, cmax <= 180, lmax <= 100, n > 0)
    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(is.na(hcl_coords[, 2]))]
    lab <- as(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),],
    'LAB')
    }
    lab <- lab[which(!is.na(hex(lab))), ]
    clus <- kmeans(coords(lab), n, iter.max=50)
    if (isTRUE(plot)) {
    swatches(hex(LAB(clus$centers)))
    }
    hex(LAB(clus$centers))
    }