Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export(cbreaks)
export(censor)
export(col2hcl)
export(col_bin)
export(col_contrasting)
export(col_darker)
export(col_factor)
export(col_lighter)
Expand Down
22 changes: 22 additions & 0 deletions R/colour-manip.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ col_mix.scales_pal <- function(a, b, amount = 0.5, space = "rgb") {
wrap_col_adjustment(a, col_mix, list(b = b, amount = amount, space = space))
}

#' Choose a contrasting colour
#'
#' Chooses either `light` or `dark` based on the lightness of `colour`.
#' This is useful for choosing a text colour that is legible on a solid background
#' cooured with `colour`.
#'
#' @param colour character vector of colours to be modified
#' @param light colour to return when `colour` is dark
#' @param dark colour to return when `colour` is light
#'
#' @return A character vector of colours with the same length as `colour`
#' @family colour manipulation
#' @export
#'
#' @examples
#' col_contrasting(c("navy", "white", "black", "yellow"))
#'
col_contrasting <- function(colour, light = "white", dark = "black") {
lab <- farver::decode_colour(colour, to = "lab")
ifelse(lab[, 1] < 50, light, dark)
}

#' Colour manipulation
#'
#' These are a set of convenience functions for standard colour manipulation
Expand Down
1 change: 1 addition & 0 deletions man/alpha.Rd

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

1 change: 1 addition & 0 deletions man/col2hcl.Rd

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

36 changes: 36 additions & 0 deletions man/col_contrasting.Rd

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

1 change: 1 addition & 0 deletions man/col_mix.Rd

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

1 change: 1 addition & 0 deletions man/colour_manip.Rd

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

1 change: 1 addition & 0 deletions man/muted.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-colour-manip.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ test_that("colour manipulation functions work on palettes", {
expect_equal(col_saturate(pal, -50)(3), c("#BF4040", "#40BF40", "#4040BF"))
expect_equal(col_mix(pal, "white")(3), c("#FF8080", "#80FF80", "#8080FF"))
})

# col_contrasting ------------------------------------------------

test_that("col_contrasting chooses suitable colours", {
colours <- c("navy", "white", "black", "yellow")
expect_equal(col_contrasting(colours), c("white", "black", "white", "black"))
})
Loading