comparison scripts/image/cubehelix.m @ 19645:ed4eeb4314e6

cubehelix: fix for the corner cases when N is 1 or 0.
author Carnë Draug <carandraug@octave.org>
date Fri, 23 Jan 2015 15:28:45 +0000
parents 5ab6fcf32d1e
children e8e3a89fa370
comparison
equal deleted inserted replaced
19644:965384560122 19645:ed4eeb4314e6
49 function map = cubehelix (n = rows (colormap ()), start = 0.5, 49 function map = cubehelix (n = rows (colormap ()), start = 0.5,
50 rots = -1.5, hue = 1, gamma = 1) 50 rots = -1.5, hue = 1, gamma = 1)
51 51
52 if (nargin > 5) 52 if (nargin > 5)
53 print_usage () 53 print_usage ()
54 elseif (! isscalar (n))
55 error ("cubehelix: N must be a scalar");
54 endif 56 endif
55 57
56 if (! isscalar (n) || n < 1) 58 if (n > 1)
57 error ("cubehelix: N must be a positive scalar"); 59 coeff = [ -0.14861 -0.29227 1.97294
60 1.78277 -0.90649 0.00000];
61
62 fract = ((0:n-1) / (n-1))';
63 angle = 2 * pi * (start/3 + 1 + rots*fract);
64 fract = fract .^ gamma;
65 amp = hue * fract .* (1-fract) /2;
66 warning ("off", "Octave:broadcast", "local");
67 map = fract + amp .* ([cos(angle) sin(angle)] * coeff);
68
69 ## Clip values (only in case users have changed values of hue or gamma)
70 map(map < 0) = 0;
71 map(map > 1) = 1;
72
73 elseif (n > 0)
74 map = [0, 0, 0];
75 else
76 map = zeros (0, 3);
58 endif 77 endif
59
60 coeff = [ -0.14861 -0.29227 1.97294
61 1.78277 -0.90649 0.00000];
62
63 fract = ((0:n-1) / (n-1))';
64 angle = 2 * pi * (start/3 + 1 + rots*fract);
65 fract = fract .^ gamma;
66 amp = hue * fract .* (1-fract) /2;
67 warning ("off", "Octave:broadcast", "local");
68 map = fract + amp .* ([cos(angle) sin(angle)] * coeff);
69
70 ## Clip values (only in case users have changed values of hue or gamma)
71 map(map < 0) = 0;
72 map(map > 1) = 1;
73 78
74 endfunction 79 endfunction
75 80
76 %!demo 81 %!demo
77 %! subplot (1, 2, 1) 82 %! subplot (1, 2, 1)