I am attempting to create color palettes based on several pictures, namely album covers.
I'm using the LastFM API to acquire the album covers and I'm using RImagePalette to generate the palettes.
However, when trying to do this for certain pictures, specifically darker ones it seems to stop working and I get and error saying:
Error in rgb(choice(x$red), choice(x$green), choice(x$blue)) :
color intensity nan, not in [0,1]
I took a stab at reading the source code, I think this is basically a bug and you should report it to the package author.
Specifically, the package considers each channel (red, green, blue) and computes the min, max, "extent" (max-min) and median. Then it takes the channel with the greatest extent and takes its median as a threshold.
The problem with your second image is the red channel has a lot of pixels (more than half) exactly = 0. So its median is 0. But since its min is also 0 (whereas the other channels have only non-0 pixels), it's also the channel with the greatest extent! So the package attempts to use a threshold of 0, bad things ensue.
potentially you could inject enough noise to avoid the median=0 problem, but not too much that you change the results. You would need to fine-tune that.
But ultimately I'd say it's a problem in the package logic: the author probably didn't consider this particular edge-case (I think mostly your images are much smaller than his test cases), and might be able to change something in the computation to get a result that makes sense. You could try opening a Github issue, though it doesn't seem like the author has been active on Github recently.