I'd like to create a colormap in matlab. I want to fade the color according to gray level of pixel.
For example:
from 255 to 160 -> white
from 159 to 120 -> pink
from 119 to 50 -> brown
from 49 to 0 -> dark
I'd like to create a colormap in matlab. I want to fade the color according to gray level of pixel.
For example:
from 255 to 160 -> white
from 159 to 120 -> pink
from 119 to 50 -> brown
from 49 to 0 -> dark
From the docs:
OK so to start we are going to create an m-by-3 matrix, in your case m is 161:
Now you want the bottom to be dark (I'm going with black) and the 50th point to be brown. but lets go with red as an example as it's easier. RGB triples for black and red respectively:
[0,0,0]
and[1,0,0]
OK so currently our enitre colormap is black. We know we want
map(50,:) = [1, 0 ,0]
i.e. red but now we want a gradient in between right? So lets use linspace for this (note that there is a better method usinginterp1
instead oflinspace
at the end of this answer):putting this in the map:
So now lets use brown instead of red, to get the triple from that link divide each colour component by 255 so our triple is
t = [101, 67, 33]./255
. OK so now just repeat that linspace procedure for each colour:And now repeat for each of your other nodes.
For example:
An alternative to using
linspace
once per channel individually and repeating this for each colour is to use linear interpolation.Create a matrix where each row is a color triple
And now make a vector of what range each color should be at (i.e. this vector defines the spacing of the colours, they need not be regularly/equally spaced):
And finally you can create the entire map with one simple interpolation:
testing
Alternatively you may use
AdvancedColormap
function from FEX. If you need a smooth colormap, you may do it like this: