nav_bar <- objects[[2]]
str(nav_bar)
#> List of 13
#> $ id : chr "49:3"
#> $ name : chr "navbar"
#> $ type : chr "RECTANGLE"
#> $ scrollBehavior : chr "SCROLLS"
#> $ blendMode : chr "PASS_THROUGH"
#> $ absoluteBoundingBox :List of 4
#> ..$ x : num -307
#> ..$ y : num -286
#> ..$ width : num 1919
#> ..$ height: num 67
#> $ absoluteRenderBounds:List of 4
#> ..$ x : num -307
#> ..$ y : num -286
#> ..$ width : num 1919
#> ..$ height: num 67
#> $ constraints :List of 2
#> ..$ vertical : chr "TOP"
#> ..$ horizontal: chr "LEFT"
#> $ fills :List of 1
#> ..$ :List of 3
#> .. ..$ blendMode: chr "NORMAL"
#> .. ..$ type : chr "SOLID"
#> .. ..$ color :List of 4
#> .. .. ..$ r: num 0.153
#> .. .. ..$ g: num 0.502
#> .. .. ..$ b: num 0.89
#> .. .. ..$ a: num 1
#> $ strokes : list()
#> $ strokeWeight : num 1
#> $ strokeAlign : chr "INSIDE"
#> $ effects : list()
Getting the background color
Now we have the name of the CSS selector, we can build a “background
color” processor, to build the background-color
CSS
attribute. The color of a object is usually in the fills
element. All colors in Figma are translated into a RGBA
(red-green-blue-alpha) configuration.
This means that every “color” element in Figma API’s data will always
have four other elements inside of it: r
(for
red), g
(for green), b
(for
blue) and a
(for alpha). Each one of
these will store a real number from 0 to 1. You want to convert these
four numbers into a hex code that represents this color, and to do that
we could use the rgb()
function.
bck_color <- function(object) {
color <- object$fills[[1]][["color"]]
as_hex <- rgb(
color$r, color$g, color$b, color$a,
maxColorValue = 1
)
return(as_hex)
}
bck_color(nav_bar)
#> [1] "#2780E3FF"
Building an attribute processor
Now, we want to build a function to transform a list of CSS
attributes (or a list of key-value pairs) into a CSS statement (or a CSS
declaration if you prefer). In more details, this function will output
CSS code in the following format:
selector {
key=value;
...more CSS attributes
}
This function will receive a selector as first input. This is the CSS
selector to be used in the CSS statement. The function will put this
selector at the beginning of the statement, right before the opening
curly brace.
Furthermore, the function will receive as second input, a list of CSS
attributes to be added to the body of the CSS statement. Each element of
this list, is another list with two elements (key
and
value
).
css_statement <- function(selector, attributes) {
key_values <- vector("character", length(attributes))
for (i in seq_along(attributes)) {
key <- attributes[[i]][["key"]]
value <- attributes[[i]][["value"]]
kv <- sprintf("\t%s: %s;", key, value)
key_values[i] <- kv
}
body <- paste(key_values, collapse = "\n")
first_line <- sprintf("%s {", selector)
# Add curly braces
body <- paste(
first_line, body, "}",
sep = "\n", collapse = ""
)
return(body)
}
With this css_statement()
function, I can easily build
CSS code like this:
attrs <- list(
list(key = "text-align", value = "center"),
list(key = "color", value = "purple"),
list(key = "width", value = "100px")
)
css_statement("body", attrs) |> cat()
#> body {
#> text-align: center;
#> color: purple;
#> width: 100px;
#> }
The CSS code for background color
At last, we can use the css_bck_color()
function to
build the necessary CSS code to specify the background color of the
navbar
HTML element, of our Quarto website. In the example
below, I’m exposing the output CSS code directly in the terminal with
cat()
. But, you could use cat()
to save this
CSS code into a file, e.g. cat(file = "style.css")
.
css_bck_color <- function(object) {
selector <- css_selector(object)
color <- bck_color(object)
color_specs <- list(
list(key = "background-color", value = color)
)
css_statement <- css_statement(selector, color_specs)
return(css_statement)
}
nav_bar |>
css_bck_color() |>
cat()
#> .navbar {
#> background-color: #2780E3FF;
#> }