function colorcubes(n) % COLORCUBES A cube of cubes in the RGB color space. % COLORCUBES, with no arguments, shows 5^3 = 125 cubes with % colors equally spaced in the RGB color space. % COLORCUBES(n) shows n-by-n-by-n colors. % COLORCUBES(2) shows 8 colors: R, G, B, C, M, Y, W, K (black). % Rotate the cube with the mouse or arrow keys. % Change n with the plus and minus buttons. % Copyright 2016-2022 Cleve Moler if nargin < 1 n = 5; end initgraphics(n); w = 0.85; [x,y,z] = cube(w); m = n-1; for i = m:-1:0 for j = m:-1:0 for k = 0:m r = k/m; g = 1-j/m; b = 1-i/m; if n == 1 [r,g,b] = deal(.5,.5,.5); end surface(i+x,j+y,k+z, ... 'facecolor',[r g b], ... 'facelighting','gouraud'); end %k end %j end %i % ------------------------ % INITGRAPHCS Inialize the colorcubes axis. % INITGRAPHICS(n) for n-by-n-by-n display. function n = initgraphics(n) clf reset shg set(gcf,'color','white', ... 'name','colorcubes', ... 'numbertitle','off') axis([-n/4 5*n/4 -n/4 5*n/4 -n/4 5*n/4]); axis off axis vis3d view(3) rotate3d set(gca,'clipping','on', ... 'userdata',n) uicontrol('string','+', ... 'units','normalized', ... 'position',[.22 .88 .06 .06], ... 'fontsize',16, ... 'fontweight','bold', ... 'callback',@plus_cb); uicontrol('string','-', ... 'units','normalized', ... 'position',[.14 .88 .06 .06], ... 'fontsize',16, ... 'fontweight','bold', ... 'callback',@minus_cb); end %initgraphics function [x,y,z] = cube(w) % CUBE Coordinates of the faces of a cube. % [x,y,z] = cube(w); surface(x,y,z) % plots a cube of with w. u = [0 0; 0 0; w w; w w]; v = [0 w; 0 w; 0 w; 0 w]; z = [w w; 0 0; 0 0; w w]; s = [nan nan]; x = [u; s; v]; y = [v; s; u]; z = [z; s; w-z]; end %cube function plus_cb(~,~) n = get(gca,'userdata'); n = n+1; colorcubes(n) end function minus_cb(~,~) n = get(gca,'userdata'); n = max(1,n-1); colorcubes(n) end end % colorcubes