cmaplevel.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. function [cmap] = cmaplevel(levels, colors, cmin, cmax, num_colors)
  2. %CMAPLEVEL Create colormap from specified levels and colors
  3. %
  4. % Description:
  5. % Pixel value within [Lv(k) Lv(k+1)) assigned to RGB color(k,:)
  6. % Usage:
  7. % [cmap] = cmaplevel(levels, colors, cmin, cmax, num_colors)
  8. % Input:
  9. % c = 1x3 float of RGB values between
  10. % Output:
  11. % str = html color code format, e.g. color(23, 47, 83)
  12. %
  13. % Xiaohu Mo
  14. % TODO input checking
  15. % TODO specify number of color levels
  16. %%
  17. if cmin > cmax
  18. cmap = [];
  19. return;
  20. elseif cmin == cmax % clevels will be NaN
  21. [tilde, ind] = max(levels);
  22. cmap = repmat(colors(ind, :), [num_colors, 1]);
  23. return;
  24. end
  25. if ~isvector(levels)
  26. cmap = [];
  27. return;
  28. end
  29. %% sort the levels and colors
  30. t = horzcat(levels(:), colors);
  31. t = sortrows(t, 1);
  32. levels = t(:, 1);
  33. colors = t(:, 2:4);
  34. %% indices of the absolute levels on colormap
  35. clevels = fix((levels-cmin)/(cmax-cmin)*num_colors) + 1;
  36. %% create cmap
  37. cmap = zeros(num_colors, 3);
  38. % add the last level below cmin if exist
  39. indices = find(clevels < 1, 1, 'last');
  40. % all useful levels
  41. indices = [indices find(clevels >= 1 & clevels <= num_colors)];
  42. % fill the colormap
  43. for ind = indices:numel(levels)
  44. cmap(clevels(ind):num_colors, :) = repmat(colors(ind, :), [num_colors-clevels(ind)+1 1]);
  45. end