2
$\begingroup$

I'm writing a function that, given a color, returns a contrasting color. It does this by maximizing the ColorDistance between the input and output colors. However, I'm getting an error that's complaining about how LABColor[l, a, b] is symbolic and not a color, which it is until Maximize gives it numerical value (and ultimately does find a nominally contrasting color). What do I do to LABColor to stop this error?

(Note that I don't think this scheme for finding contrasting colors is particularly good, I just want to know how to fix this error for general knowledge)

(* bounds of RGB space in LAB space *)
rgbCube = Cuboid[{0, -0.8, -1.13}, {1, 0.94, 0.94}];

(* Find a highly contrasting color *)
Maximize[
 ColorDistance[Pink, LABColor[l, a, b]], {l, a, b} \[Element] 
  rgbCube]
ColorDistance: Pink and LABColor[l, a, b] should be two valid color directives, images, or lists of objects with the same dimension.
Out[1]= {2.00573, {l -> 0., a -> -0.8, b -> -1.13}}
$\endgroup$
3
$\begingroup$

You can avoid that warning by stating that they are numeric like this:

rgbCube = Cuboid[{0, -0.8, -1.13}, {1, 0.94, 0.94}];
f[l_?NumericQ, a_?NumericQ, b_?NumericQ] := ColorDistance[
     RGBColor[1, 0.5, 0.5], LABColor[l, a, b]]
Maximize[f[l, a, b], {l, a, b} \[Element] rgbCube]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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