24
$\begingroup$

How can I change the color of a Locator in a Manipulate? As an example, consider the following.

Manipulate[
  Graphics[{Line[{{0,0},pt}]},
    PlotRange -> 2
  ], {{pt,{0,0}},{-2,-2},{2,2}, Locator}]

It's easy enough to hide the Locator and then add any primitives that I want:

Manipulate[
  Graphics[{
    Line[{{0,0},pt}],
    {PointSize[Large],Red,Point[pt]}},
    PlotRange -> 2
  ], {{pt,{0,0}},{-2,-2},{2,2}, Locator, Appearance -> None}]

The Locator itself, though, seems impervious to any directives I try.

$\endgroup$

4 Answers 4

16
$\begingroup$

As far as I know, you can't change the colour of the default locator directly, but you could use the specification for the default locator to create a coloured version of the default locator. These specifications can be found in the file MiscExpressions.tr located in $InstallationDirectory\SystemFiles/FrontEnd/TextResources. It turns out that the locator is specified as

GraphicsBox[
 InsetBox[GraphicsBox[{col, {AbsoluteThickness[1], 
     Antialiasing -> False, 
     LineBox[{{{0, -10}, {0, -2}}, {{0, 2}, {0, 10}}, {{-10, 0}, {-2, 
         0}}, {{2, 0}, {10, 0}}}], Antialiasing -> True, 
     CircleBox[{-0.5, 0.5}, 5]}, {AbsoluteThickness[3], Opacity[0.3], 
     CircleBox[{-0.5, 0.5}, 3]}}, ImageSize -> 17, 
   PlotRange -> {{-8, 8}, {-8, 8}}], {0, 0}, Center]]

So using this you could create a coloured locator by doing something like

loc[col_] := Rasterize[
  ToExpression@
   GraphicsBox[{col, {AbsoluteThickness[1], Antialiasing -> False, 
      LineBox[{{{0, -10}, {0, -2}}, {{0, 2}, {0, 10}}, {{-10, 0}, {-2,
           0}}, {{2, 0}, {10, 0}}}], Antialiasing -> True, 
      CircleBox[{-0.5, 0.5}, 5]}, {AbsoluteThickness[3], Opacity[0.3],
       CircleBox[{-0.5, 0.5}, 3]}}, ImageSize -> 17, 
    PlotRange -> {{-8, 8}, {-8, 8}}],
  Background -> None]

Graphics[{Disk[], Locator[{0, 0}, loc[Darker[Green]]]}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ +1 You could also Rasterize a default locator, just in case WR decides to change the def. Then modifying the coloring seems easy $\endgroup$ Commented Mar 21, 2012 at 16:56
  • $\begingroup$ Yes, this is as I thought. Thanks! $\endgroup$ Commented Mar 21, 2012 at 18:31
  • $\begingroup$ +1 for throwing in MiscExpressions.tr! $\endgroup$ Commented Oct 28, 2012 at 16:55
19
$\begingroup$

Use Appearance->SomeGraphicsObject:

l = Graphics[{Red, Disk[{0, 0}, .1]}, ImageSize -> 10];    
Manipulate[Graphics[{Line[{{0,0},pt}]},PlotRange->2],
 {{pt,{0,0}},{-2,-2},{2,2},Locator,Appearance->l}]

Mathematica graphics

You can use any Graphics or Graphics3D object for this, i.e.:

l = Graphics[{Red, Table[Circle[{0, 0}, i], {i, 3}]}, ImageSize -> 20];

Mathematica graphics

or

l=Graphics3D[{Blue,Cylinder[],Red,Sphere[{0,0,2}],Black,Thick,Dashed,
 Line[{{-2,0,2},{2,0,2},{0,0,4},{-2,0,2}}],Yellow,
 Polygon[{{-3,-3,-2},{-3,3,-2},{3,3,-2},{3,-3,-2}}],Green,
 Opacity[.3],Cuboid[{-2,-2,-2},{2,2,-1}]},ImageSize->50];

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ What if the Locator is not in a Manipulate? Take e.g. Graphics[Dynamic@Locator[], PlotRange -> 1] How the appearance be changed here? Appearance -> Graphics@Disk[] does not work as an option to the Locator. $\endgroup$
    – Szabolcs
    Commented Mar 21, 2012 at 16:43
  • 1
    $\begingroup$ @Szabolcs You can always just do Graphics[Locator[{0, 0}, l], PlotRange -> 1] $\endgroup$
    – Eli Lansey
    Commented Mar 21, 2012 at 16:46
10
$\begingroup$

You could simply draw the locator with crosshairs. Color of circle, crosshairs and inner disk can be set as desired.

Manipulate[
    Graphics[{Line[{{0, 0}, pt}]}, PlotRange -> 2], 
    {{pt, {0, 0}}, {-2, -2}, {2, 2}, Locator, 
       Appearance -> Graphics[{Black,
           Line[{{0, 1.5}, {0, 3}}],Line[{{0, -1.5}, {0, -3}}],
           Line[{{-1.5, 0}, {-3, 0}}], Line[{{1.5, 0}, {3, 0}}],
           Circle[{0, 0}, 2], Blue, Disk[]}, ImageSize -> 20]}]

locator


If you don't require a replica of the default locator and a single color suffices, you might use \[CircleDot] or \[CirclePlus]. For example,

Manipulate[Graphics[{Line[{{0, 0}, pt}]}, PlotRange -> 2], 
  {{pt, {0, 0}}, {-2, -2}, {2, 2}, Locator,  
    Appearance -> Style["\[CircleDot]", Blue, 24]}]

characterlocator

$\endgroup$
3
$\begingroup$

The Locator can be used in Graphics, producing a grayscale image, which can then be colorized:

Colorize[Graphics[{Locator[]}]

Using a gradient from the desired color to white and replacing white with transparent pixels gives colored default Locator:

ColorReplace[
  Colorize[Graphics[{Locator[]}], 
    ColorFunction -> (Blend[{Red, White}, #] &)]
  , White]

Putting it together with the original code gives:

coloredLocator[color_] := 
  ColorReplace[
    Colorize[Graphics[{Locator[]}], 
      ColorFunction -> (Blend[{color, White}, #] &)]
    , White];

Manipulate[
  Graphics[{Line[{{0, 0}, pt}]}, PlotRange -> 2], 
  {{pt, {0, 0}}, {-2, -2}, {2, 2}, Locator, Appearance -> coloredLocator[Red]}]

enter image description here

This works in Mathematica version 11 and I didn't test it in previous versions.

$\endgroup$

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.