Skip to content

Commit

Permalink
fix user-defined colors
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Sep 14, 2023
1 parent 51e02e2 commit 7640794
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 44 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Imports:
Rcpp,
rstac (>= 0.9.2-3),
sf (>= 1.0-12),
showtext,
sysfonts,
slider (>= 0.2.0),
stats,
Expand Down
4 changes: 2 additions & 2 deletions R/api_colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
#' @noRd
#' @param color_tb A SITS color table
#' @return a gglot2 object
.colors_show <- function(color_tb) {
.colors_show <- function(color_tb, font_family) {
color_tb$name <- purrr::map_chr(color_tb$name, function(name)
{ paste(name = unlist(strsplit(name, split = "_")), collapse = " ")})
n_colors <- nrow(color_tb)
Expand Down Expand Up @@ -119,7 +119,7 @@
y = .data[["y"]] + 0.8,
label = stringr::str_wrap(.data[["name"]], width = 10)
),
family = "opensans",
family = font_family,
colour = "grey15",
hjust = 0.5,
vjust = 1,
Expand Down
19 changes: 8 additions & 11 deletions R/api_conf.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,11 @@
keep.null = FALSE
)
colors <- config_colors$colors
base_names <- names(colors)
color_table <- purrr::map2_dfr(colors, base_names, function(cl, bn) {
color_table <- purrr::map2_dfr(colors, names(colors),
function(cl, nm) {
cc_tb <- tibble::tibble(
name = names(cl),
color = unlist(cl),
group = bn
name = nm,
color = cl
)
return(cc_tb)
})
Expand Down Expand Up @@ -250,8 +249,7 @@
} else {
color_table <- tibble::add_row(color_table,
name = name,
color = col,
group = "User"
color = col
)
}
}
Expand All @@ -274,11 +272,10 @@
#' @return NULL, called for side effects
#'
.conf_set_fonts <- function() {
library(showtext)
library(sysfonts)
# verifies if sysfonts package is installed
.check_require_packages("sysfonts")
sysfonts::font_add_google("Open Sans", family = "opensans")
sysfonts::font_add_google("IBM Plex Sans", family = "ibm")
sysfonts::font_add_google("Noto Sans", family = "noto")
sysfonts::font_add_google("Roboto", family = "roboto")
return(NULL)
}
#' @title Return the user configuration set in enviromental variable
Expand Down
15 changes: 10 additions & 5 deletions R/sits_colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sits_colors <- function() {
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description Shows the default SITS colors
#' @param legend One of the accepted legend in sits
#' @param font_family A font family loaded in SITS
#'
#' @return no return, called for side effects
#'
Expand All @@ -30,16 +31,20 @@ sits_colors <- function() {
#' }
#' @export
#'
sits_colors_show <- function(legend = NULL) {
# verifies if stringr package is installed
.check_require_packages("stringr")
sits_colors_show <- function(legend = NULL,
font_family = "opensans") {
# verifies if sysfonts package is installed
.check_require_packages("sysfonts")
# checks if font family is available
if (!font_family %in% sysfonts::font_families())
font_family <- "opensans"
legends <- .conf("legends")
if (purrr::is_null(legend)) {
print("Showing all available colors")
leg <- paste0(paste("Optional - select one of the legends: "),
paste(legends, collapse = ", "))
print(leg)
g <- .colors_show(sits_colors())
g <- .colors_show(sits_colors(), font_family)
return(g)
} else {
if (legend %in% legends) {
Expand All @@ -48,7 +53,7 @@ sits_colors_show <- function(legend = NULL) {
dplyr::filter(.data[["name"]] %in% colors)
color_table_legend <- color_table_legend[
match(colors, color_table_legend$name), ]
g <- .colors_show(color_table_legend)
g <- .colors_show(color_table_legend, font_family)
return(g)
} else {
print("Selected map legend not available")
Expand Down
53 changes: 29 additions & 24 deletions inst/extdata/config_colors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# and 1970 U.S. Vegetation Map
#
class_schemes:
legends: ["IGBP", "UMD", "ESA_CCI_LC", "WORLDCOVER", "PRODES"]
legends: ["IGBP", "UMD", "ESA_CCI_LC", "WORLDCOVER", "PRODES",
"TERRA_CLASS"]

IGBP: ["Evergreen_Needleleaf_Forests", "Evergreen_Broadleaf_Forests",
"Deciduous_Needleleaf_Forests", "Deciduous_Broadleaf_Forests",
Expand Down Expand Up @@ -193,8 +194,13 @@ class_schemes:
"Mining", "Water", "Wetland",
"Urban_Area", "Cropland", "Pasture"]

TERRA_CLASS: ["Forest", "Secondary_Vegetation","Degradation",
"Deforestation",
"Cropland", "Pasture", "Cerradao", "Cerrado",
"Campo_Cerrado", "Mining", "Water", "Wetland",
"Urban_Area"]

colors:
Tropical_Forest:
# Oak-hickory-pine forest in US Veg Map
Evergreen_Broadleaf_Forest : &forest "#518940"
Evergreen_Broadleaf_Forests : *forest
Expand Down Expand Up @@ -227,7 +233,6 @@ colors:
Woody_Savannas : *woodlands
Cerradao : *woodlands

Deciduous_Tropical_Forests:
# Caatinga and decidous tropical forest
Caatinga : &caatinga "#c7ebdb"
Deciduous_Forest : *caatinga
Expand All @@ -236,14 +241,13 @@ colors:
Deciduous_Broadleaf_Forest : *caatinga
Deciduous_Broadleaf_Forests : *caatinga

Planted_Forest:
# Silviculture and Planted Forest
# Northern hardwood
Planted_Forest : &planted_forest "#90cac8"
Planted_Forests : *planted_forest
Silviculture : *planted_forest

Temperate_Forest:
# Temperate Forests
# Great lakes pine forest in US Veg Map
Evergreen_Needleleaf_Forest : &enf "#96b1b7"
Evergreen_Needleleaf_Forests : *enf
Expand All @@ -256,7 +260,6 @@ colors:
Mixed_Forests : *mixedforest
Tree_Cover_Mixed_Leaf : *mixedforest

Savanna:
# Savanna and Cerrado
# Bluestem praire in US Veg Map
Savanna : &savanna "#a2d43f"
Expand All @@ -269,7 +272,7 @@ colors:
Savanna_Parkland : *campo_cerrado
Open_Cerrado : *campo_cerrado

Grassland:
# Grasslands and similar
# California steppe
Grassland : &grassland "#f6cc41"
Grasslands : *grassland
Expand All @@ -281,7 +284,7 @@ colors:
Dense_Herbaceous : *pasture
Herbaceous_Vegetation : *pasture

Shrubland:
# Shrublands
# Based on "pumpkin" palette of Flat Design Color Chart
Closed_Shrubland : &closed_shrubland "#BA4A00"
Closed_Shrublands : *closed_shrubland
Expand All @@ -293,7 +296,7 @@ colors:
Sparse_Shrubland : *shrubland
Rocky_Savanna : *shrubland

Barren:
# Barren and Sparse Vegetation
# Based on "orange" palette of Flat Design Color Chart
Barren : &barren "#FDEBD0"
Bare_Area : *barren
Expand All @@ -304,12 +307,12 @@ colors:
Non_Vegetated_Lands : *barren
Dunes : "#FEF9E7"

Tundra:
# Tundra
# based on the "wisteria" palette of Flat Design Color Chart
Lichens_Mosses : &moss "#E8DAEF"
Moss_Lichen : *moss

Flooded_trees:
# Flooded_trees and mangroves
Mangrove : &mangrove "#D4E6F1"
Mangroves : *mangrove
Tree_Cover_Flooded_Saline_Water: *mangrove
Expand All @@ -318,7 +321,7 @@ colors:
Herbaceous_Wetland : *flooded
Shrub_Herbaceous_Cover_Flooded: *flooded

Wetland:
# Water and Wetlands
# Based on "belize hole" palette from Flat Design Color Chart
Water : &water "#2980B9"
Water_Bodies : *water
Expand All @@ -328,7 +331,7 @@ colors:
Permanent_Wetland : *wetlands
Permanent_Wetlands : *wetlands

Cropland:
# Croplands
# Based on "carrot" palette from Flat Design Color Chart
Cropland : &cropland "#F0B27A"
Croplands : *cropland
Expand Down Expand Up @@ -376,7 +379,6 @@ colors:
# Based on "turquoise" palette of Flat Design Color Chart
Rice : "#FBEEE6"

Mosaic:
# Mosaic classes
Forest_Cropland_Mosaic : &mosaic_1 "#947E13"
Mosaic_Vegetation_Cropland : *mosaic_1
Expand All @@ -387,13 +389,13 @@ colors:
Mosaic_Cropland_Vegetation: *mosaic_2
Mosaic_Herbaceous_Tree_Shrub: *mosaic_2

Deforestation:
# Deforestation - PRODES and DETER
# Secondary Vegetation
# Beech-maple forest
Secondary_Vegetation : "#9bd15e"
# Deforestation, Degradation, Mining
# Based on "Alizarin" palette from Flat Design Color Chart
Degradation : "#afc97f"
# Based on "Alizarin" palette from Flat Design Color Chart "#afc97f"
Degradation : "#D6D295"
# PRODES
# Based on "Alizarin" palette from Flat Design Color Chart
Burned_Area : &burned_area "#CD6155"
Expand All @@ -410,24 +412,27 @@ colors:
Clear_Cut_Vegetation : *clear_cut_deg
# Based on "amethyst" palette from Flat Design Color Chart
Mining : "#C39BD3"
# Deforestation_Mask #FDEBD0
Deforestation : &deforestation "#C9D6AE"
# Deforestation_Mask #FDEBD0 "#C9D6AE"
Deforestation : &deforestation "#CAD4C9"
Deforestation_Mask : *deforestation
# Based on "silver" palette from Flat Design Color Chart
Non_Forest : &non_forest "#BDC3C7"
Non_Forest : &non_forest "#C9D6AE"
Natural_Non_Forested : *non_forest
Urban:

# Urban Areas
# Based on "wet asphalt" palette from Flat Design Color Chart
Urban_Area : &urban "#85929E"
Urban_Builtup_Lands : *urban
Builtup : *urban
Snow_Ice:

# Snow and Ice
# Based on "asbestos" palette from Flat Design Color Chart
Snow_Ice : &snow "#E5E8E8"
Permanent_Snow_Ice : *snow
Snow_and_Ice : *snow
Others:
# No Class uses "silver" palette from Flat Design Color Chart

# Others
# uses "silver" palette from Flat Design Color Chart
No_Class : &no_class "#F2F3F4"
No_Data : *no_class
Unclassified : *no_class
Expand Down
4 changes: 3 additions & 1 deletion man/sits_colors_show.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7640794

Please sign in to comment.