view inst/colorgradient.m @ 886:a0c42a32c6c4

Move conversion between each image type to imcast. * im2double.m, im2int16.m, im2single.m, im2uint16.m, im2uint8.m: move code into imcast. Fix several small bugs mainly dealing with indexed and logical images, small precision for Matlab compatibility, and increased performance. Expanded documentation. Added new tests. * imcast.m: implement the conversion between each image type instead of being a wrapper around functions for each conversion. This reduces code duplication, and places all very similar (but not equal) code together so that a bug fix in one can be easily notices that requires application in others. Expand documetation. Add more tests. * private/imconversion.m, private/im2float.m: remove no longer necessary function since this has all been moved to imcast. * COPYING: remove license for private/im2float.m and private/imconversion.m. * NEWS: make note of bug fixes for this functions.
author Carnë Draug <carandraug@octave.org>
date Mon, 24 Mar 2014 22:00:05 +0000
parents 1d04ebf01532
children
line wrap: on
line source

## Author: Paul Kienzle <pkienzle@users.sf.net>
## This program is granted to the public domain.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{M} =} colorgradient (@var{C}, @var{w}, @var{n})
## Define a colour map which smoothly traverses the given colors.
## @var{C} contains the colours, one row per r,g,b value.
## @var{w}(i) is the relative length of the transition from colour i to colour i+1
## in the entire gradient.  The default is ones(rows(C)-1,1).
## n is the length of the colour map.  The default is rows(colormap).
##
## E.g.,
## @example 
## colorgradient([0,0,1; 1,1,0; 1,0,0])  # blue -> yellow -> red
## x = linspace(0,1,200);
## imagesc(x(:,ones(30,1)))';
## @end example
## @end deftypefn

function ret = colorgradient (C, w, n)
  if nargin < 1 || nargin > 3
    print_usage;
  endif

  if nargin == 1
    n = rows(colormap);
    w = ones(length(C)-1,1);
  elseif nargin == 2
    if (length(w) == 1)
      n = w;
      w = ones(rows(C)-1,1);
    else
      n = rows(colormap);
    endif
  endif

  if (length(w)+1 != rows(C))
    error("must have one weight for each color interval");
  endif

  w = 1+round((n-1)*cumsum([0;w(:)])/sum(w));
  map = zeros(n,3);
  for i=1:length(w)-1
    if (w(i) != w(i+1))
      map(w(i):w(i+1),1) = linspace(C(i,1),C(i+1,1),w(i+1)-w(i)+1)';
      map(w(i):w(i+1),2) = linspace(C(i,2),C(i+1,2),w(i+1)-w(i)+1)';
      map(w(i):w(i+1),3) = linspace(C(i,3),C(i+1,3),w(i+1)-w(i)+1)';
    endif
  endfor

  if nargout == 0
    colormap(map);
  else
    ret = map;
  endif
endfunction