# HG changeset patch # User Rik # Date 1365398674 25200 # Node ID ac332eb727ddaf3554c5607fb1acce87444f9fae # Parent e09e58e44c805d75e5b5f9073dfcdae46dcf3482 Simplify calculation of colormaps * scripts/image/copper.m: Replace slower linspace with range operator. * scripts/image/hot.m: Add test for n==2 to allow elimination of subsequent if conditional tests. Replace mod calculation with simpler expression. * scripts/image/rainbow.m: Replace slower linspace with range operator. Align columns of calculation for better code readability. diff -r e09e58e44c80 -r ac332eb727dd scripts/image/copper.m --- a/scripts/image/copper.m Sun Apr 07 17:24:53 2013 -0700 +++ b/scripts/image/copper.m Sun Apr 07 22:24:34 2013 -0700 @@ -46,7 +46,7 @@ if (n == 1) map = [0, 0, 0]; elseif (n > 1) - x = linspace (0, 1, n)'; + x = [0:(n-1)]' / (n - 1); r = (x < 4/5) .* (5/4 * x) ... + (x >= 4/5); g = 0.7812 * x; diff -r e09e58e44c80 -r ac332eb727dd scripts/image/hot.m --- a/scripts/image/hot.m Sun Apr 07 17:24:53 2013 -0700 +++ b/scripts/image/hot.m Sun Apr 07 22:24:34 2013 -0700 @@ -45,23 +45,21 @@ if (n == 1) map = [1, 1, 1]; - elseif (n > 1) + elseif (n == 2) + map = [1, 1, 1/2 + 1, 1, 1 ]; + elseif (n > 2) idx = floor (3/8 * n); nel = idx; r = ones (n, 1); - if (nel > 0) - r(1:idx, 1) = [1:nel]' / nel; - endif + r(1:idx, 1) = [1:nel]' / nel; g = zeros (n, 1); g(idx+1:2*idx, 1) = r(1:idx); g(2*idx+1:end, 1) = 1; - idx = floor (3/4 * n); - if (any (mod (n, 8) == [0, 1, 3, 6])) - idx++; - endif + idx = 2*idx + 1; # approximately 3/4 *n nel = n - idx + 1; b = zeros (n, 1); diff -r e09e58e44c80 -r ac332eb727dd scripts/image/rainbow.m --- a/scripts/image/rainbow.m Sun Apr 07 17:24:53 2013 -0700 +++ b/scripts/image/rainbow.m Sun Apr 07 22:24:34 2013 -0700 @@ -49,17 +49,18 @@ if (n == 1) map = [1, 0, 0]; elseif (n > 1) - x = linspace (0, 1, n)'; + x = [0:(n-1)]' / (n - 1); - r = ((x < 2/5) + r = ( (x < 2/5) + (x >= 2/5 & x < 3/5) .* (-5 * x + 3) + (x >= 4/5) .* (10/3 * x - 8/3)); - g = ((x < 2/5) .* (5/2 * x) + g = ( (x < 2/5) .* (5/2 * x) + (x >= 2/5 & x < 3/5) + (x >= 3/5 & x < 4/5) .* (-5 * x + 4)); - b = (x >= 3/5 & x < 4/5) .* (5 * x - 3) + (x >= 4/5); + b = ( (x >= 3/5 & x < 4/5) .* (5 * x - 3) + + (x >= 4/5)); map = [r, g, b]; else