% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ipea_contrast_color.R
\name{ipea_contrast_color}
\alias{ipea_contrast_color}
\title{Contrasting text colour for labels drawn over a fill colour}
\usage{
ipea_contrast_color(background)
}
\arguments{
\item{background}{A character vector of colours (hex codes or R colour
names) used as the fill/background behind the label.}
}
\value{
A character vector the same length as \code{background}, containing
\code{"white"} or \code{"black"}.
}
\description{
Given one or more fill/background colours, returns \code{"white"}
or \code{"black"}, whichever gives better contrast against that background.
Useful for value labels drawn inside bars or over points/lines, matching
the Ipea editorial guidelines for graphics, which alternate the colour of
data labels according to how dark the colour behind them is. See
\url{https://www.ipea.gov.br/manualeditorial/padroes/padroes-grafico-visuais/ilustracoes-pi.html}.
}
\examples{
ipea_contrast_color(c("#29112F", "#D2A0C9"))

library(ggplot2)
df <- data.frame(grupo = c("A", "B"), valor = c(22.6, 4.7),
                  cor = c("#29112F", "#D2A0C9"))
ggplot(df, aes(grupo, valor, fill = cor, label = valor)) +
  geom_col() +
  geom_text(aes(color = ipea_contrast_color(cor)), size = ipea_label_size(),
            fontface = "bold") +
  scale_fill_identity() +
  scale_color_identity() +
  theme_ipea()
}
\seealso{
Other ggplot2 theme functions:
\code{\link[=ipea_label_size]{ipea_label_size()}},
\code{\link[=scale_color_ipea]{scale_color_ipea()}},
\code{\link[=scale_fill_ipea]{scale_fill_ipea()}},
\code{\link[=theme_ipea]{theme_ipea()}}
}
\concept{ggplot2 theme functions}
