File Exchange

image thumbnail

Build custom colormaps

version (2.05 KB) by M Sohrabinia
Buillds any sequence of colormaps based on wrgbcmyk colors


Updated 14 Feb 2013

View License

This function can be used to build your own custom colormaps. Imagine if you want to display rainfall distribution map. You want a colormap which ideally brings rainfall in mind, which is not achieved by colormaps such as winter, cool or jet and such. A gradient of white to blue will do the
task, but you might also use a more complex gradient (such as white+blue+red or colors='wbr'). This function can be use to build any colormap using main colors rgbcmyk. In image processing, w (white) can be used as the first color so that in the output, the background (usually with 0 values) appears white. In the example of rainfall map, 'wb' will produce a rainfall density map where the background (if its DN values are 0) will appear as white.

colors: string (char) of color codes, any sequence of rgbcmywk
representing different colors (such as 'b' for blue) is acceptable. If a
gradient of white to blue is needed, colors would be 'wb'; a rainbow of
white+blue+red+green would be 'wbrg'.

%try the output cmap:
imshow(im), colorbar
colormap(cmap) %will use the output colormap

Cite As

M Sohrabinia (2021). Build custom colormaps (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)



Linwei Hu

Sorry for interrupting. I found the way to modify the levels in your function code.

Best regards

siddharth rawat

how to add shades of red and black? I want to make a custom colormap starting from black to shades of red.

Adam Barton

Shay Ben-Sasson

If demo example does not work on 2017a, use colormap(gca, cmap)

Shay Ben-Sasson

Mohammad Amin Maddah

Mohammad Amin Maddah

Michael Stritt

Thank you very much, saved my day working on my bachelor thesis

Rafael Madrid


Thanks a lot! I was looking for this kind of function, you have just made my life easier!

M Sohrabinia

@Alfredo: thanks for your comment, you mean the range of the output? such a 0 to 1?


Very Good! It would be nice to have also the possibility to specify the desired size of the output colormap.

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!