]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/colorgradient.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / colorgradient.m
1 ## -*- texinfo -*-
2 ## @deftypefn {Function File} @var{M} = colorgradient(@var{C}, @var{w}, @var{n})
3 ## Define a colour map which smoothly traverses the given colors.
4 ## @var{C} contains the colours, one row per r,g,b value.
5 ## @var{w}(i) is the relative length of the transition from colour i to colour i+1
6 ## in the entire gradient.  The default is ones(rows(C)-1,1).
7 ## n is the length of the colour map.  The default is rows(colormap).
8 ##
9 ## E.g.,
10 ## @example 
11 ## colorgradient([0,0,1; 1,1,0; 1,0,0])  # blue -> yellow -> red
12 ## x = linspace(0,1,200);
13 ## imagesc(x(:,ones(30,1)))';
14 ## @end example
15 ## @end deftypefn
16
17 ## This program is granted to the public domain.
18 ## Author: Paul Kienzle <pkienzle@users.sf.net>
19
20 function ret = colorgradient(C,w,n)
21   if nargin < 1 || nargin > 3
22     usage("M = colorgradient(C,w,n)")
23   endif
24
25   if nargin == 1
26     n = rows(colormap);
27     w = ones(length(C)-1,1);
28   elseif nargin == 2
29     if (length(w) == 1)
30       n = w;
31       w = ones(rows(C)-1,1);
32     else
33       n = rows(colormap);
34     endif
35   endif
36
37   if (length(w)+1 != rows(C))
38     error("must have one weight for each color interval");
39   endif
40
41   w = 1+round((n-1)*cumsum([0;w(:)])/sum(w));
42   map = zeros(n,3);
43   for i=1:length(w)-1
44     if (w(i) != w(i+1))
45       map(w(i):w(i+1),1) = linspace(C(i,1),C(i+1,1),w(i+1)-w(i)+1)';
46       map(w(i):w(i+1),2) = linspace(C(i,2),C(i+1,2),w(i+1)-w(i)+1)';
47       map(w(i):w(i+1),3) = linspace(C(i,3),C(i+1,3),w(i+1)-w(i)+1)';
48     endif
49   endfor
50
51   if nargout == 0
52     colormap(map);
53   else
54     ret = map;
55   endif
56 endfunction