3
$\begingroup$

Suppose that we want to color a number of objects. We start by selecting some color (randomly). Then we want the color of the next object to be significantly different from the colors already used (including the background color, say white). After a number of attempts I was unable to find any smart way to do this. Suppose that the RGBColor model is used. Then my best result was

cl = {RGBColor[1., 1., 1.]};

myFun1[colList_List, {x_?NumericQ, y_?NumericQ, z_?NumericQ}] := 
  Last[ColorDistance[colList, RGBColor[x, y, z], DistanceFunction->"CIE94"]]

oneStep1[cl_List] := 
  (Append[cl, RGBColor[x, y, z] /. 
     Last[NMaximize[
       {myFun1[cl, {x, y, z}], 1 >= x >= 0, 1 >= y >= 0, 1 >= z >= 0}, 
       {x, y, z}]]])

Nest[oneStep1, cl, 5]

Clearly, the result is not good enough (I need at leas 10 significantly different colors).

$\endgroup$
9
  • 3
    $\begingroup$ Any reason why you couldn't use a pre-defined color space? Table[ColorData["BrightBands"][i], {i, 0, 1, .1}] There are a number of both continuous and discrete sets. $\endgroup$
    – Andy Ross
    Commented Sep 29, 2015 at 13:09
  • $\begingroup$ @user18792 Isn't this the same question: mathematica.stackexchange.com/questions/38722/…? $\endgroup$
    – Michael E2
    Commented Sep 29, 2015 at 16:45
  • $\begingroup$ Or possibly this: mathematica.stackexchange.com/questions/23647/…? $\endgroup$
    – Michael E2
    Commented Sep 29, 2015 at 16:47
  • 1
    $\begingroup$ Is it really necessary to search for suitable colors one after another? I think results should be more pleasant when they're optimized all together. $\endgroup$
    – kirma
    Commented Sep 29, 2015 at 17:05
  • $\begingroup$ Predefined colors is not good for the following reasons: $\endgroup$
    – Acus
    Commented Sep 30, 2015 at 6:47

1 Answer 1

6
$\begingroup$

Here's one approach, attempting to find a sufficiently good local maximum for minimum color difference of ten colors differing from black and white, and each other:

Module[
 {sRGB, toLCH, oldcolors, ncolors, newcolorvars, newcolors, allcolors},
 sRGB = Import["sRGB_v4_ICC_preference.icc"];
 toLCH = ColorConvert[#, sRGB -> "LCH"] &;
 oldcolors = {Black, White};
 ncolors = 10;
 newcolorvars = Array[c, {ncolors, 3}];
 allcolors = Join[oldcolors, newcolorvars];
 newcolors = RGBColor @@@
   (newcolorvars /. Last@Quiet@NMaximize[
        Min[
         ColorDistance[toLCH@#1, toLCH@#2, 
            DistanceFunction -> "CIE2000"] & @@@ 
          Subsets[allcolors, {2}]],
        Flatten@newcolorvars \[Element] 
         Cuboid[Table[0, {3 ncolors}]],
        Method -> "SimulatedAnnealing", 
        MaxIterations -> 1000000000]);
 Join[oldcolors, newcolors]]

enter image description here

Pairwise comparison visualization of selected colors:

Table[Graphics@{Circle[], 
     Table[{#[[i]], 
         Disk[{0, 0}, 
          1, \[Pi]/2 - (2 \[Pi]/Length@#) {i - 1, i}]}, {i, 
         Length@#}] &@#, {i, Disk[{0, 0}, 2/3]}}, {i, #}] &@%

enter image description here

There are multiple problems in this implementation:

  • ColorDistance seems to require unexplained pre-conversion to LCH colorspace to work acceptably. In general, there's something deeply disturbing in colorspace handling Mathematica does - or doesn't seem to do, sometimes. Linear and non-linear colorspaces are not intuitively separate, or compatible, for instance.
  • NMaximize has trouble trying to find good maxima because ColorDistance is only numeric and the domain has lots of local maxima.
  • ColorDistance functions in general are not even meant to perform large color separation measures, but rather ones very close to a reference color. (Related commentary: Is ColorDistance symmetric?)
  • Min is not necessary a sensible aesthetic metric. Something like Mean@# / Variance@# & might make more sense, but is takes forever in practice.
$\endgroup$
2
  • 1
    $\begingroup$ ColorDistance seems to require unexplained pre-conversion to LCH colorspace to work acceptably Could you make an example about this point? ColorDistance should work with any supported color directive. Linear and non-linear colorspaces are not intuitively separate, or compatible, for instance RGB-based spaces are non-linear, CIEXYZ is linear while LAB and LUV are not (L is a non-linear transformation of Y). Not sure about what you mean by compatible here. $\endgroup$
    – Batracos
    Commented Mar 17, 2016 at 12:56
  • $\begingroup$ @Batracos This is really an unscientific hunch. I'm feeling that colors converted from "traditional" RGBColor to other spaces, or other spaces to RGBColor getting displayed on the notebook sometimes fail to observe linearity (or non-linearity) correctly. I haven't really pinpointed an exact way to demonstrate this, though. $\endgroup$
    – kirma
    Commented Mar 17, 2016 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.