# How to extract colors from ColorsNear?

Is there any way to get a discrete list of samples from the ColorsNear function? I have tried

ColorsNear[hexToRGB["#9581FD"], .5, ColorDistance -> "CIE2000"] //
Subdivide[8]


where

hexToRGB =
RGBColor @@ (IntegerDigits[#~StringDrop~1~FromDigits~16, 256, 3]/
255.) &;


But it doesn't return anything meaningful.

I'm guessing what is being returned is some sort of gradient object (idk new to mathematica) but it still seems plausable to take samples from this.

Just want to add to creidhne's answer that to get a list of nearest colors in order I use the following

cp = ChromaticityPlot3D[
ColorsNear[RGBColor["#9981df"], .1, "CIE2000"], "LAB",
PlotRange -> All]
R = DiscretizeGraphics[cp]
Graphics3D[{PointSize[Tiny], Point[RandomPoint[R, 5000]]},
Boxed -> False]
c = LABColor /@ RandomPoint[R, 50];
t = FindShortestTour[c,
DistanceFunction -> (ColorDistance[#1, #2,
DistanceFunction -> "CIE2000"] &)][[2]];
c[[t]]
Nearest[c, RGBColor["#72a1d8"], 10,
DistanceFunction -> (ColorDistance[#1, #2,
DistanceFunction -> "CIE2000"] &)]

• ColorsNear returns a central color and a "ball" or radius about that central color. So you shouldn't expect a discrete list (it's more like a region). What you are trying to do? – bill s Apr 8 '20 at 15:06
• right, so could I take a slice of the sphere, and extract a set of colors on the perimeter? Essentially I want a look at what colors are available at a fixed distance away from another color. I realize we are talking about a 3d object but foremost I want the farthest points(the points along the perimeter a radius r away) but if I could get the gradient out to that radius and extract from that would be ideal. Hopefully that made sense. – skyfire Apr 8 '20 at 15:14
• Presumably I could then tweak L, a, b to find better a better look at the colors I am looking for. – skyfire Apr 8 '20 at 15:16
• If you have some way of manipulating the "ball" that would suffice. I could then maybe look at taking a directional derivative or something to work with it to find properties of interest such as the fastest direction towards saturation, or w/e. The examples wolfram gives don't give alot to work with. – skyfire Apr 8 '20 at 15:23
• ColorsNear is giving you the ball, which varies depending on color space (LAB, RGB, etc). If you want to look at the ball, check out the second example under "Basic Examples" in the help file. It shows you how to look at the ball. – bill s Apr 8 '20 at 15:43

## 1 Answer

ChromaticityPlot3D makes a graphic display of the region that's returned by ColorsNear. In the LAB color space, the region is clipped because the distance, 0.5, includes a large gamut of colors.

ChromaticityPlot3D[ColorsNear[RGBColor["#9581FD"],0.5,"CIE2000"],"LAB",PlotRange->All]


So, let's choose a smaller distance and use its more manageable region.

cp=ChromaticityPlot3D[ColorsNear[RGBColor["#9581FD"],0.2,"CIE2000"],"LAB",PlotRange->All]


Use DiscretizeGraphics to convert the graphics object to a MeshRegion. Because ChromaticityPlot3D used the LAB color space to display the ColorsNear region, points from the region are LAB colors.

\[ScriptCapitalR] = DiscretizeGraphics[cp];


This graphic shows that RandomPoint will select points on the region (the points are on surface of the region, excluding its interior).

Graphics3D[{PointSize[Tiny],Point[RandomPoint[\[ScriptCapitalR],5000]]},Boxed-> False]


The region allows a way to find a random point on the surface of the region, or a point on the surface of the region nearest to a given point. For example,

RandomPoint[\[ScriptCapitalR]]


or to find the color nearest to RGB white,

RegionNearest[\[ScriptCapitalR],Level[ColorConvert[White,"LAB"],1]]


Again, because the ChromaticityPlot3D used the LAB color space, the points represent LAB colors. Here's a list of samples from the ColorsNear region. The colors from the random points are LAB colors; use ColorConvert to find the RGB values.

cols = LABColor @@@ RandomPoint[\[ScriptCapitalR], 10]


ColorConvert[cols, "RGB"]


• you sir are genius. – skyfire Apr 13 '20 at 20:36
• Don't suppose its possible to return say the top 3 nearest from a target color? – skyfire Apr 13 '20 at 21:08
• I tried target = ColorConvert[RGBColor["#c468d7"], "LAB"] // {#[[1]], #[[2]], #[[3]]} &; near = RegionNearest[R]; n1 = near[target]; near[n1] but n1 just returns itself. – skyfire Apr 13 '20 at 21:16
• You can use Level[ColorConvert[RGBColor["#c468d7"],"LAB"],1] to get the LAB components of the color. As it happens, Level works with any color, e.g., Level[Red,1]==={1,0,0}. – creidhne Apr 13 '20 at 22:35