Просмотр исходного кода

Factor light-dark color test out of choose_dark_or_light()

tags/v0.3.0
Garrick Aden-Buie 7 лет назад
Родитель
Сommit
bfea283b54
2 измененных файлов: 11 добавлений и 8 удалений
  1. +9
    -6
      R/utils.R
  2. +2
    -2
      man/choose_dark_or_light.Rd

+ 9
- 6
R/utils.R Просмотреть файл

@@ -52,23 +52,26 @@ adjust_value_color <- function(color_hex, strength = 0.5) {
#' best contrast. Follows W3C Recommendations.
#'
#' @references <https://stackoverflow.com/a/3943023/2022615>
#' @param x The background color
#' @param x The background color (hex)
#' @param black Text or foreground color, e.g. "#222" or
#' `substitute(darken_color(x, 0.8))`, if black text provides the best contrast.
#' @param white Text or foreground color or expression, e.g. "#EEE" or
#' `substitute(lighten_color(x, 0.8))`, if white text provides the best contrast.
#' @export
choose_dark_or_light <- function(x, black = "#000", white = "#FFF") {
# x = color_hex
# black <- substitute(black)
# white <- substitute(white)
choose_dark_or_light <- function(x, black = "#000000", white = "#FFFFFF") {
if (is_light_color(x)) eval(black) else eval(white)
}

is_light_color <- function(x) {
# this function returns TRUE if the given color
# is light-colored and requires dark text
color_rgb <- col2rgb(x)[, 1]
# from https://stackoverflow.com/a/3943023/2022615
color_rgb <- color_rgb / 255
color_rgb[color_rgb <= 0.03928] <- color_rgb[color_rgb <= 0.03928]/12.92
color_rgb[color_rgb > 0.03928] <- ((color_rgb[color_rgb > 0.03928] + 0.055)/1.055)^2.4
lum <- t(c(0.2126, 0.7152, 0.0722)) %*% color_rgb
if (lum[1, 1] > 0.179) eval(black) else eval(white)
lum[1, 1] > 0.179
}

#' @keywords internal

+ 2
- 2
man/choose_dark_or_light.Rd Просмотреть файл

@@ -4,10 +4,10 @@
\alias{choose_dark_or_light}
\title{Choose dark or light color}
\usage{
choose_dark_or_light(x, black = "#000", white = "#FFF")
choose_dark_or_light(x, black = "#000000", white = "#FFFFFF")
}
\arguments{
\item{x}{The background color}
\item{x}{The background color (hex)}

\item{black}{Text or foreground color, e.g. "#222" or
\code{substitute(darken_color(x, 0.8))}, if black text provides the best contrast.}

Загрузка…
Отмена
Сохранить