Skip to content

Commit

Permalink
improved printing
Browse files Browse the repository at this point in the history
  • Loading branch information
CoryMcCartan committed Feb 28, 2022
1 parent 7942f80 commit 52d5b77
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ Imports:
ggplot2,
scales
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0),
cli,
rstudioapi
colorspace,
viridis,
rstudioapi
knitr,
rmarkdown,
testthat (>= 3.0.0)
License: MIT + file LICENSE
URL: https://github.com/CoryMcCartan/wacolors
Encoding: UTF-8
Expand Down
5 changes: 3 additions & 2 deletions R/colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ cont_pal = c("sound_sunset", "ferries", "forest_fire", "sea_star", "sea",
#' to automatically interpolate between colors.
#' @param reverse `TRUE` if palette should be reversed.
#'
#' @return A vector of colors of type `palette`. If printed to the console, will
#' plot the palette as well.
#' @return A vector of colors of type `palette`. Use the `plot()` function to
#' plot the palette. If the `cli` package is installed, printing the palette
#' to the console will also show its colors.
#'
#' @examples
#' wa_pal("rainier")
Expand Down
33 changes: 20 additions & 13 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,31 @@ match_pal = function(name) {
list(pal=pal, name=name)
}

# `print.palette` modified from that in `wesanderson` (c) 2016 Karthik Ram
#' @export
#' @importFrom graphics rect par image text
print.palette = function(x, ...) {
n <- length(x)
old <- par(mar=c(0.5, 0.5, 0.5, 0.5))
on.exit(par(old))

image(1:n, 1, as.matrix(1:n), col=x,
ylab="", xaxt="n", yaxt="n", bty="n")
x_print = unclass(x)
if (!is.null(pal_name <- attr(x, "name", exact=TRUE))) {
cat("`", pal_name, "`\n", sep="")
attr(x_print, "name") = NULL
}
print(x_print)

rect(0, 0.9, n + 1, 1.1, col=grDevices::rgb(1, 1, 1, 0.8), border=NA)
text((n + 1) / 2, 1, labels=attr(x, "name"), col="black", cex=1, font=2)
if (requireNamespace("cli", quietly=TRUE)) {
n_colors = cli::num_ansi_colors()
if (n_colors >= 256) {
if (n_colors < 256^3/2) cat("approximate palette:\n")
block = "\u00A0\u00A0"
cat(" ")
for (color in x) {
cat(cli::make_ansi_style(color, bg=TRUE)(block))
cat(" ")
}
}
}

if (!is.null(names(x)))
text(1:n, 1.25, labels=names(x), col="black", cex=1)
cat("\n")

print(unclass(x))
invisible(x)
}

Expand All @@ -53,7 +60,7 @@ plot.palette = function(x, ...) {
ylab="", xaxt="n", yaxt="n", bty="n")

rect(0, 0.9, n + 1, 1.1, col=grDevices::rgb(1, 1, 1, 0.8), border=NA)
text((n + 1) / 2, 1, labels=attr(x, "name"), col="black", cex=1, font=2)
text((n + 1) / 2, 1, labels=attr(x, "name", exact=TRUE), col="black", cex=1, font=2)

if (!is.null(names(x)))
text(1:n, 1.25, labels=names(x), col="black", cex=1)
Expand Down
5 changes: 3 additions & 2 deletions man/wa_pal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 52d5b77

Please sign in to comment.