bluewhitered.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. %Nathan Childress (2024). bluewhitered (https://www.mathworks.com/matlabcentral/fileexchange/4058-bluewhitered), MATLAB Central File Exchange.
  2. function newmap = bluewhitered(m)
  3. %BLUEWHITERED Blue, white, and red color map.
  4. % BLUEWHITERED(M) returns an M-by-3 matrix containing a blue to white
  5. % to red colormap, with white corresponding to the CAXIS value closest
  6. % to zero. This colormap is most useful for images and surface plots
  7. % with positive and negative values. BLUEWHITERED, by itself, is the
  8. % same length as the current colormap.
  9. %
  10. % Examples:
  11. % ------------------------------
  12. % figure
  13. % imagesc(peaks(250));
  14. % colormap(bluewhitered(256)), colorbar
  15. %
  16. % figure
  17. % imagesc(peaks(250), [0 8])
  18. % colormap(bluewhitered), colorbar
  19. %
  20. % figure
  21. % imagesc(peaks(250), [-6 0])
  22. % colormap(bluewhitered), colorbar
  23. %
  24. % figure
  25. % surf(peaks)
  26. % colormap(bluewhitered)
  27. % axis tight
  28. %
  29. % See also HSV, HOT, COOL, BONE, COPPER, PINK, FLAG,
  30. % COLORMAP, RGBPLOT.
  31. if nargin < 1
  32. m = size(get(gcf,'colormap'),1);
  33. end
  34. % default
  35. bottom = [0 0 0.5];
  36. botmiddle = [0 0.5 1];
  37. middle = [1 1 1];
  38. topmiddle = [1 0 0];
  39. top = [0.5 0 0];
  40. % custom colour code
  41. % {'#2980B9','#5499C7','#7FB3D5','#A9CCE3','#D4E6F1','#EAF2F8',...
  42. % '#FBEEE6','#F6DDCC','#EDBB99','#E59866','#DC7633','#D35400'}
  43. % bottom = hex2rgb('2980B9');%hex2rgb('2980B9')*255
  44. % botmiddle = hex2rgb('7FB3D5');%hex2rgb('7FB3D5')*255
  45. % middle = [1 1 1];
  46. % topmiddle = hex2rgb('E59866');%hex2rgb('E59866')*255
  47. % top = hex2rgb('D35400');%hex2rgb('D35400')*255
  48. % Find middle
  49. lims = get(gca, 'CLim');
  50. % Find ratio of negative to positive
  51. if (lims(1) < 0) & (lims(2) > 0)
  52. % It has both negative and positive
  53. % Find ratio of negative to positive
  54. ratio = abs(lims(1)) / (abs(lims(1)) + lims(2));
  55. neglen = round(m*ratio);
  56. poslen = m - neglen;
  57. % Just negative
  58. new = [bottom; botmiddle; middle];
  59. len = length(new);
  60. oldsteps = linspace(0, 1, len);
  61. newsteps = linspace(0, 1, neglen);
  62. newmap1 = zeros(neglen, 3);
  63. for i=1:3
  64. % Interpolate over RGB spaces of colormap
  65. newmap1(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
  66. end
  67. % Just positive
  68. new = [middle; topmiddle; top];
  69. len = length(new);
  70. oldsteps = linspace(0, 1, len);
  71. newsteps = linspace(0, 1, poslen);
  72. newmap = zeros(poslen, 3);
  73. for i=1:3
  74. % Interpolate over RGB spaces of colormap
  75. newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
  76. end
  77. % And put 'em together
  78. newmap = [newmap1; newmap];
  79. elseif lims(1) >= 0
  80. % Just positive
  81. new = [middle; topmiddle; top];
  82. len = length(new);
  83. oldsteps = linspace(0, 1, len);
  84. newsteps = linspace(0, 1, m);
  85. newmap = zeros(m, 3);
  86. for i=1:3
  87. % Interpolate over RGB spaces of colormap
  88. newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
  89. end
  90. else
  91. % Just negative
  92. new = [bottom; botmiddle; middle];
  93. len = length(new);
  94. oldsteps = linspace(0, 1, len);
  95. newsteps = linspace(0, 1, m);
  96. newmap = zeros(m, 3);
  97. for i=1:3
  98. % Interpolate over RGB spaces of colormap
  99. newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
  100. end
  101. end
  102. %
  103. % m = 64;
  104. % new = [bottom; botmiddle; middle; topmiddle; top];
  105. % % x = 1:m;
  106. %
  107. % oldsteps = linspace(0, 1, 5);
  108. % newsteps = linspace(0, 1, m);
  109. % newmap = zeros(m, 3);
  110. %
  111. % for i=1:3
  112. % % Interpolate over RGB spaces of colormap
  113. % newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
  114. % end
  115. %
  116. % % set(gcf, 'colormap', newmap), colorbar