5

I am trying to draw a CIE Chromaticity Diagram with pgfplots. I found TikZ Chromaticity Diagram which shows a very beautiful solution using tikz. To draw conveniently datasets on the CIE chromaticity diagram I would like to transfer this example to be used with pgfplots and the variable plot sizes but I have no clue how to get it done.

The question is: How can I fill the area within the spectral curve in a pgfplot with a CIE conform color fading like it is shown in the referenced question so that I can place additional plots with pgfplots easily on top of that and how can I make the fading dynamic so it scales to the drawn spectral curve by itself?

I am not that familiar with pgfplots and filling backgrounds so probably my attempt is impossible.

Here is a MWE that is basically a pgfplots picture with integrated components from the referenced question:

\documentclass{standalone}
\usepackage{luatex85}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{fadings}

\def\colorspaceline{(0.1738,0.0049)(0.1736,0.0049)(0.1733,0.0048)(0.1730,0.0048)(0.1726,0.0048)(0.1721,0.0048)(0.1714,0.0051)(0.1703,0.0058)(0.1689,0.0069)(0.1669,0.0086)(0.1644,0.0109)(0.1611,0.0138)(0.1566,0.0177)(0.1510,0.0227)(0.1440,0.0297)(0.1355,0.0399)(0.1241,0.0578)(0.1096,0.0868)(0.0913,0.1327)(0.0687,0.2007)(0.0454,0.2950)(0.0235,0.4127)(0.0082,0.5384)(0.0039,0.6548)(0.0139,0.7502)(0.0389,0.8120)(0.0743,0.8338)(0.1142,0.8262)(0.1547,0.8059)(0.1929,0.7816)(0.2296,0.7543)(0.2658,0.7243)(0.3016,0.6923)(0.3373,0.6589)(0.3731,0.6245)(0.4087,0.5896)(0.4441,0.5547)(0.4788,0.5202)(0.5125,0.4866)(0.5448,0.4544)(0.5752,0.4242)(0.6029,0.3965)(0.6270,0.3725)(0.6482,0.3514)(0.6658,0.3340)(0.6801,0.3197)(0.6915,0.3083)(0.7006,0.2993)(0.7079,0.2920)(0.7140,0.2859)(0.7190,0.2809)(0.7230,0.2770)(0.7260,0.2740)(0.7283,0.2717)(0.7300,0.2700)(0.7311,0.2689)(0.7320,0.2680)(0.7327,0.2673)(0.7334,0.2666)(0.7340,0.2660)(0.7344,0.2656)(0.7346,0.2654)(0.7347,0.2653)}


% Derived transformation matrix
\def\XYZtoRGB{
  { 3.2410}{-1.5374}{-0.4986}
  {-0.9692}{ 1.8760}{ 0.0416}
  { 0.0556}{-0.2040}{ 1.0570}}

 % Linear color to gamma corrected transform
\def\gammaCorrect{
  dup 0.0031308 le                    % if < 0.0031308
  {12.92 mul}                         % then linear transform
  {1 2.4 div exp 1.055 mul -0.055 add}% else power transform
  ifelse }

% ------------------------------------------------------------- TYPE 4 HELPERS

\def\scalarProduct#1#2#3{
  #3 mul     exch
  #2 mul add exch
  #1 mul add }

\def\applyMatrix#1#2#3#4#5#6#7#8#9{
  3 copy 3 copy
  \scalarProduct{#7}{#8}{#9} 7 1 roll
  \scalarProduct{#4}{#5}{#6} 5 1 roll
  \scalarProduct{#1}{#2}{#3} 3 1 roll }

\def\xyYtoXYZ{                        % x y Y
  3 copy 3 1 roll                     % x y Y Y x y
  add neg 1 add mul 2 index div       % x y Y Y*(-(x+y)+1)/y=Z
  4 1 roll                            % Z x y Y
  dup                                 % Z x y Y Y=Y
  5 1 roll                            % Y Z x y Y
  3 2 roll                            % Y Z y Y x
  mul exch div                        % Y Z Y*x/y=X
  3 1 roll }                          % X Y Z

\def\gammaCorrectVector{
  \gammaCorrect 3 1 roll
  \gammaCorrect 3 1 roll
  \gammaCorrect 3 1 roll}


\begin{document}
\begin{tikzpicture}

\pgfdeclarefunctionalshading{colorspace}
  {\pgfpointorigin}{\pgfpoint{1000bp}{1000bp}}{}{
    1000 div exch 1000 div exch       % x y   (chromaticity)
    1.0                               % x y Y (chromaticity+luminance)
    \xyYtoXYZ                         % X Y Z (XYZ)
    \expandafter\applyMatrix\XYZtoRGB % R G B (sRGB linear)
    \gammaCorrectVector }             % R G B (sRGB gamma corrected)

        \begin{axis}[
        ymin=0,
        ymax=0.85,
        xmin=0,
        xmax=0.75,
        xlabel=x,
        ylabel=y,
        width=5cm,
        height=5cm,
        clip=false]
        %Spektrale Rand des CIE Raums
        \addplot[no marks,black,smooth] coordinates {\colorspaceline} -- cycle;

        \clip [smooth] plot coordinates {\colorspaceline} -- cycle;
        % sRGB color space
        \pgfuseshading{colorspace}

        \end{axis}
\end{tikzpicture}

\end{document}

As I understand it the shading is static and not automatically adaptable to the spectral curve drawn in the plot. See the MWE as a poor starting point to find a solution.

EDIT: I used the code which superimposes an axis around the tikz picture provided by BambOo. As standalone it works but integrating it into a larger document in my case will screw up the CIE color shading and the area will be shown as gray + the PDF Viewer recognizes an error on that page. As in the documentation of PGF/tikz is stated the fading with functions is fragile and that might happen here as well.

EDIT: addplot graphics from pgfplots might be a way of getting it done. Therefor a clean picture of the color space is needed which is cut on defined positions on the cooridinate system of the color space.

2
  • What is your question? Mar 25, 2018 at 4:09
  • @Joel Kulesza The question is how can I fill the area within the spectral curve with a CIE conform color fading like it is shown in the referenced question so that I can place additional plots with pgfplots easily on top of that.
    – DMZ
    Mar 25, 2018 at 5:19

2 Answers 2

8

Run with xelatex

\documentclass{article}
\usepackage{pst-cie}
\begin{document}

\begin{pspicture}(-1,-1)(8.5,11)
\psChromaticityDiagram[Planck,trianglecolor=black]
\rput(5.5,8){\white \textbf{Colorspace sRGB}}
\end{pspicture} 

\end{document}

enter image description here

1
  • 3
    nice way of drawing this but it does not answer the question for a solution with pgfplots. Besides that I tried to compile it with an up to date version of MikTeX and get GhostScript 9.22 errors. Do you have any idea what the problem might be? Is it possible with pstricks to plot a dataset onto the colorspace? I am not familiar with pstricks.
    – DMZ
    Apr 8, 2018 at 12:52
5
+50

Maybe I misunderstood your question, but can't you just superimpose a pgfplots axis to the exsiting scope (taken from your linked post) ?

EDIT: I added a scaling factor (by hand) to obtain the required result when superimposing the initial scope with an axis environment.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{positioning}

% ------------------------------------------------------------------- RAW DATA

% DATASET ORIGIN:
%  http://steve.hollasch.net/cgindex/color/freq-rgb.html
% Spectral stimuli colors - CIE 1931
\def\spectralLocus{ % xy coordinates from 390nm to 700nm in steps of 5nm
  (0.1738,0.0049)(0.1736,0.0049)(0.1733,0.0048)(0.1730,0.0048)(0.1726,0.0048)
  (0.1721,0.0048)(0.1714,0.0051)(0.1703,0.0058)(0.1689,0.0069)(0.1669,0.0086)
  (0.1644,0.0109)(0.1611,0.0138)(0.1566,0.0177)(0.1510,0.0227)(0.1440,0.0297)
  (0.1355,0.0399)(0.1241,0.0578)(0.1096,0.0868)(0.0913,0.1327)(0.0687,0.2007)
  (0.0454,0.2950)(0.0235,0.4127)(0.0082,0.5384)(0.0039,0.6548)(0.0139,0.7502)
  (0.0389,0.8120)(0.0743,0.8338)(0.1142,0.8262)(0.1547,0.8059)(0.1929,0.7816)
  (0.2296,0.7543)(0.2658,0.7243)(0.3016,0.6923)(0.3373,0.6589)(0.3731,0.6245)
  (0.4087,0.5896)(0.4441,0.5547)(0.4788,0.5202)(0.5125,0.4866)(0.5448,0.4544)
  (0.5752,0.4242)(0.6029,0.3965)(0.6270,0.3725)(0.6482,0.3514)(0.6658,0.3340)
  (0.6801,0.3197)(0.6915,0.3083)(0.7006,0.2993)(0.7079,0.2920)(0.7140,0.2859)
  (0.7190,0.2809)(0.7230,0.2770)(0.7260,0.2740)(0.7283,0.2717)(0.7300,0.2700)
  (0.7311,0.2689)(0.7320,0.2680)(0.7327,0.2673)(0.7334,0.2666)(0.7340,0.2660)
  (0.7344,0.2656)(0.7346,0.2654)(0.7347,0.2653)}

% DATASET ORIGIN:
%  http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html
% Blackbody colors - CIE 1931
\def\planckianLocus{ % xy coordinates from 1000K to 40000K in steps of 100K
  (0.6499,0.3474)(0.6361,0.3594)(0.6226,0.3703)(0.6095,0.3801)(0.5966,0.3887)
  (0.5841,0.3962)(0.5720,0.4025)(0.5601,0.4076)(0.5486,0.4118)(0.5375,0.4150)
  (0.5267,0.4173)(0.5162,0.4188)(0.5062,0.4196)(0.4965,0.4198)(0.4872,0.4194)
  (0.4782,0.4186)(0.4696,0.4173)(0.4614,0.4158)(0.4535,0.4139)(0.4460,0.4118)
  (0.4388,0.4095)(0.4320,0.4070)(0.4254,0.4044)(0.4192,0.4018)(0.4132,0.3990)
  (0.4075,0.3962)(0.4021,0.3934)(0.3969,0.3905)(0.3919,0.3877)(0.3872,0.3849)
  (0.3827,0.3820)(0.3784,0.3793)(0.3743,0.3765)(0.3704,0.3738)(0.3666,0.3711)
  (0.3631,0.3685)(0.3596,0.3659)(0.3563,0.3634)(0.3532,0.3609)(0.3502,0.3585)
  (0.3473,0.3561)(0.3446,0.3538)(0.3419,0.3516)(0.3394,0.3494)(0.3369,0.3472)
  (0.3346,0.3451)(0.3323,0.3431)(0.3302,0.3411)(0.3281,0.3392)(0.3261,0.3373)
  (0.3242,0.3355)(0.3223,0.3337)(0.3205,0.3319)(0.3188,0.3302)(0.3171,0.3286)
  (0.3155,0.3270)(0.3140,0.3254)(0.3125,0.3238)(0.3110,0.3224)(0.3097,0.3209)
  (0.3083,0.3195)(0.3070,0.3181)(0.3058,0.3168)(0.3045,0.3154)(0.3034,0.3142)
  (0.3022,0.3129)(0.3011,0.3117)(0.3000,0.3105)(0.2990,0.3094)(0.2980,0.3082)
  (0.2970,0.3071)(0.2961,0.3061)(0.2952,0.3050)(0.2943,0.3040)(0.2934,0.3030)
  (0.2926,0.3020)(0.2917,0.3011)(0.2910,0.3001)(0.2902,0.2992)(0.2894,0.2983)
  (0.2887,0.2975)(0.2880,0.2966)(0.2873,0.2958)(0.2866,0.2950)(0.2860,0.2942)
  (0.2853,0.2934)(0.2847,0.2927)(0.2841,0.2919)(0.2835,0.2912)(0.2829,0.2905)
  (0.2824,0.2898)(0.2818,0.2891)(0.2813,0.2884)(0.2807,0.2878)(0.2802,0.2871)
  (0.2797,0.2865)(0.2792,0.2859)(0.2788,0.2853)(0.2783,0.2847)(0.2778,0.2841)
  (0.2774,0.2836)(0.2770,0.2830)(0.2765,0.2825)(0.2761,0.2819)(0.2757,0.2814)
  (0.2753,0.2809)(0.2749,0.2804)(0.2745,0.2799)(0.2742,0.2794)(0.2738,0.2789)
  (0.2734,0.2785)(0.2731,0.2780)(0.2727,0.2776)(0.2724,0.2771)(0.2721,0.2767)
  (0.2717,0.2763)(0.2714,0.2758)(0.2711,0.2754)(0.2708,0.2750)(0.2705,0.2746)
  (0.2702,0.2742)(0.2699,0.2738)(0.2696,0.2735)(0.2694,0.2731)(0.2691,0.2727)
  (0.2688,0.2724)(0.2686,0.2720)(0.2683,0.2717)(0.2680,0.2713)(0.2678,0.2710)
  (0.2675,0.2707)(0.2673,0.2703)(0.2671,0.2700)(0.2668,0.2697)(0.2666,0.2694)
  (0.2664,0.2691)(0.2662,0.2688)(0.2659,0.2685)(0.2657,0.2682)(0.2655,0.2679)
  (0.2653,0.2676)(0.2651,0.2673)(0.2649,0.2671)(0.2647,0.2668)(0.2645,0.2665)
  (0.2643,0.2663)(0.2641,0.2660)(0.2639,0.2657)(0.2638,0.2655)(0.2636,0.2652)
  (0.2634,0.2650)(0.2632,0.2648)(0.2631,0.2645)(0.2629,0.2643)(0.2627,0.2641)
  (0.2626,0.2638)(0.2624,0.2636)(0.2622,0.2634)(0.2621,0.2632)(0.2619,0.2629)
  (0.2618,0.2627)(0.2616,0.2625)(0.2615,0.2623)(0.2613,0.2621)(0.2612,0.2619)
  (0.2610,0.2617)(0.2609,0.2615)(0.2608,0.2613)(0.2606,0.2611)(0.2605,0.2609)
  (0.2604,0.2607)(0.2602,0.2606)(0.2601,0.2604)(0.2600,0.2602)(0.2598,0.2600)
  (0.2597,0.2598)(0.2596,0.2597)(0.2595,0.2595)(0.2593,0.2593)(0.2592,0.2592)
  (0.2591,0.2590)(0.2590,0.2588)(0.2589,0.2587)(0.2588,0.2585)(0.2587,0.2584)
  (0.2586,0.2582)(0.2584,0.2580)(0.2583,0.2579)(0.2582,0.2577)(0.2581,0.2576)
  (0.2580,0.2574)(0.2579,0.2573)(0.2578,0.2572)(0.2577,0.2570)(0.2576,0.2569)
  (0.2575,0.2567)(0.2574,0.2566)(0.2573,0.2565)(0.2572,0.2563)(0.2571,0.2562)
  (0.2571,0.2561)(0.2570,0.2559)(0.2569,0.2558)(0.2568,0.2557)(0.2567,0.2555)
  (0.2566,0.2554)(0.2565,0.2553)(0.2564,0.2552)(0.2564,0.2550)(0.2563,0.2549)
  (0.2562,0.2548)(0.2561,0.2547)(0.2560,0.2546)(0.2559,0.2545)(0.2559,0.2543)
  (0.2558,0.2542)(0.2557,0.2541)(0.2556,0.2540)(0.2556,0.2539)(0.2555,0.2538)
  (0.2554,0.2537)(0.2553,0.2536)(0.2553,0.2535)(0.2552,0.2534)(0.2551,0.2533)
  (0.2551,0.2532)(0.2550,0.2531)(0.2549,0.2530)(0.2548,0.2529)(0.2548,0.2528)
  (0.2547,0.2527)(0.2546,0.2526)(0.2546,0.2525)(0.2545,0.2524)(0.2544,0.2523)
  (0.2544,0.2522)(0.2543,0.2521)(0.2543,0.2520)(0.2542,0.2519)(0.2541,0.2518)
  (0.2541,0.2517)(0.2540,0.2516)(0.2540,0.2516)(0.2539,0.2515)(0.2538,0.2514)
  (0.2538,0.2513)(0.2537,0.2512)(0.2537,0.2511)(0.2536,0.2511)(0.2535,0.2510)
  (0.2535,0.2509)(0.2534,0.2508)(0.2534,0.2507)(0.2533,0.2507)(0.2533,0.2506)
  (0.2532,0.2505)(0.2532,0.2504)(0.2531,0.2503)(0.2531,0.2503)(0.2530,0.2502)
  (0.2530,0.2501)(0.2529,0.2500)(0.2529,0.2500)(0.2528,0.2499)(0.2528,0.2498)
  (0.2527,0.2497)(0.2527,0.2497)(0.2526,0.2496)(0.2526,0.2495)(0.2525,0.2495)
  (0.2525,0.2494)(0.2524,0.2493)(0.2524,0.2493)(0.2523,0.2492)(0.2523,0.2491)
  (0.2523,0.2491)(0.2522,0.2490)(0.2522,0.2489)(0.2521,0.2489)(0.2521,0.2488)
  (0.2520,0.2487)(0.2520,0.2487)(0.2519,0.2486)(0.2519,0.2485)(0.2519,0.2485)
  (0.2518,0.2484)(0.2518,0.2484)(0.2517,0.2483)(0.2517,0.2482)(0.2517,0.2482)
  (0.2516,0.2481)(0.2516,0.2481)(0.2515,0.2480)(0.2515,0.2480)(0.2515,0.2479)
  (0.2514,0.2478)(0.2514,0.2478)(0.2513,0.2477)(0.2513,0.2477)(0.2513,0.2476)
  (0.2512,0.2476)(0.2512,0.2475)(0.2512,0.2474)(0.2511,0.2474)(0.2511,0.2473)
  (0.2511,0.2473)(0.2510,0.2472)(0.2510,0.2472)(0.2509,0.2471)(0.2509,0.2471)
  (0.2509,0.2470)(0.2508,0.2470)(0.2508,0.2469)(0.2508,0.2469)(0.2507,0.2468)
  (0.2507,0.2468)(0.2507,0.2467)(0.2506,0.2467)(0.2506,0.2466)(0.2506,0.2466)
  (0.2505,0.2465)(0.2505,0.2465)(0.2505,0.2464)(0.2505,0.2464)(0.2504,0.2463)
  (0.2504,0.2463)(0.2504,0.2463)(0.2503,0.2462)(0.2503,0.2462)(0.2503,0.2461)
  (0.2502,0.2461)(0.2502,0.2460)(0.2502,0.2460)(0.2502,0.2459)(0.2501,0.2459)
  (0.2501,0.2459)(0.2501,0.2458)(0.2500,0.2458)(0.2500,0.2457)(0.2500,0.2457)
  (0.2500,0.2456)(0.2499,0.2456)(0.2499,0.2456)(0.2499,0.2455)(0.2498,0.2455)
  (0.2498,0.2454)(0.2498,0.2454)(0.2498,0.2454)(0.2497,0.2453)(0.2497,0.2453)
  (0.2497,0.2452)(0.2497,0.2452)(0.2496,0.2452)(0.2496,0.2451)(0.2496,0.2451)
  (0.2496,0.2450)(0.2495,0.2450)(0.2495,0.2450)(0.2495,0.2449)(0.2495,0.2449)
  (0.2494,0.2449)(0.2494,0.2448)(0.2494,0.2448)(0.2494,0.2447)(0.2493,0.2447)
  (0.2493,0.2447)(0.2493,0.2446)(0.2493,0.2446)(0.2492,0.2446)(0.2492,0.2445)
  (0.2492,0.2445)(0.2492,0.2445)(0.2491,0.2444)(0.2491,0.2444)(0.2491,0.2444)
  (0.2491,0.2443)(0.2491,0.2443)(0.2490,0.2443)(0.2490,0.2442)(0.2490,0.2442)
  (0.2490,0.2442)(0.2489,0.2441)(0.2489,0.2441)(0.2489,0.2441)(0.2489,0.2440)
  (0.2489,0.2440)(0.2488,0.2440)(0.2488,0.2439)(0.2488,0.2439)(0.2488,0.2439)
  (0.2487,0.2438)}

% ---------------------------------------------- sRGB COLORSPACE SPECIFICATION

% DATASET ORIGIN: http://www.color.org/sRGB.xalter

% CIE chromaticities for ITU-R BT.709 reference primaries
\def\primariesLoci{
  (0.6400,0.3300)  % R
  (0.3000,0.6000)  % G
  (0.1500,0.0600)} % B

% CIE standard illuminant D65
\def\whitepointLocus{
  (0.3127,0.3290)}

% Derived transformation matrix
\def\XYZtoRGB{
  { 3.2410}{-1.5374}{-0.4986}
  {-0.9692}{ 1.8760}{ 0.0416}
  { 0.0556}{-0.2040}{ 1.0570}}

 % Linear color to gamma corrected transform
\def\gammaCorrect{
  dup 0.0031308 le                    % if < 0.0031308
  {12.92 mul}                         % then linear transform
  {1 2.4 div exp 1.055 mul -0.055 add}% else power transform
  ifelse }

% ------------------------------------------------------------- TYPE 4 HELPERS

\def\scalarProduct#1#2#3{
  #3 mul     exch
  #2 mul add exch
  #1 mul add }

\def\applyMatrix#1#2#3#4#5#6#7#8#9{
  3 copy 3 copy
  \scalarProduct{#7}{#8}{#9} 7 1 roll
  \scalarProduct{#4}{#5}{#6} 5 1 roll
  \scalarProduct{#1}{#2}{#3} 3 1 roll }

\def\xyYtoXYZ{                        % x y Y
  3 copy 3 1 roll                     % x y Y Y x y
  add neg 1 add mul 2 index div       % x y Y Y*(-(x+y)+1)/y=Z
  4 1 roll                            % Z x y Y
  dup                                 % Z x y Y Y=Y
  5 1 roll                            % Y Z x y Y
  3 2 roll                            % Y Z y Y x
  mul exch div                        % Y Z Y*x/y=X
  3 1 roll }                          % X Y Z

\def\gammaCorrectVector{
  \gammaCorrect 3 1 roll
  \gammaCorrect 3 1 roll
  \gammaCorrect 3 1 roll}

% -------------------------------------------------------------------- DRAWING

\begin{document}
\begin{tikzpicture}

\pgfdeclarefunctionalshading{colorspace}
  {\pgfpointorigin}{\pgfpoint{100bp}{100bp}}{}{
    100 div exch 100 div exch       % x y   (chromaticity)
    1.0                               % x y Y (chromaticity+luminance)
    \xyYtoXYZ                         % X Y Z (XYZ)
    \expandafter\applyMatrix\XYZtoRGB % R G B (sRGB linear)
    \gammaCorrectVector }             % R G B (sRGB gamma corrected)

\begin{scope} [shift={(-50bp,-50bp)}, scale=10bp/1cm]
  % Background + viewport
  %\fill [red] (0,0) rectangle (10,10);
  %\useasboundingbox (0,0) rectangle (10,10);
  % xy grid
  %\draw [dashed, gray] grid (10,10);
  \begin{scope} [scale=10]
    % Spectral locus marks
    \path [mark=*, mark repeat=2, white]
      plot [mark size=0.10, mark phase=1] coordinates {\spectralLocus}
      plot [mark size=0.05, mark phase=2] coordinates {\spectralLocus};
    % Smooth spectral locus contour for clipping
    \clip [smooth] plot coordinates {\spectralLocus} -- cycle;
    % sRGB color space
    \pgfuseshading{colorspace}
    % Standard illuminant mark
    \draw [gray] \whitepointLocus circle (0.005);
    % Reference primaries gamut
    \fill [black, even odd rule, opacity=0.5]
      rectangle +(1,1) plot coordinates {\primariesLoci} -- cycle;
    % Planckian locus markings
    \path [mark=*,gray]
      plot [mark size=0.05, mark repeat=10] coordinates {\planckianLocus}
      plot [mark size=0.01, mark repeat=1 ] coordinates {\planckianLocus};
  \end{scope}
\end{scope}

\begin{axis} [
  xmajorgrids,ymajorgrids,major grid style={dashed},
  at={(current bounding box.south west)},
  scale=9.27bp/1cm,
  xmin=0,xmax=1,ymin=0,ymax=1,
  width=350bp,height=350bp,
  axis on top=false,
  xtick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1},
  ytick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1}]
  \addplot [red,thick,domain=0:1,samples=100] {x};
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

EDIT

This is closer in terms of pgfplots integration, however, I can't figure how to set the shading solors properly, all the scaling factors mixed altogether a messing with my mind. I will try to complete my answer later.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{positioning}

% ------------------------------------------------------------------- RAW DATA

% DATASET ORIGIN:
%  http://steve.hollasch.net/cgindex/color/freq-rgb.html
% Spectral stimuli colors - CIE 1931
\def\spectralLocus{ % xy coordinates from 390nm to 700nm in steps of 5nm
  (0.1738,0.0049)(0.1736,0.0049)(0.1733,0.0048)(0.1730,0.0048)(0.1726,0.0048)
  (0.1721,0.0048)(0.1714,0.0051)(0.1703,0.0058)(0.1689,0.0069)(0.1669,0.0086)
  (0.1644,0.0109)(0.1611,0.0138)(0.1566,0.0177)(0.1510,0.0227)(0.1440,0.0297)
  (0.1355,0.0399)(0.1241,0.0578)(0.1096,0.0868)(0.0913,0.1327)(0.0687,0.2007)
  (0.0454,0.2950)(0.0235,0.4127)(0.0082,0.5384)(0.0039,0.6548)(0.0139,0.7502)
  (0.0389,0.8120)(0.0743,0.8338)(0.1142,0.8262)(0.1547,0.8059)(0.1929,0.7816)
  (0.2296,0.7543)(0.2658,0.7243)(0.3016,0.6923)(0.3373,0.6589)(0.3731,0.6245)
  (0.4087,0.5896)(0.4441,0.5547)(0.4788,0.5202)(0.5125,0.4866)(0.5448,0.4544)
  (0.5752,0.4242)(0.6029,0.3965)(0.6270,0.3725)(0.6482,0.3514)(0.6658,0.3340)
  (0.6801,0.3197)(0.6915,0.3083)(0.7006,0.2993)(0.7079,0.2920)(0.7140,0.2859)
  (0.7190,0.2809)(0.7230,0.2770)(0.7260,0.2740)(0.7283,0.2717)(0.7300,0.2700)
  (0.7311,0.2689)(0.7320,0.2680)(0.7327,0.2673)(0.7334,0.2666)(0.7340,0.2660)
  (0.7344,0.2656)(0.7346,0.2654)(0.7347,0.2653)}

% DATASET ORIGIN:
%  http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html
% Blackbody colors - CIE 1931
\def\planckianLocus{ % xy coordinates from 1000K to 40000K in steps of 100K
  (0.6499,0.3474)(0.6361,0.3594)(0.6226,0.3703)(0.6095,0.3801)(0.5966,0.3887)
  (0.5841,0.3962)(0.5720,0.4025)(0.5601,0.4076)(0.5486,0.4118)(0.5375,0.4150)
  (0.5267,0.4173)(0.5162,0.4188)(0.5062,0.4196)(0.4965,0.4198)(0.4872,0.4194)
  (0.4782,0.4186)(0.4696,0.4173)(0.4614,0.4158)(0.4535,0.4139)(0.4460,0.4118)
  (0.4388,0.4095)(0.4320,0.4070)(0.4254,0.4044)(0.4192,0.4018)(0.4132,0.3990)
  (0.4075,0.3962)(0.4021,0.3934)(0.3969,0.3905)(0.3919,0.3877)(0.3872,0.3849)
  (0.3827,0.3820)(0.3784,0.3793)(0.3743,0.3765)(0.3704,0.3738)(0.3666,0.3711)
  (0.3631,0.3685)(0.3596,0.3659)(0.3563,0.3634)(0.3532,0.3609)(0.3502,0.3585)
  (0.3473,0.3561)(0.3446,0.3538)(0.3419,0.3516)(0.3394,0.3494)(0.3369,0.3472)
  (0.3346,0.3451)(0.3323,0.3431)(0.3302,0.3411)(0.3281,0.3392)(0.3261,0.3373)
  (0.3242,0.3355)(0.3223,0.3337)(0.3205,0.3319)(0.3188,0.3302)(0.3171,0.3286)
  (0.3155,0.3270)(0.3140,0.3254)(0.3125,0.3238)(0.3110,0.3224)(0.3097,0.3209)
  (0.3083,0.3195)(0.3070,0.3181)(0.3058,0.3168)(0.3045,0.3154)(0.3034,0.3142)
  (0.3022,0.3129)(0.3011,0.3117)(0.3000,0.3105)(0.2990,0.3094)(0.2980,0.3082)
  (0.2970,0.3071)(0.2961,0.3061)(0.2952,0.3050)(0.2943,0.3040)(0.2934,0.3030)
  (0.2926,0.3020)(0.2917,0.3011)(0.2910,0.3001)(0.2902,0.2992)(0.2894,0.2983)
  (0.2887,0.2975)(0.2880,0.2966)(0.2873,0.2958)(0.2866,0.2950)(0.2860,0.2942)
  (0.2853,0.2934)(0.2847,0.2927)(0.2841,0.2919)(0.2835,0.2912)(0.2829,0.2905)
  (0.2824,0.2898)(0.2818,0.2891)(0.2813,0.2884)(0.2807,0.2878)(0.2802,0.2871)
  (0.2797,0.2865)(0.2792,0.2859)(0.2788,0.2853)(0.2783,0.2847)(0.2778,0.2841)
  (0.2774,0.2836)(0.2770,0.2830)(0.2765,0.2825)(0.2761,0.2819)(0.2757,0.2814)
  (0.2753,0.2809)(0.2749,0.2804)(0.2745,0.2799)(0.2742,0.2794)(0.2738,0.2789)
  (0.2734,0.2785)(0.2731,0.2780)(0.2727,0.2776)(0.2724,0.2771)(0.2721,0.2767)
  (0.2717,0.2763)(0.2714,0.2758)(0.2711,0.2754)(0.2708,0.2750)(0.2705,0.2746)
  (0.2702,0.2742)(0.2699,0.2738)(0.2696,0.2735)(0.2694,0.2731)(0.2691,0.2727)
  (0.2688,0.2724)(0.2686,0.2720)(0.2683,0.2717)(0.2680,0.2713)(0.2678,0.2710)
  (0.2675,0.2707)(0.2673,0.2703)(0.2671,0.2700)(0.2668,0.2697)(0.2666,0.2694)
  (0.2664,0.2691)(0.2662,0.2688)(0.2659,0.2685)(0.2657,0.2682)(0.2655,0.2679)
  (0.2653,0.2676)(0.2651,0.2673)(0.2649,0.2671)(0.2647,0.2668)(0.2645,0.2665)
  (0.2643,0.2663)(0.2641,0.2660)(0.2639,0.2657)(0.2638,0.2655)(0.2636,0.2652)
  (0.2634,0.2650)(0.2632,0.2648)(0.2631,0.2645)(0.2629,0.2643)(0.2627,0.2641)
  (0.2626,0.2638)(0.2624,0.2636)(0.2622,0.2634)(0.2621,0.2632)(0.2619,0.2629)
  (0.2618,0.2627)(0.2616,0.2625)(0.2615,0.2623)(0.2613,0.2621)(0.2612,0.2619)
  (0.2610,0.2617)(0.2609,0.2615)(0.2608,0.2613)(0.2606,0.2611)(0.2605,0.2609)
  (0.2604,0.2607)(0.2602,0.2606)(0.2601,0.2604)(0.2600,0.2602)(0.2598,0.2600)
  (0.2597,0.2598)(0.2596,0.2597)(0.2595,0.2595)(0.2593,0.2593)(0.2592,0.2592)
  (0.2591,0.2590)(0.2590,0.2588)(0.2589,0.2587)(0.2588,0.2585)(0.2587,0.2584)
  (0.2586,0.2582)(0.2584,0.2580)(0.2583,0.2579)(0.2582,0.2577)(0.2581,0.2576)
  (0.2580,0.2574)(0.2579,0.2573)(0.2578,0.2572)(0.2577,0.2570)(0.2576,0.2569)
  (0.2575,0.2567)(0.2574,0.2566)(0.2573,0.2565)(0.2572,0.2563)(0.2571,0.2562)
  (0.2571,0.2561)(0.2570,0.2559)(0.2569,0.2558)(0.2568,0.2557)(0.2567,0.2555)
  (0.2566,0.2554)(0.2565,0.2553)(0.2564,0.2552)(0.2564,0.2550)(0.2563,0.2549)
  (0.2562,0.2548)(0.2561,0.2547)(0.2560,0.2546)(0.2559,0.2545)(0.2559,0.2543)
  (0.2558,0.2542)(0.2557,0.2541)(0.2556,0.2540)(0.2556,0.2539)(0.2555,0.2538)
  (0.2554,0.2537)(0.2553,0.2536)(0.2553,0.2535)(0.2552,0.2534)(0.2551,0.2533)
  (0.2551,0.2532)(0.2550,0.2531)(0.2549,0.2530)(0.2548,0.2529)(0.2548,0.2528)
  (0.2547,0.2527)(0.2546,0.2526)(0.2546,0.2525)(0.2545,0.2524)(0.2544,0.2523)
  (0.2544,0.2522)(0.2543,0.2521)(0.2543,0.2520)(0.2542,0.2519)(0.2541,0.2518)
  (0.2541,0.2517)(0.2540,0.2516)(0.2540,0.2516)(0.2539,0.2515)(0.2538,0.2514)
  (0.2538,0.2513)(0.2537,0.2512)(0.2537,0.2511)(0.2536,0.2511)(0.2535,0.2510)
  (0.2535,0.2509)(0.2534,0.2508)(0.2534,0.2507)(0.2533,0.2507)(0.2533,0.2506)
  (0.2532,0.2505)(0.2532,0.2504)(0.2531,0.2503)(0.2531,0.2503)(0.2530,0.2502)
  (0.2530,0.2501)(0.2529,0.2500)(0.2529,0.2500)(0.2528,0.2499)(0.2528,0.2498)
  (0.2527,0.2497)(0.2527,0.2497)(0.2526,0.2496)(0.2526,0.2495)(0.2525,0.2495)
  (0.2525,0.2494)(0.2524,0.2493)(0.2524,0.2493)(0.2523,0.2492)(0.2523,0.2491)
  (0.2523,0.2491)(0.2522,0.2490)(0.2522,0.2489)(0.2521,0.2489)(0.2521,0.2488)
  (0.2520,0.2487)(0.2520,0.2487)(0.2519,0.2486)(0.2519,0.2485)(0.2519,0.2485)
  (0.2518,0.2484)(0.2518,0.2484)(0.2517,0.2483)(0.2517,0.2482)(0.2517,0.2482)
  (0.2516,0.2481)(0.2516,0.2481)(0.2515,0.2480)(0.2515,0.2480)(0.2515,0.2479)
  (0.2514,0.2478)(0.2514,0.2478)(0.2513,0.2477)(0.2513,0.2477)(0.2513,0.2476)
  (0.2512,0.2476)(0.2512,0.2475)(0.2512,0.2474)(0.2511,0.2474)(0.2511,0.2473)
  (0.2511,0.2473)(0.2510,0.2472)(0.2510,0.2472)(0.2509,0.2471)(0.2509,0.2471)
  (0.2509,0.2470)(0.2508,0.2470)(0.2508,0.2469)(0.2508,0.2469)(0.2507,0.2468)
  (0.2507,0.2468)(0.2507,0.2467)(0.2506,0.2467)(0.2506,0.2466)(0.2506,0.2466)
  (0.2505,0.2465)(0.2505,0.2465)(0.2505,0.2464)(0.2505,0.2464)(0.2504,0.2463)
  (0.2504,0.2463)(0.2504,0.2463)(0.2503,0.2462)(0.2503,0.2462)(0.2503,0.2461)
  (0.2502,0.2461)(0.2502,0.2460)(0.2502,0.2460)(0.2502,0.2459)(0.2501,0.2459)
  (0.2501,0.2459)(0.2501,0.2458)(0.2500,0.2458)(0.2500,0.2457)(0.2500,0.2457)
  (0.2500,0.2456)(0.2499,0.2456)(0.2499,0.2456)(0.2499,0.2455)(0.2498,0.2455)
  (0.2498,0.2454)(0.2498,0.2454)(0.2498,0.2454)(0.2497,0.2453)(0.2497,0.2453)
  (0.2497,0.2452)(0.2497,0.2452)(0.2496,0.2452)(0.2496,0.2451)(0.2496,0.2451)
  (0.2496,0.2450)(0.2495,0.2450)(0.2495,0.2450)(0.2495,0.2449)(0.2495,0.2449)
  (0.2494,0.2449)(0.2494,0.2448)(0.2494,0.2448)(0.2494,0.2447)(0.2493,0.2447)
  (0.2493,0.2447)(0.2493,0.2446)(0.2493,0.2446)(0.2492,0.2446)(0.2492,0.2445)
  (0.2492,0.2445)(0.2492,0.2445)(0.2491,0.2444)(0.2491,0.2444)(0.2491,0.2444)
  (0.2491,0.2443)(0.2491,0.2443)(0.2490,0.2443)(0.2490,0.2442)(0.2490,0.2442)
  (0.2490,0.2442)(0.2489,0.2441)(0.2489,0.2441)(0.2489,0.2441)(0.2489,0.2440)
  (0.2489,0.2440)(0.2488,0.2440)(0.2488,0.2439)(0.2488,0.2439)(0.2488,0.2439)
  (0.2487,0.2438)}

% ---------------------------------------------- sRGB COLORSPACE SPECIFICATION

% DATASET ORIGIN: http://www.color.org/sRGB.xalter

% CIE chromaticities for ITU-R BT.709 reference primaries
\def\primariesLoci{
  (0.6400,0.3300)  % R
  (0.3000,0.6000)  % G
  (0.1500,0.0600)} % B

% CIE standard illuminant D65
\def\whitepointLocus{
  (0.3127,0.3290)}

% Derived transformation matrix
\def\XYZtoRGB{
  { 3.2410}{-1.5374}{-0.4986}
  {-0.9692}{ 1.8760}{ 0.0416}
  { 0.0556}{-0.2040}{ 1.0570}}

 % Linear color to gamma corrected transform
\def\gammaCorrect{
  dup 0.0031308 le                    % if < 0.0031308
  {12.92 mul}                         % then linear transform
  {1 2.4 div exp 1.055 mul -0.055 add}% else power transform
  ifelse }

% ------------------------------------------------------------- TYPE 4 HELPERS

\def\scalarProduct#1#2#3{
  #3 mul     exch
  #2 mul add exch
  #1 mul add }

\def\applyMatrix#1#2#3#4#5#6#7#8#9{
  3 copy 3 copy
  \scalarProduct{#7}{#8}{#9} 7 1 roll
  \scalarProduct{#4}{#5}{#6} 5 1 roll
  \scalarProduct{#1}{#2}{#3} 3 1 roll }

\def\xyYtoXYZ{                        % x y Y
  3 copy 3 1 roll                     % x y Y Y x y
  add neg 1 add mul 2 index div       % x y Y Y*(-(x+y)+1)/y=Z
  4 1 roll                            % Z x y Y
  dup                                 % Z x y Y Y=Y
  5 1 roll                            % Y Z x y Y
  3 2 roll                            % Y Z y Y x
  mul exch div                        % Y Z Y*x/y=X
  3 1 roll }                          % X Y Z

\def\gammaCorrectVector{
  \gammaCorrect 3 1 roll
  \gammaCorrect 3 1 roll
  \gammaCorrect 3 1 roll}

% -------------------------------------------------------------------- DRAWING

\begin{document}
\begin{tikzpicture}

\pgfdeclarefunctionalshading{colorspace}
  {\pgfpointorigin}{\pgfpoint{100bp}{100bp}}{}{
    1000 div exch 1000 div exch       % x y   (chromaticity)
    1.0                               % x y Y (chromaticity+luminance)
    \xyYtoXYZ                         % X Y Z (XYZ)
    \expandafter\applyMatrix\XYZtoRGB % R G B (sRGB linear)
    \gammaCorrectVector }             % R G B (sRGB gamma corrected)

\begin{axis} [axis background/.style={fill=black}, scale=200bp/5cm,xmajorgrids,ymajorgrids,major grid style={dashed},xmin=0,xmax=0.9,ymin=0,ymax=0.9,width=5cm,height=5cm]
    % Spectral locus marks
    \addplot [mark=*, mark repeat=2, white]
      plot [mark size=0.10, mark phase=1] coordinates {\spectralLocus}
      plot [mark size=0.05, mark phase=2] coordinates {\spectralLocus};

    \addplot [shading=colorspace] plot coordinates {\spectralLocus} -- cycle;
    % Standard illuminant mark
    \draw [gray] \whitepointLocus circle (0.005);
    % Reference primaries gamut
    \fill [black, even odd rule, opacity=0.5] rectangle +(1,1) plot coordinates {\primariesLoci} -- cycle;
    % Planckian locus markings
    \addplot [mark=*,gray]
      plot [mark size=0.05, mark repeat=10] coordinates {\planckianLocus}
      plot [mark size=0.01, mark repeat=1 ] coordinates {\planckianLocus};

  \addplot [red,thick,domain=0:0.8,samples=100] {x};
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

4
  • Thanks for the idea but how can I make sure that the scaling is linked to the shading. Comparing your drawing with the one in the example shows a difference between locations of the outer points and the values of the axis.
    – DMZ
    Apr 7, 2018 at 13:25
  • @DMZ I updated my answer, but I have to admit I am not that familiar with functional shadings. I will try later.
    – BambOo
    Apr 7, 2018 at 15:30
  • I used your updated first example and it works as a standalone but integrating it into a document with a bit more stuff will screw up the color fading and will cause an error within the pdf for that page. The functional shading seems very fragile.
    – DMZ
    Apr 8, 2018 at 13:02
  • DMZ, sorry about that, there are several warnings regarding functional shading, and unfortunately I think it is very difficult to use precisely. Also, the result highly depends on the renderer so beware of surprises ;). I'm sorry not to have better solutions for you. I would advise using the solution proposed by @Herbert
    – BambOo
    Apr 8, 2018 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .