0

I would like to know if that is possible to reproduce a color palette from an image.

Here is an example. Pages 1 and 3 show a rainbow palette in the figure legend.

The software is proprietary and has very little room for customization. I would like to regenerate the legend by R (or any other programming language, e.g. python) but retain the scale/ range of the colors.

I recognize that some color picker tools are available. However, the resolution of the palette is quite low, and often 3-4 colors are displayed in the same row. I am not sure which color I should pick in the raster image.

William Wong
  • 453
  • 2
  • 9

2 Answers2

1

Base R has a rainbow palette, which is similar to what is shown in the linked PDF

A better option might be the turbo color map in the viridis package, which transitions more gradually across the visible spectrum.

This PDF is a great general reference to colors in R, and may help you construct the gradient you're aiming to replicate.

In ggplot2, you can create these sorts of palettes using scale_gradient (link).

library(ggplot2)
library(scales)

  ggplot(mtcars, aes(factor(cyl), disp, fill = mpg)) +
  geom_col() +
  scale_fill_gradientn(colors = c(muted('red'), 'red', 'yellow1'))

Created on 2023-04-19 with reprex v2.0.2

Seth
  • 1,659
  • 1
  • 4
  • 11
  • Thanks! Indeed, I also want the "YellowHot" palette from them in addition to the more common rainbow palette. An example is [here](https://www.researchgate.net/figure/Visualization-of-ICG-fluorescence-A-ICG-is-readily-visualized-with-a-commercially_fig5_340586041). I cannot find a palette that is from "brown" to "yellow". – William Wong Apr 19 '23 at 19:17
  • 1
    I've edited the answer to include one way to construct custom gradient palettes. – Seth Apr 19 '23 at 22:38
0

You can use OpenCV (cv2) library and export the color data as a histogram. It's also possible to apply a mask to a certain area of the image to extract the color data from that part.

For examples/documentation you can find more here: Histograms - 1 : Find, Plot, Analyze and this one might suite you a bit more where it shows how to create 2D histograms: Histograms - 3 : 2D Histograms

Hope this helps!

jooleer
  • 14
  • 3