Mercurial > hg > octave-lyh
changeset 16466:ac332eb727dd
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.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 07 Apr 2013 22:24:34 -0700 |
parents | e09e58e44c80 |
children | cc9e2751e073 |
files | scripts/image/copper.m scripts/image/hot.m scripts/image/rainbow.m |
diffstat | 3 files changed, 12 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/image/copper.m +++ b/scripts/image/copper.m @@ -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;
--- a/scripts/image/hot.m +++ b/scripts/image/hot.m @@ -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);
--- a/scripts/image/rainbow.m +++ b/scripts/image/rainbow.m @@ -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