annotate scripts/image/cubehelix.m @ 19530:5ab6fcf32d1e

New function for colormap cubehelix. * scripts/image/cubehelix.m: new colormap. This colormap uses all colors while keeping a perceived continuous intensity increase. * NEWS: list new function for 4.2. * doc/interpreter/image.txi: add new function to manual on the colormaps section.
author Carnë Draug <carandraug@octave.org>
date Mon, 29 Dec 2014 18:26:12 +0000
parents
children ed4eeb4314e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19530
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
1 ## Copyright (C) 2014 Carnë Draug
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
2 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
3 ## This file is part of Octave.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
4 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
8 ## your option) any later version.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
9 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
13 ## General Public License for more details.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
14 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
18
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{map} =} cubehelix ()
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
21 ## @deftypefnx {Function File} {@var{map} =} cubehelix (@var{n})
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
22 ## Create cubehelix colormap.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
23 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
24 ## This colormap varies from black to white going though blue, green, and red
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
25 ## tones while maintaining a monotonically increasing perception of intensity.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
26 ## This is achieved by transversing a color cube from black to white through
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
27 ## a helix, hence the name cubehelix, while taking into account the perceived
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
28 ## brightness of each channel according to the NTSC specifications from 1953.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
29 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
30 ## @example
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
31 ## rgbplot (cubehelix (256))
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
32 ## @end example
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
33 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
34 ## The argument @var{n} must be a scalar and corresponds to the lenght of the
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
35 ## colormap. Defaults to the length of the current colormap.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
36 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
37 ## Development of this colormap is described in @cite{Green, D. A., 2011,
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
38 ## "A colour scheme for the display of astronomical intensity images",
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
39 ## Bulletin of the Astronomical Society of India, 39, 289.}.
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
40 ##
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
41 ## @seealso{colormap}
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
42 ## @end deftypefn
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
43
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
44 ## Author: Carnë Draug <carandraug@octave.org>
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
45
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
46 ## PKG_ADD: colormap ("register", "cubehelix");
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
47 ## PKG_DEL: colormap ("unregister", "cubehelix");
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
48
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
49 function map = cubehelix (n = rows (colormap ()), start = 0.5,
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
50 rots = -1.5, hue = 1, gamma = 1)
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
51
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
52 if (nargin > 5)
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
53 print_usage ()
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
54 endif
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
55
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
56 if (! isscalar (n) || n < 1)
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
57 error ("cubehelix: N must be a positive scalar");
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
58 endif
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
59
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
60 coeff = [ -0.14861 -0.29227 1.97294
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
61 1.78277 -0.90649 0.00000];
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
62
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
63 fract = ((0:n-1) / (n-1))';
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
64 angle = 2 * pi * (start/3 + 1 + rots*fract);
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
65 fract = fract .^ gamma;
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
66 amp = hue * fract .* (1-fract) /2;
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
67 warning ("off", "Octave:broadcast", "local");
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
68 map = fract + amp .* ([cos(angle) sin(angle)] * coeff);
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
69
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
70 ## Clip values (only in case users have changed values of hue or gamma)
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
71 map(map < 0) = 0;
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
72 map(map > 1) = 1;
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
73
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
74 endfunction
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
75
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
76 %!demo
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
77 %! subplot (1, 2, 1)
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
78 %! rgbplot (cubehelix (256), "composite")
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
79 %! subplot (1, 2, 2)
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
80 %! rgbplot (cubehelix (256))
5ab6fcf32d1e New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
81