1
$\begingroup$

I'm making a composite 2D plot, combining different types of plots (Graphics, ListPlot etc.) with Show. One layer of the plots contains a polygon filled with colors which depend on the 2D coordinates on the polygon. (The vertices of the polygon are {{0.6400744901465835, 0.3299704937157755}, {0.29999999497269986, 0.5999999722714001}, {0.15001662467009716, 0.060006658004021524}}.)

To draw the polygon, I first thought of using ColorFunction with DensityPlot, but it appeared to only receive the value of the function itself, not the coordinates. My next attempt was with RegionPlot, which does get the coordinates. This looks like the following code.

(* Functions to calculate color from x and y coordinates *)
xyY2XYZ[x_, y_, Y_] = {(x Y)/y, Y, -(((-1 + x + y) Y)/y)};
normalize[x_] := x/Max@x
XYZ2sRGB[c_] := (normalize[( {
      {3.2406255, -1.537208, -0.4986286},
      {-0.9689307, 1.8757561, 0.0415175},
      {0.0557101, -0.2040211, 1.0569959}
     } ).c])^(1/2.2)
(* Actual plotting *)
RegionPlot[True, {x, 0.14, 0.65}, {y, 0.05, 0.61}, 
 ColorFunction -> 
  Function[{x, y}, 
   RGBColor[
    If[Max@Abs@Im@# != 0, {1, 1, 1}, Re@#] &@
     XYZ2sRGB@xyY2XYZ[x, y, 1]]], ColorFunctionScaling -> False, 
 PlotPoints -> 250]

Output of the above code

But I had to increase PlotPoints to 250, otherwise the result was too pixelized on the border, and MaxRecursion didn't appear to affect the output. This results in long time to wait, and the border still remains jagged. Also, the blue line between the image and the frame is an unwanted artifact.

So, what's a better way to do this?

$\endgroup$
3
$\begingroup$

OK, I've managed to fix both performance and jaggedness problems. For this, I just had to use DiscretizeRegion combined with Polygon as follows:

RegionPlot[
 DiscretizeRegion[
  Polygon[{{0.6400744901465835, 0.3299704937157755},
           {0.29999999497269986, 0.5999999722714001},
           {0.15001662467009716, 0.060006658004021524}}],
  MaxCellMeasure -> {"Length" -> 0.05}],
 ColorFunction -> Function[{x, y}, RGBColor[Re@XYZ2sRGB@xyY2XYZ[x, y, 1]]], 
 ColorFunctionScaling -> False, BoundaryStyle -> None]

Output

$\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.