MATLAB Answers


A formula of color: how does MATLAB link indexed value of color with RGB format?

Asked by Igor
on 18 Jan 2012
Accepted Answer by Jan
As known, MATLAB can plot graphics with colorbar and chosen colormap and also read/write images in RGB-scheme. And two color model are used, indexed (defined by double type C in range 0..1) and RGB.
How easy to define analytically function F:C->(R,G,B), i.e. r=r(c),g=g(c),b=b(c)?
With natural aasumptions: F(0)=(0,0,0),F(1)=(1,1,1) and F(?)=(1,0,0),.. F(?)=(0,1,1)
It would be helpful for intellectual colormaps....


Sign in to comment.

1 Answer

Answer by Jan
on 18 Jan 2012
 Accepted Answer

As far as I know, you cannot map colors to a single parameter using an analytical function. The RGB colorspace uses 3 componnts, which cannot be projected onto a line.
The standard method is a lookup table using colormap.
Java can use a 24 bit value as a RGB color applying something like this
RGB = uint8(floor(rand(40, 20, 3) * 256));
Ind = RGB(:, 1) + 256 * RGB(:,2) + 65535 * RGB(:, 3);
Usually the alpha channel is added also and the colors are stored with 32 bits.
What do you want to achieve?

  1 Comment

I think that simplest way to define this "like as" analytic function (thank you for code) is:
fist byte of C -- R-value
second -- G
third -- B
fourth (power) -- transparency
And the goal is achieved.
But it's interesting to imagine such function F as math formula.

Sign in to comment.