Function Repository Resource:

ImageColorFunction

Source Notebook

Create a color function from colors in an image

Contributed by: Mark Greenberg

ResourceFunction["ImageColorFunction"][img]

gives a color function based on the colors in img.

Details and Options

ResourceFunction["ImageColorFunction"] works with 2D and 3D images.
ResourceFunction["ImageColorFunction"] accepts 1-channel, 3-channel and 4-channel images, outputting a function that returns RGBColor values.
ResourceFunction["ImageColorFunction"] can take the following options:
SampleCountAutomaticif a positive integer, the maximum number of key or indexed colors
SampleOrder"Dominance"order of the key or indexed colors
SmoothedTruewhether key colors are evenly spaced (True) or spaced according to dominance (False)
IndexedFalsewhether the color function is continuous (False) or discrete (True)
For SampleCountn, continuous functions will use n or the number of dominant colors in img, whichever is lowest; discrete functions will use n.
Possible values for the SampleOrder option are as follows:
"Dominance"most dominant color (0) to least dominant (1)
"Red"least red (0) to most red (1)
"Green"least green (0) to most green (1)
"Blue"least blue (0) to most blue (1)
"Hue"least h (0) to greatest h (1) in Hue[h]
"Saturation"lowest saturation (0) to highest saturation (1)
"Brightness"darkest color (0) to brightest color (1)

Examples

Basic Examples (2) 

Make a color function from an image:

In[1]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/57d4de70-9d3f-4c23-ba81-76eb3b32ecd0"]
Out[1]=

Use the color function in a graphics expression:

In[2]:=
RegionPlot[True, {x, 0, 1}, {y, 0, 1}, ColorFunction -> f, Frame -> False, Axes -> {True, False}, AspectRatio -> .1, ImageSize -> 500]
Out[2]=

Options (2) 

Change the number of key colors in the color function:

In[3]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/795c99c1-0d5f-49d3-986a-c6b18f38f015"]
Out[3]=

Change the order of key colors in the color function:

In[4]:=
o = {"Dominance", "Red", "Green", "Blue", "Hue", "Saturation", "Brightness"};
Column[Table[
  RegionPlot[True, {x, 0, 1}, {y, 0, 1}, ColorFunction -> ResourceFunction["ImageColorFunction"][img, SampleOrder -> o[[i]]], Frame -> False, Axes -> False, AspectRatio -> .1, ImageSize -> 240], {i, 1, 7}]]
Out[4]=

Change the spacing of key colors in the color function:

In[5]:=
Column[Table[
  RegionPlot[True, {x, 0, 1}, {y, 0, 1}, ColorFunction -> ResourceFunction["ImageColorFunction"][img, Smoothed -> tf], Frame -> False, Axes -> False, AspectRatio -> .1, ImageSize -> 240], {tf, {True, False}}]]
Out[5]=

Make a discrete color function:

In[6]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/4266cc27-96e1-4ad3-a4a6-97e62f68ad48"]

Use the function as an indexed color scheme:

In[7]:=
Plot[Evaluate[Table[Sin[x + Pi n/4], {n, 0, 7}]], {x, 0, 2 Pi}, Filling -> Axis, PlotStyle -> f2[1 ;; 6]]
Out[7]=

Publisher

Mark Greenberg

Version History

  • 1.0.0 – 11 November 2019

Related Resources

License Information