Goal
I have a couple of icons (technically sRGB but mostly black-ish or white-ish with a transparent background). I want to change the color to darkblue
(say).
My attempt
Input Pic
Code
library(magick)
pic <- image_read(file.path(system.file(package = "magick"), "images", "shape_rectangle.gif"))
## find dark spot and use `image_fill`
pixels <- pic |>
image_data() |>
as.integer()
(idx <- which(pixels[, , 3] == max(pixels[, , 3]), arr.ind = TRUE)[1, ])
# row col
# 49 25
image_fill(pic, "darkblue", "+49+25")
Result
Problem
While this approach works in this simple case, I am not happy with this “solution” for my real icons. The logos can consist of several “areas” and while I could fill all dark spots, this seems to be first of all a total overkill and I assume that there is a function more suitable for this task rather than flood filling the area.
In principle I want to do exactly the same in R
as has been done on command line here: How to colorize a black-transparent PNG icon with ImageMagick
Example Logo