Bläddra i källkod

Replace set_text_color()

Uses the algorithm mentioned at https://stackoverflow.com/a/3943023/2022615 based on the luminance of the background color to determine black or white text. The threshold favored black text in a way that didn't look good with our plots, so I increased the threshold from 0.179 to 0.333.
pull/18/merge
Garrick Aden-Buie 7 år sedan
förälder
incheckning
0a6a02dd64
5 ändrade filer med 26 tillägg och 5 borttagningar
  1. +2
    -1
      DESCRIPTION
  2. +0
    -4
      R/plot_helpers.R
  3. +13
    -0
      R/utils.R
  4. +4
    -0
      tests/testthat.R
  5. +7
    -0
      tests/testthat/test-set_text_color.R

+ 2
- 1
DESCRIPTION Visa fil

@@ -29,7 +29,8 @@ Imports:
Suggests:
knitr,
roxygen2,
viridis
viridis,
testthat
VignetteBuilder:
knitr
Encoding: UTF-8

+ 0
- 4
R/plot_helpers.R Visa fil

@@ -1,7 +1,3 @@


set_text_color <- function(a) ifelse(apply(col2rgb(a), 2, mean) > 127, "black", "white")

#' Animates a plot
#'
#' @param d a preprocessed dataset

+ 13
- 0
R/utils.R Visa fil

@@ -0,0 +1,13 @@
set_text_color <- function(x, black = "#000000", white = "#FFFFFF") {
# x = color_hex
color_rgb <- col2rgb(x)
# modified from https://stackoverflow.com/a/3943023/2022615
# following W3 guidelines: https://www.w3.org/TR/WCAG20/#relativeluminancedef
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(color_rgb) %*% c(0.2126, 0.7152, 0.0722)
lum <- lum[,1]
# threshold is supposed to be 0.179 but 1/3 seems to work better for our plots
ifelse(lum > 1/3, black, white)
}

+ 4
- 0
tests/testthat.R Visa fil

@@ -0,0 +1,4 @@
library(testthat)
library(tidyverbs)

test_check("tidyverbs")

+ 7
- 0
tests/testthat/test-set_text_color.R Visa fil

@@ -0,0 +1,7 @@
context("test-set_text_color")

test_that("correct color selection", {
colors <- c("#FFFFFF", "#9E788C", "#B679E5", "#4BB757",
"#000000", "#0027D8", "#E6071B", "#495B3F")
expect_equal(set_text_color(colors), c(rep("#000000", 4), rep("#FFFFFF", 4)))
})

Laddar…
Avbryt
Spara