Nested foreach functioning unpredictably when drawing colored spheres in 3 dimensions
I'm visualising color systems with TikZ to my students, and here I'm creating a 3D set of axes which represent red, green, blue and their combinations as shaded spheres.

For some reason, the result looks alright on the Blue axis, but doesn't work as expected on Green and Red axes as when their values are increased the spheres stay gray. Where's the problem?
I've taken care that everything is represented in code in the same order, RGB. Code first sets up some variables about axes (x, y, z), then step size/distance between spheres and then the max value for assumed 8-bit color, 255.
The point is that the nested for eaches go through all integer values between 0–255 for red, green and blue (all combinations), the second tikzmath creates scaled variables to be used with color information, and then with every pass a shaded sphere is drawn with the proper color and positioning on the axes.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=red/maxvalue*100;greens=green/maxvalue*100;blues=blue/maxvalue*100;
shadedraw[ball color=red!reds!green!greens!blue!blues!] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
tikz-pgf color
add a comment |
I'm visualising color systems with TikZ to my students, and here I'm creating a 3D set of axes which represent red, green, blue and their combinations as shaded spheres.

For some reason, the result looks alright on the Blue axis, but doesn't work as expected on Green and Red axes as when their values are increased the spheres stay gray. Where's the problem?
I've taken care that everything is represented in code in the same order, RGB. Code first sets up some variables about axes (x, y, z), then step size/distance between spheres and then the max value for assumed 8-bit color, 255.
The point is that the nested for eaches go through all integer values between 0–255 for red, green and blue (all combinations), the second tikzmath creates scaled variables to be used with color information, and then with every pass a shaded sphere is drawn with the proper color and positioning on the axes.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=red/maxvalue*100;greens=green/maxvalue*100;blues=blue/maxvalue*100;
shadedraw[ball color=red!reds!green!greens!blue!blues!] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
tikz-pgf color
add a comment |
I'm visualising color systems with TikZ to my students, and here I'm creating a 3D set of axes which represent red, green, blue and their combinations as shaded spheres.

For some reason, the result looks alright on the Blue axis, but doesn't work as expected on Green and Red axes as when their values are increased the spheres stay gray. Where's the problem?
I've taken care that everything is represented in code in the same order, RGB. Code first sets up some variables about axes (x, y, z), then step size/distance between spheres and then the max value for assumed 8-bit color, 255.
The point is that the nested for eaches go through all integer values between 0–255 for red, green and blue (all combinations), the second tikzmath creates scaled variables to be used with color information, and then with every pass a shaded sphere is drawn with the proper color and positioning on the axes.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=red/maxvalue*100;greens=green/maxvalue*100;blues=blue/maxvalue*100;
shadedraw[ball color=red!reds!green!greens!blue!blues!] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
tikz-pgf color
I'm visualising color systems with TikZ to my students, and here I'm creating a 3D set of axes which represent red, green, blue and their combinations as shaded spheres.

For some reason, the result looks alright on the Blue axis, but doesn't work as expected on Green and Red axes as when their values are increased the spheres stay gray. Where's the problem?
I've taken care that everything is represented in code in the same order, RGB. Code first sets up some variables about axes (x, y, z), then step size/distance between spheres and then the max value for assumed 8-bit color, 255.
The point is that the nested for eaches go through all integer values between 0–255 for red, green and blue (all combinations), the second tikzmath creates scaled variables to be used with color information, and then with every pass a shaded sphere is drawn with the proper color and positioning on the axes.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=red/maxvalue*100;greens=green/maxvalue*100;blues=blue/maxvalue*100;
shadedraw[ball color=red!reds!green!greens!blue!blues!] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
tikz-pgf color
tikz-pgf color
asked Aug 26 '18 at 4:41
JoonasD6JoonasD6
938
938
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I do not know precisely what went wrong but I guess that there is some sort of unitarity problem when the fractions do not add up to 100. Here is some other method that does what I think you want.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolorRGBreds,greens,blues
shadedraw[ball color=mycolor] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument

EDIT: Appended a version with unique color names. With all these different viewers around one never knows. The output is the same.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolor-reds-greens-bluesRGBreds,greens,blues
shadedraw[ball color=mycolor-reds-greens-blues] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
That is very close to how I initially tried to color the spheres; I thought definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
– JoonasD6
Aug 26 '18 at 15:36
1
Did before I read your plea. You deserve it. Just had to test a bit further first. :)
– JoonasD6
Aug 26 '18 at 15:43
@JoonasD6 Thanks! I added a second version with unique color names.
– marmot
Aug 26 '18 at 15:52
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f447747%2fnested-foreach-functioning-unpredictably-when-drawing-colored-spheres-in-3-dime%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I do not know precisely what went wrong but I guess that there is some sort of unitarity problem when the fractions do not add up to 100. Here is some other method that does what I think you want.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolorRGBreds,greens,blues
shadedraw[ball color=mycolor] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument

EDIT: Appended a version with unique color names. With all these different viewers around one never knows. The output is the same.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolor-reds-greens-bluesRGBreds,greens,blues
shadedraw[ball color=mycolor-reds-greens-blues] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
That is very close to how I initially tried to color the spheres; I thought definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
– JoonasD6
Aug 26 '18 at 15:36
1
Did before I read your plea. You deserve it. Just had to test a bit further first. :)
– JoonasD6
Aug 26 '18 at 15:43
@JoonasD6 Thanks! I added a second version with unique color names.
– marmot
Aug 26 '18 at 15:52
add a comment |
I do not know precisely what went wrong but I guess that there is some sort of unitarity problem when the fractions do not add up to 100. Here is some other method that does what I think you want.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolorRGBreds,greens,blues
shadedraw[ball color=mycolor] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument

EDIT: Appended a version with unique color names. With all these different viewers around one never knows. The output is the same.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolor-reds-greens-bluesRGBreds,greens,blues
shadedraw[ball color=mycolor-reds-greens-blues] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
That is very close to how I initially tried to color the spheres; I thought definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
– JoonasD6
Aug 26 '18 at 15:36
1
Did before I read your plea. You deserve it. Just had to test a bit further first. :)
– JoonasD6
Aug 26 '18 at 15:43
@JoonasD6 Thanks! I added a second version with unique color names.
– marmot
Aug 26 '18 at 15:52
add a comment |
I do not know precisely what went wrong but I guess that there is some sort of unitarity problem when the fractions do not add up to 100. Here is some other method that does what I think you want.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolorRGBreds,greens,blues
shadedraw[ball color=mycolor] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument

EDIT: Appended a version with unique color names. With all these different viewers around one never knows. The output is the same.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolor-reds-greens-bluesRGBreds,greens,blues
shadedraw[ball color=mycolor-reds-greens-blues] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
I do not know precisely what went wrong but I guess that there is some sort of unitarity problem when the fractions do not add up to 100. Here is some other method that does what I think you want.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolorRGBreds,greens,blues
shadedraw[ball color=mycolor] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument

EDIT: Appended a version with unique color names. With all these different viewers around one never knows. The output is the same.
documentclassmemoir
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture[scale=0.03]
tikzmathx = 300; y = x; z=x; step=51;maxvalue=255; %optimal step size 51
draw[->, very thick, red] (0,0,0) -- (x,0,0) node[right]R;
draw[->, very thick, green] (0,0,0) -- (0,y,0) node[above]G;
draw[->, very thick, blue] (0,0,0) -- (0,0,z) node[below left]B;
foreach red in 0,step,...,maxvalue
foreach green in 0,step,...,maxvalue
foreach blue in 0,step,...,maxvalue
tikzmathreds=(red/maxvalue)*255;
greens=(green/maxvalue)*255;blues=(blue/maxvalue)*255;
definecolormycolor-reds-greens-bluesRGBreds,greens,blues
shadedraw[ball color=mycolor-reds-greens-blues] (red,green,blue) circle [radius=10cm];
endtikzpicture
enddocument
edited Aug 26 '18 at 15:51
answered Aug 26 '18 at 8:40
marmotmarmot
95.1k4110210
95.1k4110210
That is very close to how I initially tried to color the spheres; I thought definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
– JoonasD6
Aug 26 '18 at 15:36
1
Did before I read your plea. You deserve it. Just had to test a bit further first. :)
– JoonasD6
Aug 26 '18 at 15:43
@JoonasD6 Thanks! I added a second version with unique color names.
– marmot
Aug 26 '18 at 15:52
add a comment |
That is very close to how I initially tried to color the spheres; I thought definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
– JoonasD6
Aug 26 '18 at 15:36
1
Did before I read your plea. You deserve it. Just had to test a bit further first. :)
– JoonasD6
Aug 26 '18 at 15:43
@JoonasD6 Thanks! I added a second version with unique color names.
– marmot
Aug 26 '18 at 15:52
That is very close to how I initially tried to color the spheres; I thought definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
– JoonasD6
Aug 26 '18 at 15:36
That is very close to how I initially tried to color the spheres; I thought definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
– JoonasD6
Aug 26 '18 at 15:36
1
1
Did before I read your plea. You deserve it. Just had to test a bit further first. :)
– JoonasD6
Aug 26 '18 at 15:43
Did before I read your plea. You deserve it. Just had to test a bit further first. :)
– JoonasD6
Aug 26 '18 at 15:43
@JoonasD6 Thanks! I added a second version with unique color names.
– marmot
Aug 26 '18 at 15:52
@JoonasD6 Thanks! I added a second version with unique color names.
– marmot
Aug 26 '18 at 15:52
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f447747%2fnested-foreach-functioning-unpredictably-when-drawing-colored-spheres-in-3-dime%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

