recolorPhylopic: Add transparency and/or change color of a picture

Description Usage Arguments Value Author(s) Examples

View source: R/recolorPhylopic.R

Description

This function adds transparency and/or change color of a picture (array).

Usage

1
recolorPhylopic(img, alpha = 0.2, color = NULL)

Arguments

img

array a picture loaded by png::readPNG().

alpha

numeric transparency factor between 0 (transparent) and 1 (opaque).

color

array a color name (e.g. "red", "steelblue") or an hexadecimal value (e.g. "#ff0000")

Value

This function returns a picture matrix with colors and/or transparency altered. This matrix can be plotted with the function graphics::rasterImage() (see examples).

Author(s)

Nicolas CASAJUS, nicolas.casajus@fondationbiodiversite.com

Nicolas LOISEAU, nicolas.loiseau1@gmail.com

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
# Download Rlogo in png format
download.file(
  url      = "https://www.r-project.org/logo/Rlogo.png",
  destfile = "Rlogo.png"
)

# Import Rlogo (require `png` package)
rlogo <- png::readPNG("Rlogo.png")

# Get image dimensions
xmax <- dim(rlogo)[2]
ymax <- dim(rlogo)[1]

# Remove figure margins
o_par <- par()
par(mar = rep(0, 4))


### ORIGINAL IMAGE   ------


# Empty plot
xmin <- 0
ymin <- xmin

plot(
  x    = xmin,
  x    = ymin,
  xlim = c(xmin, xmax), # X-axis extent (number of image rows)
  ylim = c(ymin, ymax), # Y-axis extent (number of image cols)
  asp  = 1,             # Preserve x-axis/y-axis ratio
  ann  = FALSE,         # Remove figure annotations
  axes = FALSE,         # Remove figure axes
  type = "n"            # Remove figure data
)

# Add Rlogo
graphics::rasterImage(
  image   = rlogo,
  xleft   = xmin,
  ybottom = ymin,
  xright  = xmax,
  ytop    = ymax,
  angle   = 0
)


### IMAGE WITH TRANSPARENCY   ------

# Empty plot
plot(
  x    = 0,
  xlim = c(0, xmax),
  ylim = c(0, ymax),
  asp  = 1,
  ann  = FALSE,
  axes = FALSE,
  type = "n"
)

# Add transparency
rlogo_alpha <- recolorPhylopic(rlogo, alpha = 0.25, color = NULL)

# Add Rlogo
graphics::rasterImage(
  image   = rlogo_alpha,
  xleft   = 0,
  ybottom = 0,
  xright  = xmax,
  ytop    = ymax,
  angle   = 0
)


### MONOCHROMATIC IMAGE   ------

# Empty plot
plot(
  x    = 0,
  xlim = c(0, xmax),
  ylim = c(0, ymax),
  asp  = 1,
  ann  = FALSE,
  axes = FALSE,
  type = "n"
)

# Add transparency
rlogo_blue <- recolorPhylopic(rlogo, alpha = 1, color = "steelblue")

# Add Rlogo
graphics::rasterImage(
  image   = rlogo_blue,
  xleft   = 0,
  ybottom = 0,
  xright  = xmax,
  ytop    = ymax,
  angle   = 0
)

# Delete downloaded Rlogo
file.remove("Rlogo.png")

# Reset default par()
par(o_par)

FRBCesab/ecorar documentation built on June 2, 2021, 8:18 p.m.