View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Colorspace Transformations

4.9 | 12 ratings Rate this file 132 Downloads (last 30 days) File Size: 66.2 KB File ID: #28790 Version: 1.4
image thumbnail

Colorspace Transformations


Pascal Getreuer (view profile)


21 Sep 2010 (Updated )

Transform colors between sRGB, YCbCr, YPbPr, YUV, YIQ, HSV, HSL, HSI, XYZ, Lab, Luv, LCH, CAT02 LMS

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

This package converts colors between sRGB, Y'PbPr, Y'CbCr, JPEG-Y'CbCr, Y'UV, Y'IQ, Y'DbDr, HSV, HSL, HSI, CIE XYZ, CIE L*a*b* (CIELAB), CIE L*u*v* (CIELUV), and CIE L*ch (CIELCH), and CIE CAT02 LMS. It can be used either as part of a C/C++ program or compiled as a MATLAB MEX function.

For use in Matlab, colorspace is compiled as a MEX function by entering

mex colorspace.c

on the Matlab command console. As an alternative to MEX, a pure M-code version colorspace.m is also included.

For use in C programs, an example command line program colorcalc is included.

B = colorspace(S,A) converts the color representation of image A where S is a string specifying the conversion. S tells the source and destination color spaces, S = 'dest<-src', or alternatively, S = 'src->dest'. Supported color spaces are
'RGB' = sRGB IEC 61966-2-1
'YPbPr' = Luma (ITU-R BT.601) + Chroma
'YCbCr' = Luma + Chroma
'JPEG-YCbCr' = space used in JPEG
'YUV' = NTSC PAL Y'UV Luma + Chroma
'YIQ' = NTSC Y'IQ Luma + Chroma
'YDbDr' = SECAM Luma + Chroma
'HSV' or 'HSB' = Hue Saturation Value/Brightness
'HSL' or 'HLS' = Hue Saturation Luminance
'HSI' = Hue Saturation Intensity
'Lab' = CIE L*a*b* (CIELAB)
'Luv' = CIE L*u*v* (CIELUV)

All transforms assume 2 degree observer and D65 illuminant. Color space names are case insensitive. When sRGB is the source or destination, it can be omitted. For example 'yuv<-' is short for 'yuv<-rgb'.


This file inspired Hu Sl < > Rgb Image Conversion Tools, Image Manipulation Toolbox, Image Blending Functions, Cmocean Perceptually Uniform Colormaps, Generate Maximally Perceptually Distinct Colors, Explore Experimental Data, Perceptually Improved Colormaps, Hslcolormap, Hsi < > Rgb Image Conversion Tools, and Cbrewer2.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (22)
02 Feb 2017 Klaus Villforth

The Colorspace Transformations by Pascal Getreuer assume D65/2°, which is a two degree observer angle and a D65 illuminant. However, D65/10° with a ten degree observer is the common standard beside C/2° and D50/2°. The use of D65/2° may conflict with international standards.

17 Jan 2017 KAE

KAE (view profile)

colorspace_ciedemo is a nice plot of the CIE1931 diagram with pure colors labeled, which is useful.

Comment only
08 Jun 2016 Baptiste

10 May 2016 Thomas

Thomas (view profile)

First of all thanks for this excellent contribution.

Checking up the Luv computation i found, that you use the (pre-2004 CIE standard) decimal approximation 0.008856 for some conversions instead of the exact rational 216/24389.
( See here: )

Not a big deal, but changing line 484 to
i = (Y <= 216/24389)
should be exact. Similarly line 491, i suppose.

(sorry for the accidental double post)

Comment only
10 May 2016 Thomas

Thomas (view profile)

First of all thanks for this excellent contribution.

Checking up the Luv computation i found, that you use the (pre-2004 CIE standard) decimal approximation 0.008856 for some conversions instead of the exact rational 216/24389.
( See here: )

Not a big deal, but changing line 484 to

16 Mar 2016 Cem

Cem (view profile)

Thanks for the contribution. Awesome!

Comment only
09 Feb 2016 Chad Greene

Chad Greene (view profile)

Excellent. Thanks for sharing.

06 Nov 2015 Royi Avital

Great submission.

Any chance to add CMYK (One of the profiles, SWOP for instance)?

Thank You.

Comment only
22 Oct 2015 zz

zz (view profile)


24 Sep 2015 DGM

Maybe I'm just missing something that would be obvious to someone that knows more about this, but there does not appear to be any implementation of HSI conversion in the M-code. The colorspace string 'hsi' is mapped directly to 'hsl', and certainly both HSL and HSI conversions appear to return the exact same results.
There does appear to be an implementation in the c code, however.
I thought about just writing a function myself, but I rather like the convenience of using colorspace().

Comment only
14 May 2014 John

John (view profile)

17 Jul 2013 Jan Neggers

awesome tool, I luv colorspaces!

12 May 2013 Jurgen

Jurgen (view profile)

eric and x.d, I remember reading something like "the Image Processing Toolbox conversion is based on ICC profiles, and it uses a D65 to D50 Bradford adaptation transformation to the D50 white point." That may explain the differences?

Comment only
01 Apr 2012 x.d

x.d (view profile)

Thanks for your great work!
I have used the function “colorspace( )” which can be found in the page of "" as follows:

rgb = imread('peppers.png');
C = makecform('srgb2lab');
lab = applycform(rgb,C);

lab_2 = colorspace('lab<-rgb', rgb);
lab_3 = lab2uint8(lab_2);

I was surprised that the values in lab and lab_3 were quite different. I want to know the mistake in my code or my logical.

Thanks a lot.

02 Mar 2012 Eric

Eric (view profile)

Could you comment on the differences between your function and what comes with the image processing toolbox? Specifically, for the same XYZ values, why do I get different results using colorspace as compared to makecform and applycform? Are there arguments to makecform that would yield results consistent with colorspace? As an example, I have been using Spectral and XYZ Color Functions ( and in the function spectrumRGB I have replaced the makecform and applycform calls with a call to colorspace and comparing the results.


Comment only
07 Dec 2011 Nathan Orloff

For the same images, this is about 2 times as fast as:

cform = makecform('srgb2lab');
lab_Ia = applycform(Ia,cform);

Which is fantastic.

20 May 2011 Yajun

Yajun (view profile)

thanks very much for your good job,it has been an obstacle for me for a long time

25 Oct 2010 Rob Chambers

Works great, appreciate the MEX code. Far more useful and well-done than matlab's conversions. I've been very impressed with all your stuff, Pascal :)

15 Oct 2010 Matteo Niccoli

I used Colorspace in conjunction with cline
( ) for an example in my Perceptually improved colormaps submission.
I used them this way:

% J=jet(256);
% LJ=colorspace('RGB->Lab',J);
% figure; cline([1:1:256],LJ(:,1),LJ(:,1),[1:1:256],J);
% title ('L* plot for jet colormap','Color','k','FontSize',12);

Also, as I was reading Steve's post on Lab based colormaps (thank you Steve!), I used colorspace to do the transformations as I do not have the Image Processign Toolbox ). This helped me visualize things (notice again I used it togeter with cline):

% radius = 50;
% theta = linspace(0, pi/2, 256).';
% a = radius * cos(theta);
% b = radius * sin(theta);
% L = linspace(0, 100, 256).';
% Lab = [L, a, b];
% RGBhlx=colorspace('RGB<-Lab',(Lab));
% figure; cline(a, b, L, L,RGBhlx); view(3);
% VIEW(-165,-15);

Comment only
13 Oct 2010 Matteo Niccoli

Thanks for your comments Pascal. I used your 2006 implementation many many times, and I recently used this version for the Perceptually improved colormaps. It worked well for me.

30 Sep 2010 Pascal Getreuer

Pascal Getreuer (view profile)

Thanks, Matteo!

The difference between this package and my previous 2006-2008 Color Space Converter is that colorspace is now implemented in C code. There are two advantages to this. First, colorspace now runs in MATLAB as a MEX function, which is noticeably more efficient than the previous M-code version. Second, colorspace can also be used from normal C and C++ programs---MATLAB is not required to use colorspace.

In case MEX is not an option, a pure M-code version of colorspace is also included (effectively an updated version of the previous colorspace).

Additionally, the sRGB definition of gamma-corrected RGB is now used, and two spaces have been added (HSI and CIE CAT02 LMS).

Comment only
24 Sep 2010 Matteo Niccoli

Hi Pascal
Great work!

Could you add in here comments/details on the differences between this and the earlier version (2006 Color Space Converter -updated 2008)?

Thank you

Comment only
29 Sep 2010 1.3

* Added missing colorspace.h file---thanks to William Cook
* Added M-code version colorspace.m
* Added JPEG-Y'CbCr
* ITU-R BT.709 gamma correction replaced with more standard sRGB definition
* Fix in Xyz2Luv
* Fix in CIE tongue demo

14 Jan 2011 1.4

added tags, minor update in documentation

Contact us