Mercurial > octave-nkf
annotate scripts/image/cubehelix.m @ 19647:e8e3a89fa370
Fix colormap functions when N is not of class double (bug #44070)
* autumn.m, bone.m, cool.m, copper.m, cubehelix.m, gray.m, hsv.m, ocean.m,
rainbow.m, spring.m, summer.m, winter.m: cast N to double since in this
colormap functions, N is used directly used in the operations and ends up
casting colormap to something else. Note that a colormap MUST be of class
double. Also, if N is an integer class, all values in colormap end up 0
or 1. Also simplify code for input checking by declaring default on function
signature line.
* flag.m, gmap40.m, hot.m, jet.m, lines.m, pink.m, prism.m, white.m: simplify
input check like the other colormap functions.
* test/colormaps.tst: add new test file to test all colormaps at once.
* test/Makefile.am: add new test file to build system.
used to create the colormap and if less
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Fri, 23 Jan 2015 15:37:56 +0000 |
parents | ed4eeb4314e6 |
children | 4197fc428c7d |
rev | line source |
---|---|
19564
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 () |
19645
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
54 elseif (! isscalar (n)) |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
55 error ("cubehelix: N must be a scalar"); |
19564
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
56 endif |
19647
e8e3a89fa370
Fix colormap functions when N is not of class double (bug #44070)
Carnë Draug <carandraug@octave.org>
parents:
19645
diff
changeset
|
57 n = double (n); |
19564
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
58 |
19645
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
59 if (n > 1) |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
60 coeff = [ -0.14861 -0.29227 1.97294 |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
61 1.78277 -0.90649 0.00000]; |
19564
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
62 |
19645
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
63 fract = ((0:n-1) / (n-1))'; |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
64 angle = 2 * pi * (start/3 + 1 + rots*fract); |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
65 fract = fract .^ gamma; |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
66 amp = hue * fract .* (1-fract) /2; |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
67 warning ("off", "Octave:broadcast", "local"); |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
68 map = fract + amp .* ([cos(angle) sin(angle)] * coeff); |
19564
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
69 |
19645
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
70 ## Clip values (only in case users have changed values of hue or gamma) |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
71 map(map < 0) = 0; |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
72 map(map > 1) = 1; |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
73 |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
74 elseif (n > 0) |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
75 map = [0, 0, 0]; |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
76 else |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
77 map = zeros (0, 3); |
ed4eeb4314e6
cubehelix: fix for the corner cases when N is 1 or 0.
Carnë Draug <carandraug@octave.org>
parents:
19564
diff
changeset
|
78 endif |
19564
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
79 |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
80 endfunction |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
81 |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
82 %!demo |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
83 %! subplot (1, 2, 1) |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
84 %! rgbplot (cubehelix (256), "composite") |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
85 %! subplot (1, 2, 2) |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
86 %! rgbplot (cubehelix (256)) |
5ab6fcf32d1e
New function for colormap cubehelix.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
87 |