Learn R Programming

fansi (version 0.5.0)

make_styles: Generate CSS Mapping Classes to Colors

Description

Given a set of class names, produce the CSS that maps them to the default 8-bit colors. This is a helper function to generate style sheets for use in examples with either default or remixed fansi colors. In practice users will create their own style sheets mapping their classes to their preferred styles.

Usage

make_styles(classes, rgb.mix = diag(3))

Arguments

classes

a character vector of either 16, 32, or 512 class names, or a scalar integer with value 8, 16, or 256. The character vectors are described in sgr_to_html. The scalar integers will cause this function to generate classes for the basic colors (8), basic + bright (16), or all 256 8-bit colors (256), with class names in "fansi-color-###" (or "fansi-bgcol-###" for background colors), which is what sgr_to_html generates when-user defined classes are not provided. TRUE is also a valid input and is equivalent to 256.

rgb.mix

3 x 3 numeric matrix to remix color channels. Given a N x 3 matrix of numeric RGB colors rgb, the colors used in the style sheet will be rgb %*% rgb.mix. Out of range values are clipped to the nearest bound of the range.

Value

A character vector that can be used as the contents of a style sheet.

See Also

Other HTML functions: html_esc(), in_html(), sgr_to_html()

Examples

Run this code
# NOT RUN {
## Generate some class strings; order matters
classes <- do.call(paste, c(expand.grid(c("fg", "bg"), 0:7), sep="-"))
writeLines(classes[1:4])

## Some Default CSS
css0 <- "span {font-size: 60pt; padding: 10px; display: inline-block}"

## Associated class strings to styles
css1 <- make_styles(classes)
writeLines(css1[1:4])

## Generate SGR-derived HTML, mapping to classes
string <- "\033[43mYellow\033[m\n\033[45mMagenta\033[m\n\033[46mCyan\033[m"
html <- sgr_to_html(string, classes=classes)
writeLines(html)

## Combine in a page with styles and display in browser
# }
# NOT RUN {
in_html(html, css=c(css0, css1))
# }
# NOT RUN {
## Change CSS by remixing colors, and apply to exact same HTML
mix <- matrix(
  c(
    0, 1, 0,  # red output is green input
    0, 0, 1,  # green output is blue input
    1, 0, 0   # blue output is red input
  ),
  nrow=3, byrow=TRUE
)
css2 <- make_styles(classes, rgb.mix=mix)
## Display in browser: same HTML but colors changed by CSS
# }
# NOT RUN {
in_html(html, css=c(css0, css2))
# }

Run the code above in your browser using DataLab