Newsgroups: comp.graphics,sci.image.processing
Path: cantaloupe.srv.cs.cmu.edu!nntp.club.cc.cmu.edu!miner.usbm.gov!rsg1.er.usgs.gov!jobone!newsxfer.itd.umich.edu!gatech!swrinde!pipex!uknet!pcl!ajoec1
From: ajoec1@westminster.ac.uk (Adrian Ford)
Subject: New colour FAQ
Message-ID: <D0C7M1.FH0@westminster.ac.uk>
Summary:  Replacement for the unmaintained color spaces faq.
Organization: University of Westminster
X-Newsreader: TIN [version 1.2 PL2]
Date: Mon, 5 Dec 1994 12:20:24 GMT
Lines: 1116
Xref: glinda.oz.cs.cmu.edu comp.graphics:65589 sci.image.processing:11271

 ------------------------------------------------------------------------
                      Colour Space Conversions.
------------------------------------------------------------------------
Complied by Alan Roberts (Alan.Roberts@rd.bbc.co.uk) and Adrian Ford 
(ajoec1@westminster.ac.uk) This document may be reproduced and 
distributed freely, provided that you do not alter it in any way,
credit its source and that no charge is made for it. 

Home:- ftp westminster.ac.uk in directory /pub/itrg

Description:
This document should be used instead of the Colour Spaces FAQ, issued by
David Bourgin (The Best Player). Some of the equations given in David's
FAQ are incorrect, and the explanations misleading. David Bourgin appears
to no longer be maintaining his FAQ. This document provides details of 
transforms between common colour spaces, preference is given to real world 
colour spaces, rather than proprietary or device specific colour spaces 
which have meaning only when applied in specific circumstances. We recommend 
that this doc is read in conjunction with Charles Poynton's documents, 
GammaFAQ.pdf and ColorFAQ.pdf for a more complete introduction to colour 
systems. They are available from;

elaine.crcg.edu in directory pub/doc/color

For an adobe acrobat PDF format viewer, anonymous ftp from; adobe.com

Date of this edition:- 01 December 1994

Contents:
 Section [1] Some definitions and explanations.
  [1.1] What is the correct way to describe colour that we see?
  [1.2] What is a colour space?
  [1.3] Why is there more than one colour space?
  [1.4] What's the difference between device dependent and device 
        independent?
  [1.5] What is a colour gamut?
  [1.6] What is the CIE System?
  [1.7] What colour space should I use?
 Section [2] Gamma and linearity.
 Section [3]  Grassman's Laws of additive colour mixture.
 Section [4]   Tristimuli, Chromaticity, and colorimetric systems
  [4.1] CIE XYZ (1931)
  [4.2] CIE YUV (1960)
  [4.3] CIE YU'V'
  [4.4] CIE L*u*v*
  [4.5] CIE LAB
  [4.6] CMC colour difference formula.
 Section [5]   Computer Graphics Colour Spaces.
  [5.1] Computer RGB colour space.
  [5.2] CMY(K) (Cyan Magenta Yellow (Black))
  [5.3] HSL and HSV (Hue Saturation and Lightness / Value)
 Section [6]   TV and allied non-linear systems
  [6.1] European Y'U'V' (EBU)
  [6.2] American Y'I'Q'
  [6.3] SMPTE-C RGB
  [6.4] ITU.BT-601 Y'CbCr
  [6.5] ITU.BT-709 HDTV studio production in Y'CbCr
  [6.6] SMPTE-240M Y'PbPr
  [6.7] Kodak PhotoYCC colour space for PhotoCD images.
 Section [7] The references and bedtime reading.
 Section [8] Footnotes.

Section [1] Some definitions and explanations.

This is a chatty bit about colour spaces related to computers.

[1.1] What is the correct way to describe colour that we see?

Colour is described by three attributes. The following terms have been 
defined by the CIE (see 1.6):

brightness    - the human sensation by which an area exhibits more or less 
                light.
Hue	      - the human sensation according to which an area appears 
	        to be similar to one, or to proportions of two, of the 
	        perceived colours red, yellow, green and blue.
Colourfulness - the human sensation according to which an area appears to 
	        exhibit more or less of its hue.

lightness     - the sensation of an area's brightness relative to a reference
                white in the scene.
chroma        - the colourfulness of an area relative to the brightness of a
	        reference white.
saturation    - the colourfulness of an area relative to its brightness.

see Hunt's measuring colour for these in full.

A `colour' is a visual sensation produced by a specific SPD (spectral
power distribution) incident on the retina. The eye senses colour using
three sensitive cells - the cones. This trichromatic mechanism means
that two different SPD's may produce the same visual sensation - 
an effect known as metarmerism.

[1.2] What is a colour space?

A colour space is a method by which we can specify, create and visualise 
colour. As human's, we may define a colour by its attributes of brightness, 
hue and colourfulness. A computer will define a colour in terms of the 
excitations of red, green and blue phosphors on the CRT faceplate. A printing 
press defines colour in terms of the reflectance and absorbance of cyan, 
magenta, yellow and black inks on the paper. A colour is usually
specified by using three coordinates, or attributes, which represent its
position within a specific colour space. These coordinates do not tell
us what the colour looks like, only where the colour is located within
the colour space.

[1.3] Why is there more than one colour space?

Different colour spaces are better for different applications, some 
equipment has limiting factors that dictate the size and type of colour space 
that can be used.  Some colour spaces are perceptually linear, i.e. a 10 
unit change in stimulus will produce the same change in perception wherever 
it is applied. Many colour spaces, particularly in computer graphics are not 
linear in this way. Some colour spaces are intuitive to use, i.e. it is easy 
for the user to navigate within them and creating desired colours is 
relatively easy. Finally, some colour spaces are device dependent while 
others are not - i.e device independent.

[1.4] What's the difference between device dependent and device independent?

A device dependent colour space is a colour space where the colour produced 
depends on the equipment and the set-up used to produce it. For example the 
colour produced using pixel values of rgb = 250,134,67 will alter as 
you change the brightness and contrast on your display. In the same way if 
you swap your monitor the red, green and blue phosphors will have slightly 
different characteristics and the colour produced will change. Thus RGB is a 
colour space that is dependent on the system being used, it is device 
dependent. A device independent colour space is one where the coordinates 
used to specify the colour will produce the same colour wherever they are 
applied. An example of a device independent colour space is the CIE L*a*b* 
colour space (known as CIELab and based on the human visual system). 

Another way of looking a device dependency is to imagine an RGB cube within 
a colour space representing all possible colours (for example a CIE based
colour space). We define a colour by the values on the three axes, however 
the exact colour will depend on the position of the cube within the 
perceptual colour space, move the cube (by changing the set-up) and the 
colour will change.
 
Some device dependent colour spaces have their position within CIE space 
defined, these are known as device calibrated colour spaces and are a kind 
of half way house between dependent and independent colour spaces. For 
example, a graphics file that contains colorimetric information,
i.e. the white point, transfer functions, and phosphor chromaticities,
would enable device dependent RGB data to be modified for whatever
device was being used - i.e. calibrated to specific devices.

[1.5] What is a colour gamut?

A colour gamut is the boundary of the colour space in three dimensions.
Often the gamut will be represented in only two dimensions, for example
on a CIE u'v' chromaticity diagram. (see 4.3)

[1.6] What is the CIE System?

The CIE has defined a system that classifies colour according to the HVS 
(the human visual system). Using this system we can specify any colour in 
terms of its CIE coordinates. 

The CIE system works by weighting the SPD of an object in terms of three
colour matching functions. These functions are the sensitivities of a
standard observer to light of different wavelengths. The weighting is 
performed over the visual spectrum, from around 360nm to 830nm in set 
intervals. However, the illuminant, and lighting and viewing geometry are 
carefully defined, since these all affect the appearance of a particular
colour. This process produces three CIE tri-stimulus values, XYZ, which 
are the building blocks from which many colour measurements are made.

[1.7] What colour space should I use?

That depends on what you want to do, but here is a list of the pros and cons 
of some of the more common, computer related, colour spaces;

RGB (Red Green Blue)

Additive colour system based on trichromatic theory, used by CRT 
displays where proportions of excitation of red, green and blue emitting 
phosphors produce colours when visually fused. Easy to implement, non linear, 
device dependent, unintuitive, common (used in television cameras, computer 
graphics etc).

CMY(K) (Cyan Magenta Yellow (Black))

Subtractive colour. Used in printing and photography. Printers often include 
the fourth component, black ink, to improve the colour gamut (by increasing 
the density range), improving blacks, saving money and speeding drying (less 
ink to dry). Fairly easy to implement, difficult to transfer *properly* from 
RGB (simple transforms are, well, simple), device dependent, non-linear, 
unintuitive.

HSL (Hue Saturation and Lightness)

This represents a wealth of similar colour spaces, alternatives include HSI 
(intensity), HSV (value), HCI (chroma / colourfulness),  HVC, TSD (hue 
saturation and darkness) etc etc. Most of these colour spaces are linear 
transforms from RGB and are thus, device dependent, non-linear but very 
intuitive. In addition the separation of the luminance component has 
advantages in image processing and other applications. (But take care, the 
complete isolation of the separate components will require a space optimised 
for your device. See later notes on CIE colour spaces) 

YIQ, YUV, YCbCr, YCC  (Luminance - Chrominance)

These are the television transmission colour spaces, also known as
transmission primaries. (YIQ and YUV are analogue for NTSC and PAL, and 
YCbCr is digital). They separate luminance from chrominance (lightness 
from colour) and are useful in compression and image processing applications. 
They are device dependent and, unless you are a TV engineer, unintuitive. 
Kodaks PhotoCD system uses a type of YCC colour space, PhotoYCC, which is a 
device calibrated colour space.

CIE 

There are two CIE based colour spaces, CIELuv and CIELab. They are near 
linear (as close as any colour space is expected to sensibly get), device 
independent (unless your in the habit of swapping your eye balls with aliens), 
but not very intuitive to use. 

From CIELuv you can derive CIELhs or CIELhc where h is the hue (an angle), 
s the saturation and c the chroma. This is more intuitive to work with when
specifying colours. CIELuv also has an associated chromaticity diagram, a two 
dimensional chart which makes additive colour mixing very easy to visualise, 
hence CIELuv is widely used in additive colour applications, like television.

CIELab has no associated two dimensional chromaticity diagram and no 
correlate of saturation so only Lhc can be used.

Section [2] Gamma and linearity.

Many image processing operations, and also colour space transforms that 
involve device independent colour spaces, like the CIE system based
ones, must be performed in a linear luminance domain. 
By this we really mean that the relationship between pixel 
values specified in software and the luminance of a specific area on the CRT 
display must be known. In most cases the CRT will have a non-linear response. 
The luminance of a CRT is generally modeled using a power function with an 
exponent, gamma, somewhere between 2.2 (NTSC and SMPTE specifications) and 
2.8 (as given by Hunt and Sproson). Recent measurements performed at the BBC 
in the UK (by Richard Salmon and Alan Roberts) have shown that the actual 
value of gamma is very dependent upon the accurate setting of the CRT's black 
level. For correctly set-up CRT's, gamma is 2.35 +/- 0.1. This relationship 
is given as follows;

Luminance = voltage ^ gamma

Where luminance and voltage are normalised. 
   
In order to display image information as linear luminance  we need to modify 
the voltages sent to the CRT. This process stems from television systems
where the camera and receiver had different transfer functions (which,
unless corrected, would cause problems with tone reproduction). The
modification applied is known as gamma correction and is given below;

New_Voltage = Old_Voltage ^ (1/gamma)

(both voltages are normalised and gamma is the value of the exponent of the 
power function that most closely models the luminance-voltage relationship 
of the display being used.)

For a colour computer system we can replace the voltages by the pixel values 
selected, this of course assumes that your graphics card converts digital 
values to analogue voltages in a linear way. (For precision work you should 
check this). The colour relationships are;

    Red  = a* (Red' ^gamma) +b
    Green= a* (Green' ^gamma) +b
    Blue = a* (Blue' ^gamma) +b

where Red', Green', and Blue' are the normalised input RGB pixel values and 
Red, Green, and Blue are the normalised gamma corrected signals sent to the 
graphics card. The values of the constants a and b compensate for the overall 
system gain and system offset respectively. (essentially gain is contrast and 
offset is intensity.) For basic applications the value of a, b and gamma can 
be assumed to be consistent between colour channels, however for precise 
applications they must be measured for each channel separately.
    	   
A more accurate description of the gamma relationship has recently been given 
in a paper by Berns. See also Charles Poynton's paper and his
document GammaFAQ. The implementation of gamma correction for television
standards is discussed more fully in section 6.

Note: Gamma correction performed in integer maths is prone to large 
quantisation errors. For example, applying a gamma correction of 1/2.2 to 
an image with an original gamma of one (linear luminance) produces a drop 
in the number of grey levels from 245 to 196. Therefore take care not to 
alter the transfer characteristics more than is necessary, if you need to 
gamma correct images try to keep the originals so that you can pass them 
on to others without passing on the degradations that you've produced ;-). 
In addition, any implementation of gamma correction which takes no account 
of the transfer function of the image acquisition system, is an incorrect 
implementation.

Section [3]  Grassman's Laws of additive colour mixture.

Any colour (source C) can be matched by a linear combination of three other 
colours (primaries eg. RGB), provided that none of those three can be matched 
by a combination of the other two. This is fundamental to colorimetry and is 
Grassman's first law of colour mixture. So a colour C can be matched by Rc 
units of red, Gc units of green and Bc units of blue. The units are can be 
measured in any form that quantifies light power.

   C = Rc(R) + Gc(G) + Bc(B)

A mixture of any two colours (sources C1 and C2) can be matched by linearly 
adding together the mixtures of any three other colours that individually 
match the two source colours. This is Grassman's second law of colour 
mixture. It can be extended to any number of source colours.

   C3(C3) = C1(C1) + C2(C2) = [R1+R2](R) + [G1+G2](G) + [B1+B2](B)

Colour matching persists at all luminances. This is Grassman's third law. It 
fails at very low light levels where rod cell vision (scoptopic) takes over 
from cone cell vision (photopic).

   kC3(C3) = kC1(C1) + kC2(C2).

The symbols in square brackets are the names of the colours, and not numerical 
values. The equality sign should not be used to signify an identity, in 
colorimetry it means a colour matching, the colour on one side of the equality 
looks the same as the colour on the other side.

These laws govern all aspects of additive colour work, but they apply only 
signals in the "linear-light" domain. They can be extended into subtractive 
colour work.

Section [4]   Tristimuli, Chromaticity, and colorimetric systems

A colour can be described as a mixture of three other colours or "Tristimuli". 
Typically RGB for CRT based systems (TV, computer) or XYZ (fundamental 
measurements). The amounts of each stimulus define the colour. However, it is 
frequently useful to separate the colour definition into "luminance" and 
"chromaticity". Lower case is always used to signify chromaticity coordinates, 
upper case always signifies tristimulus values (or amounts of the primaries). 
Chromaticity coordinates can be plotted on a two-dimensional diagram that 
defines all the visible colours, luminance is normal to that diagram.

[4.1] CIE XYZ (1931)

The CIE XYZ (1931) system is at the root of all colorimetry. It is defined 
such  that all visible colours can be defined using only positive values, 
and, the Y value is luminance. Consequently, the colours of the XYZ 
primaries themselves are not visible. The chromaticity diagram is highly 
non-linear, in that a vector of unit magnitude representing the difference 
between two chromaticities is not uniformly visible. A colour defined in this 
system is referred to as Yxy. A third coordinate, z, can also be defined but 
is redundant since x+y+z=1 for all colours.

    x = X / (X + Y + Z)
    y = Y / (X + Y + Z)

[4.2] CIE YUV (1960)

This is a linear transformation of Yxy, in an attempt to produce a 
chromaticity diagrams in which a vector of unit magnitude (difference between 
two points representing two colours) is equally visible at all colours. Y is 
unchanged from XYZ or Yxy. Difference non-uniformity is reduced considerably, 
but not enough. A third coordinate, w, can also be defined but is redundant.

    u = 2x / (6y - x + 1.5)
    v = 3y / (6y - x + 1.5)


[4.3] CIE YU'V'

This is another linear transformation of Yxy. Y remains unchanged. 
Difference non-uniformity is further reduced, but still not enough. Again, 
a third coordinate, w', can be defined, but is redundant.

    u' = u    = 2x / (6y - x + 1.5)
    v' = 1.5v = 4.5y / (6y - x + 1.5)


[4.4] CIE L*u*v*

This is based on CIE Yu'v' (1976) and is a further attempt to linearise 
the perceptibility of unit vector colour differences. It is a non-linear 
colour space, but the conversions are reversible. Colouring information is 
centered on the colour of the white point of the system, subscript n, (D65 
in most TV systems). The non-linear relationship for Y* is intended to mimic 
the logarithmic response of the eye.

    L* = 116*((Y/Yn)^(1/3))     for Y/Yn>0.008856
    L* = 903.3*Y/Yn             for Y/Yn<=0.008856
    u* = 13*(L*)*(u'-u'n)
    v* = 13*(L*)*(v'-v'n)

L* scales from 0 to 100 for relative luminance (Y/Yn) scaling 0 to 1.

There are three other, more meaningful polar parameters which more closely 
match  the human visual experience than do the Cartesian parameters.

    Chroma                     C* = (u*^2 + v*^2) ^ 0.5
    Hue                       huv = arctan(v* / u*)
    Psychometric Saturation   suv = C* / L*

Hue is an angle in four quadrants.

[4.5] CIE LAB

This is based directly on CIE XYZ (1931) and is another attempt to linearise 
the perceptibility of unit vector colour differences. Again, it is non-linear, 
and the conversions are still reversible. Colouring information is referred 
to the colour of the white point of the system, subscript n. The non-linear 
relationships for L* a* and b* are the same as for CIELUV and are intended 
to mimic the logarithmic  response of the eye.

    L*=116*((Y/Yn)^(1/3))       for Y/Yn>0.008856
    L*=903.3*Y/Yn               for Y/Yn<=0.008856
    a*=500*(f(X/Xn)-f(Y/Yn))
    b*=200*(f(Y/Yn)-f(Z/Zn))
    where f(t)=t^(1/3)          for t>0.008856
          f(t)=7.787*t+16/116   for t<=0.008856

Again, L* scales from 0 to 100.

Again, there are polar parameters that more closely match the visual 
experience of colours.

    Chroma C*  = (a*^2 + b*^2)^ 0.5
    Hue    hab = arctan(b*/a)

Hue is an angle in four quadrants, and there is no saturation term in this 
system.


[4.6] CMC colour difference formula.

The CMC colour difference formula is better than CIE Lab for evaluating
small colour differences.

Colour difference = [(D_L / l * Sl)^2 + (D_C / c * Sc)^2 + (D_H / Sh)^"2]^0.5

where; Sl = 0.040975L / (1+0.01765L) unless L < 16 when Sl = 0.511
       Sc = 0.0638C / (1+0.0131C) + 0.638
       Sh = (f*T + 1 - f)Sc

where; f = (C^4 / (C^4 + 1900))^0.5
       T = 0.36 + | 0.4 cos(h + 35| unless h is between 164 and 345
         degrees when
            T = 0.56 + | 0.2 cos(h + 168) |

D_L is delta E star, D_C is delta C star, D_H delta H star, L is L star
and h hue angle.a:

for predicting colour differences, the weights l and c are set to unity,
and the colour difference is referred to as the CMC(1:1) difference.

Section [5]   Computer Graphics Colour Spaces.

Traditionally colour spaces for computer graphics have been designed for
specific devices - RGB for displays or CMY for printers. As such they
are device dependent (and meaningless if applied to different devices or
the same device in different conditions). Most computer graphics colour
spaces are perceptually non-linear, i.e. colour differences vary
throughout the space, this makes them inefficient for coding colour
information. Another problem is that they can be unintuitive, by this we
mean that a novice may find it difficult to specify a desired colour -
for example brown in RGB.
A caveat. The transforms in this section are generally simplifications of the
real world. We strongly recommend reading the references at the end of
this FAQ as well as Charles Poynton's ColorFAQ.pdf

We assume all values are normalised to 1.

[5.1] Computer RGB colour space.

This is the colour space produced on a CRT (or similar) display when
pixel values are applied to a graphics card. To convert RGB pixel values
to CIE XYZ tri-stimulus values is a two stage process.

1) Calculate the displayed luminance for each pixel value, this is done
by applying the gamma correction factor to the signal. Ideally this
should be calculated fro each channel independently.

2) Perform a matrix transform from the linear RGB to CIE XYZ values.
This transform takes the following form.

       |X|       |Xr  Xg  Xb|     |r| 
       |Y|   =   |Yr  Yg  Yb|  *  |g|
       |Z|	 |Zr  Zg  Zb|     |b|

where Xr,Yr,Zr, are the CIE XYZ tristimulus values of the red phosophor
at full emission measured from the CRT faceplate.
Xg,Yg,Zg, are the CIE XYZ tristimulus values of the green phosophor
at full emission measured from the CRT faceplate.
and Xb,Yb,Zb, are the CIE XYZ tristimulus values of the blue phosophor
at full emission measured from the CRT faceplate.

To convert from XYZ to RGB use the inverse matrix.

       |r|       |Xr  Xg  Xb|(-1)     |X| 
       |g|   =   |Yr  Yg  Yb|     *   |Y|
       |b|	 |Zr  Zg  Zb|         |Z|

Television systems are RGB systems with specific characteristics for the
gamma relationship and the phosphor chromaticities.

See Berns for a detailed implementation and a description of some of the
pitfalls and Sproson for RGB to CIE conversions in television systems.

[5.2] CMY(K) (Cyan Magenta Yellow (Black))

CMY(K) is device dependent, unintuitive and perceptually non-linear; 

To convert from RGB to CMY for printing, the simplest method is to
convert from RGB to CIE XYZ and then from CIE XYZ to CMY(K). This is not
a trivial process and ideally takes into consideration other factors -
such as viewing conditions, lighting, printing inks etc. 

Simple (and often incorrect) transforms for RGB to CMY are given below, 
indeed some Adobe Postscript documents state them;

RGB to CMY	                   CMY to RGB			
Cyan    = 1-Red                    Red   = 1-Cyan
Magenta = 1-Green                  Green = 1-Magenta 
Yellow  = 1-Blue                   Blue  = 1-Yellow
 
CMY to CMYK				
Black   = minimum (Cyan,Magenta,Yellow)	
Cyan    = (Cyan-Black)/(1-Black)		
Magenta = (Magenta-Black)/(1-Black)	
Yellow  = (Yellow-Black)/(1-Black)	

CMYK to CMY
Cyan    = minimum(1,Cyan*(1-Black)+Black)
Magenta = minimum(1,Magenta*(1-Black)+Black)
Yellow  = minimum(1,Yellow*(1-Black)+Black)

These may be sufficient if you simply want to specify CMY colours on an
RGB display's user interface, but for printing, where any kind of match
between display and print is desired, they are totally inaccurate. If
you want to try this kind of thing you're getting into the domain of
colour management systems and should be reading Charle's Poynton's
ColorFAQ.

[5.3] HSL and HSV (Hue Saturation and Lightness / Value)

Other similar colour spaces include HSI (intensity), HCI (chroma / 
colourfulness), HVC, TSD (hue saturation and darkness) etc etc

As linear transforms from RGB they are device dependent, non-linear but very 
intuitive. The separation of the luminance component has advantages in image 
processing - but the complete isolation of the separate components will 
require  a (CIE based) device independent colour space.

RGB to HSV

max = maximum (Red, Green, Blue)
min = minimum (Red, Green, Blue)
V   = max

S   = (max - min) / max   if max <> 0
S   = 0                   if max  = 0

H is undefined if S = 0

H = (Green - Blue) / delta    if Red is max
H = 2 + (Blue - Red) / delta  if Green is max
H = 4 + (Red - Green) / delta if Blue is max

where delta = max - min

to put H into degrees, H = H + 60 and H = H + 360 if H < 0

HSV to RGB

Red = Green = Blue = V  when S = 0 and H is undefined, 
otherwise;
  i = floor(H/60) and f = H - i
  p = V(1-S) and q = V(1-(S*f)) and t = V(1-S(1-f))
 then 
  Red = v, Green = t, Blue = p when i = 0
  Red = q, Green = v, Blue = p when i = 1
  Red = p, Green = v, Blue = t when i = 2
  Red = p, Green = q, Blue = v when i = 3
  Red = t, Green = p, Blue = v when i = 4
  Red = v, Green = p, Blue = q when i = 5

RGB to HLS
    
max = maximum (Red, Green, Blue)
min = minimum (Red, Green, Blue)
L = (max + min)/2

S = 0 and H is undefined if max = min
S = (max-min)/(max+min)  if L <= 0.5
S = (max-min/(2-max-min) if L >  0.5

H = (Green - Blue) / delta    if Red is max
H = 2 + (Blue - Red) / delta  if Green is max
H = 4 + (Red - Green) / delta if Blue is max

where delta = max - min

to put H into degrees, H = H + 60 and H = H + 360 if H < 0

Note, Hue, H, is calculated in the same way as for HSV.

HSL to RGB

Red = Green = Blue = L if S = 0 and H is undefined,
otherwise;
 Red   = m1 + (m2-m1) * a / 60      if a < 60
       = m2                         if 60 < a < 180
       = m1 + (m2-m1)*(240 - a)/60  if 180 < a < 240
       = m1                         if 240 < a < 360
         where a = H + 120 normalised to lie between 0 and 360 degrees
 Green = m1 + (m2-m1) * a / 60      if a < 60
       = m2                         if 60 < a < 180
       = m1 + (m2-m1)*(240 - a)/60  if 180 < a < 240
       = m1                         if 240 < a < 360
         where a = H normalised to lie between 0 and 360 degrees
 Blue  = m1 + (m2-m1) * a / 60      if a < 60
       = m2                         if 60 < a < 180
       = m1 + (m2-m1)*(240 - a)/60  if 180 < a < 240
       = m1                         if 240 < a < 360
         where a = H - 120 normalised to lie between 0 and 360 degrees
In all cases, 
  m2 = L(1+S)      if L <= 0.5
  m2 = L + S + LS  if L >  0.5
  m1 = 2L - m2

RGB to HSI

     I = (Red + Green + Blue) /3
 cos H = (0.5(Red - Green)+(Red - Blue)) / ((Red - Green)^2 + (Red -
          Blue)(Green - Blue))^0.5
     S = 1 - (3/(Red + Green + Blue)) * min
         where min = minimum (Red, Green, Blue)

HSI to RGB

For 0 < H < 120
    Blue   = (1 - S)/3
    Red    = (1 + ((S cos H)/(cos(60 - H)))/3
    Green  = 1 - (Red + Blue)
For 120 < H < 240
    H = H - 120 and then
    Red    = (1 - S)/3
    Green  = (1 + ((S cos H)/(cos(60 - H)))/3
    Blue   = 1 - (Red + Green)
For 240 < H < 360
    H = H -240 and then
    Green  = (1 - S)/3
    Blue   = (1 + ((S cos H)/(cos(60 - H)))/3
    Red    = 1 - (Green + Blue)

Then for each case, Red = 3*Red*I, Green = 3*Green*I and Blue = 3*Blue*I

see Foley and VanDam for full details.

Section [6]   TV and allied non-linear systems

All TV standards use non-linear signals, pre-corrected for the non-linear 
transfer characteristic of the display CRT. It is here that the most 
confusion exists, and so this is a VERY important section to understand.

A typical CRT has a non-linear voltage-to-light transfer function with a 
power law usually denoted by gamma. The value of gamma is theoretically 
2.5, but is specified as 2.2 in NTSC systems, 2.8 in PAL systems, and is 
actually nearer to 2.35 for real CRTs. Any signal destined for display on 
a CRT must be distorted by an inverse law. In practice, that is impossible 
because a pure power law has infinite slope (gain) at zero (black). TV 
systems limit the gain near black to a value between 4 and 5 by offsetting 
the power law. This has the side advantage of increasing saturation in a way 
that compensates for the display having a dark surround. For example the 
ITU-BT.709 specification is:-
    
    Volts = (1 + a) * Light ^ (law) - a        for Light > b
    Volts = slope * Light                      for Light <= b
              where a=0.099, law=0.45, b=0.018.

and the gain at zero is 4.5. This law is similar to the formula used for L* 
(see above).

So for accurate colour calculations, this law (or whichever law was actually 
applied) must be undone to return to linear signals before doing conversions. 
The law should be reapplied to the results to get the drive signals for the 
actual display.

A signal that has been gamma-corrected is shown primed (Y', R', G', B' etc). 
In general, undoing the gamma law will return to linear signals, but that 
is not always true, especially with the Y' signal, which is not directly 
related to the CIE Y value. It is a shame that the TV industry used Y' for 
the luminance channel, because it created a great deal of confusion, most 
of which still exists. But careful reading of the following section shows 
the way to performing totally accurate colour calculations using any colour 
system.

[6.1] European Y'U'V' (EBU)

European TV (PAL and SECAM coded) uses Y'U'V' components. Y' is similar to 
perceived luminance, U' and V' carry the colour information and some 
luminance information and are bipolar (they go negative as well as positive). 
The symbols U and V here are not related to the U and V of CIE YUV (1960).

This coding is also used in some 525 line systems with PAL subcarriers, 
particularly in parts of the Americas. The specification here is that of the 
European Broadcasting Union (EBU). Y' has a bandwidth of 5 MHz in Europe, 
5.5 MHz in UK. The U' and V' signals usually have up to 2.5 MHz bandwidth 
in a component studio system, but can be as little as 600 kHz or less in a 
VHS recorder. U' and V' always have the same bandwidth as each other. The 
CRT gamma law is assumed to be 2.8, but camera correction laws are the same 
as in all other systems (approximately 0.45). The system white point is D65, 
the chromaticity coordinates are:
    R:      xr=0.64      yr=0.33
    G:      xg=0.29      yg=0.60
    B:      xb=0.15      yb=0.06
    White:  xn=0.312713  yn=0.329016

The conversion equations for linear signals are:-
    X = 0.431*R + 0.342*G + 0.178*B
    Y = 0.222*R + 0.707*G + 0.071*B
    Z = 0.020*R + 0.130*G + 0.939*B

    R = 3.063*X - 1.393*Y - 0.476*Z
    G =-0.969*X + 1.876*Y + 0.042*Z
    B = 0.068*X - 0.229*Y + 1.069*Z

the coding equations for non-linear signals are:
    Y'= 0.299*R' + 0.587*G' + 0.114*B'
    U'= 0.493*(B' - Y') =-0.147*R' - 0.289*G' + 0.436*B'
    V'= 0.877*(R' - Y') = 0.615*R' - 0.515*G' - 0.100*B'

    R'= Y' + 0.000*U' + 1.140*V'
    G'= Y' - 0.396*U' - 0.581*V'
    B'= Y' + 2.029*U' + 0.000*V'

The conversion equations between linear 709 RGB signals 
(see later) and EBU RGB signals are:
    Re= 0.9578*R7 + 0.0422*G7 + 0.0000*B7
    Ge= 0.0000*R7 + 1.0000*G7 + 0.0000*B7
    Be= 0.0000*R7 + 0.0118*G7 + 0.9882*B7

    R7= 1.0440*Re - 0.0440*Ge + 0.0000*Be
    G7= 0.0000*Re + 1.0000*Ge + 0.0000*Be
    B7= 0.0000*Re - 0.0119*Ge + 1.0119*Be


[6.2] American Y'I'Q'

American TV (NTSC coded) uses Y'I'Q' components. Again Y' is similar to 
perceived luminance, I' and Q' carry colour information and some luminance 
information and are derived by rotating the U'V' vector formed by colour 
coding as described in section 3.1 by 33 degrees. The Y' signal usually has 
4.2 MHz bandwidth in a 525 line system. Originally the I' and Q' signals 
were to have different bandwidths (0.5 and 1.5 MHz) but they now commonly 
have the same bandwidth (1 MHz). The coding is also used in some 625 line 
countries with NTSC subcarriers, again mostly in the Americas. The CRT 
gamma law is assumed to 2.2. The system white pointis Illuminant C, the 
chromaticity coordinates are:-
    R:      xr=0.67      yr=0.33
    G:      xg=0.21      yg=0.71
    B:      xb=0.14      yb=0.08
    White: xn=0.310063 yn=0.316158 zn=0.373779

The conversion equations for linear signals are:-
    X = 0.607*R + 0.174*G + 0.200*B
    Y = 0.299*R + 0.587*G + 0.114*B
    Z = 0.000*R + 0.066*G + 1.116*B

    R = 1.910*X - 0.532*Y - 0.288*Z
    G =-0.985*X + 1.999*Y - 0.028*Z
    B = 0.058*X - 0.118*Y + 0.898*Z

The coding equations for non-linear signals are:-
    Y'= 0.299*R' + 0.587*G' + 0.114*B'
    I'=-0.27*(B' - Y') + 0.74*(R' - Y') = 0.596*R' - 0.274*G' + 0.322*B'
    Q'= 0.41*(B' - Y') + 0.48*(R' - Y') = 0.212*R' - 0.523*G' - 0.311*B'

    R'= Y' + 0.956*I' + 0.621*Q'
    G'= Y' - 0.272*I' - 0.647*Q'
    B'= Y' - 1.105*I' + 1.702*Q'

It is possible to define a transformation matrix between EBU Y'U'V' and NTSC 
Y'I'Q'. However, this only makes sense if the primaries are the same for the 
two systems, and clearly they are defined differently. However, over the 
years, the American NTSC system has changed its primaries several times 
until they are now very similar to those of the EBU systems. The non-linear 
connecting equations are:
     I'=-0.27/0.493*U' + 0.74/0.877*V' =-0.547667343*U' +  0.843785633*V'
     Q'= 0.41/0.493*U' + 0.48/0.877*V' = 0.831643002*U' +  0.547320410*V'

and:
     U'=-0.546512701*I' +  0.842540416*Q'
     V'= 0.830415704*I' +  0.546859122*Q'

To all intents and purposes these equations are identical and so one practical 
set of equations can be used in either direction:
     I'=-0.547*U' + 0.843*V'
     Q'= 0.831*U' + 0.547*V'

     U'=-0.547*I' + 0.843*Q'
     V'= 0.831*I' + 0.547*Q'

The conversion equations relating NTSC RGB signals to EBU and 709 are:
    Rntsc= 0.6984*Rebu + 0.2388*Gebu + 0.0319*Bebu
    Gntsc= 0.0193*Rebu + 1.0727*Gebu - 0.0596*Bebu
    Bntsc= 0.0169*Rebu + 0.0525*Gebu + 0.8450*Bebu

    Rebu= 1.4425*Rntsc - 0.3173*Gntsc - 0.0769*Bntsc
    Gebu=-0.0275*Rntsc + 0.9350*Gntsc + 0.0670*Bntsc
    Bebu=-0.0272*Rntsc - 0.0518*Gntsc + 1.1081*Bntsc

and:
    Rntsc= 0.6698*R709 + 0.2678*G709 + 0.0323*B709
    Gntsc= 0.0185*R709 + 1.0742*G709 - 0.0603*B709
    Bntsc= 0.0162*R709 + 0.0432*G709 + 0.8551*B709

    R709= 1.5073*Rntsc - 0.3725*Gntsc - 0.0832*Bntsc
    G709=-0.0275*Rntsc + 0.9350*Gntsc + 0.0670*Bntsc
    B709=-0.0272*Rntsc - 0.0401*Gntsc + 1.1677*Bntsc

[6.3] SMPTE-C RGB

SMPTE-C is the current colour standard for broadcasting in America, the old 
NTSC standard for primaries is no longer in wide use because the primaries 
of the system have gradually shifted towards those of the EBU 
(see section 6.2). In all other respects, SMPTE-C is the same as NTSC. 
The CRT gamma law is assumed to be 2.2. The white point is now D65, and 
the chromaticities are:

    R:     xr=0.630     yr=0.340
    G:     xg=0.310     yg=0.595
    B:     xb=0.155     yb=0.070
    White: xn=0.312713  yn=0.329016

The conversion equations for linear signals are:
    X = 0.3935*R + 0.3653*G + 0.1916*B
    Y = 0.2124*R + 0.7011*G + 0.0866*B
    Z = 0.0187*R + 0.1119*G + 0.9582*B

    R = 3.5058*X - 1.7397*Y - 0.5440*Z
    G =-1.0690*X + 1.9778*Y + 0.0352*Z
    B = 0.0563*X - 0.1970*Y + 1.0501*Z

The coding equations for non-linear signals are the same as for NTSC:
    Y'= 0.299*R' + 0.587*G' + 0.114*B'
    I'=-0.27*(B' - Y') + 0.74*(R' - Y') = 0.596*R' - 0.274*G' + 0.322*B'
    Q'= 0.41*(B' - Y') + 0.48*(R' - Y') = 0.212*R' - 0.523*G' - 0.311*B'

    R'= Y' + 0.956*I' + 0.621*Q'
    G'= Y' - 0.272*I' - 0.647*Q'
    B'= Y' - 1.105*I' + 1.702*Q'

and the same conversion equations work between EBU and SMPTE-C components:
    I'=-0.547*U' + 0.843*V'
    Q'= 0.831*U' + 0.547*V'

    U'=-0.547*I' + 0.843*Q'
    V'= 0.831*I' + 0.547*Q'

The conversion equations relating SMPTE-C RGB signals to EBU and 709 signals 
are:
    Rsmptec= 1.1123*Rebu - 0.1024*Gebu - 0.0099*Bebu
    Gsmptec=-0.0205*Rebu + 1.0370*Gebu - 0.0165*Bebu
    Bsmptec= 0.0017*Rebu + 0.0161*Gebu + 0.9822*Bebu

    Rebu= 0.9007*Rsmptec + 0.0888*Gsmptec + 0.0105*Bsmptec
    Gebu= 0.0178*Rsmptec + 0.9658*Gsmptec + 0.0164*Bsmptec
    Bebu=-0.0019*Rsmptec - 0.0160*Gsmptec + 1.0178*Bsmptec

and:
    Rsmptec= 1.0654*R709 - 0.0554*G709 - 0.0010*B709
    Gsmptec=-0.0196*R709 + 1.0364*G709 - 0.0167*B709
    Bsmptec= 0.0016*R709 + 0.0044*G709 + 0.9940*B709

    R709= 0.9395*Rsmptec + 0.0502*Gsmptec + 0.0103*Bsmptec
    G709= 0.0178*Rsmptec + 0.9658*Gsmptec + 0.0164*Bsmptec
    B709=-0.0016*Rsmptec - 0.0044*Gsmptec + 1.0060*Bsmptec

[6.4] ITU.BT-601 Y'CbCr

This is the international standard for digital coding of TV pictures at 525 
and 625 line rates. It is independent of the scanning standard and the 
system primaries, therefore there are no chromaticity coordinates, no CIE 
XYZ matrices, and no assumptions about white point or CRT gamma. It deals 
only with the digital representation of R'G'B' signals in Y'CbCr form. 
The non-linear coding matrices are:

    Y'= 0.299*R' + 0.587*G' + 0.114*B'
    Cb=-0.169*R' - 0.331*G' + 0.500*B'
    Cr= 0.500*R' - 0.419*G' - 0.081*B'

    R'= Y' + 0.000*U' + 1.403*V'
    G'= Y' - 0.344*U' - 0.714*V'
    B'= Y' + 1.773*U' + 0.000*V'


[6.5] ITU.BT-709 HDTV studio production in Y'CbCr

This is a recent standard, defined only as an interim standard for HDTV 
studio production. It was defined by the CCIR (now the ITU) in 1988, but is 
not yet recommended for use in broadcasting. The primaries are the R and B 
from the EBU, and a G which is midway between SMPTE-C and EBU. The CRT gamma 
law is assumed to be 2.2. White is D65. The chromaticities are:
    R:      xr=0.64       yr=0.33
    G:      xg=0.30       yg=0.60
    B:      xb=0.15       yb=0.06
    White:  xn=0.312713   yn=0.329016

The conversion equations for linear signals are:
    X = 0.412*R + 0.358*G + 0.180*B
    Y = 0.213*R + 0.715*G + 0.072*B
    Z = 0.019*R + 0.119*G + 0.950*B

    R = 3.241*X - 1.537*Y - 0.499*Z
    G =-0.969*X + 1.876*Y + 0.042*Z
    B = 0.056*X - 0.204*Y + 1.057*Z

The coding equations for non-linear signals are:
    Y'= 0.2215*R' + 0.7154*G' + 0.0721*B'
    Cb=-0.1145*R' - 0.3855*G' + 0.5000*B'
    Cr= 0.5016*R' - 0.4556*G' - 0.0459*B'

    R'= Y' + 0.0000*Cb + 1.5701*Cr
    G'= Y' - 0.1870*Cb - 0.4664*Cr
    B'= Y' - 1.8556*Cb + 0.0000*Cr

The conversion equations between linear 709 RGB signals and EBU RGB signals 
are:
    Re= 0.9578*R7 + 0.0422*G7 + 0.0000*B7
    Ge= 0.0000*R7 + 1.0000*G7 + 0.0000*B7
    Be= 0.0000*R7 + 0.0118*G7 + 0.9882*B7

    R7= 1.0440*Re - 0.0440*Ge + 0.0000*Be
    G7= 0.0000*Re + 1.0000*G7 + 0.0000*Be
    B7= 0.0000*Re - 0.0119*Ge + 1.0119*Be


[6.6] SMPTE-240M Y'PbPr

This one of the developments of NTSC component coding, in which the B primary
and white point were changed. The CRT gamma law is assumed to be 2.2. The 
white point is D65, chromaticity coordinates are:
    R:      xr=0.67     yr=0.33
    G:      xg=0.21     yg=0.71
    B:      xb=0.15     yb=0.06
    White:  xn=0.312713 yn=0.329016

The conversion equations for linear signals are:
    X = 0.567*R + 0.190*G + 0.193*B
    Y = 0.279*R + 0.643*G + 0.077*B
    Z = 0.000*R + 0.073*G + 1.016*B

    R = 2.042*X - 0.565*Y - 0.345*Z
    G =-0.894*X + 1.815*Y + 0.032*Z
    B = 0.064*X - 0.129*Y + 0.912*Z

The coding equations for non-linear signals are:
   Y'= 0.2122*R' + 0.7013*G' + 0.0865*B'
   Pb=-0.1162*R' - 0.3838*G' + 0.5000*B'
   Pr= 0.5000*R' - 0.4451*G' - 0.0549*B'

   R'= Y + 0.0000*Pb + 1.5756*Pr
   G'= Y - 0.2253*Pb + 0.5000*Pr
   B'= Y + 1.8270*Pb + 0.0000*Pr

The conversion equations relating SMPTE 240 RGB to EBU and 709 primaries are:
    R240= 0.7466*Re + 0.2534*Ge + 0.0000*Be
    G240= 0.0187*Re + 0..9813*Ge + 0.0000*Be    
    B240= 0.0185*Re + 0.0575*Ge + 0.9240*Be

    Re= 1.3481*R240 - 0.3481*G240 + 0.0000*B240
    Ge=-0.0257*R240 + 1.0257*G240 + 0.0000*B240
    Be=-0.0254*R240 - 0.0568*G240 + 1.0822*B240

and:
    R240= 0.7151*R709 + 0.2849*G709 + 0.0000*B709
    G240= 0.0179*R709 + 0.9821*G709 + 0.0000*B709
    B240= 0.0177*R709 + 0.0472*G709 + 0.9350*B709

    R709= 1.4086*R240 - 0.4086*G240 + 0.0000*B240
    G709=-0.0257*R240 + 1.0457*G240 + 0.0000*B240
    B709=-0.0254*R240 - 0.0440*G240 + 1.0695*B240


[6.7] Kodak PhotoYCC colour space for PhotoCD images.

The Kodak PhotoYCC colour space was designed for encoding images with the 
PhotoCD system. It is based on both CCIR Recommendations 709 and 601-1, 
having a colour gamut defined by the CCIR 709 primaries and a luminance 
- chrominance representation of colour like CCIR 601-1's YCbCr. 
 
To encode data, a transfer function (gamma correction) is first applied;

For R,G,B > 0.018
R' = 1.099*R ^0.45 - 0.099
G' = 1.099*G ^0.45 - 0.099
B' = 1.099*B ^0.45 - 0.099

For R,G,B, <= 0.018
R' = 4.5*R
G' = 4.5*G
B' = 4.5*B

Secondly the R'G'B' data is transformed into PhotoYCC data, 

        Luma    = 0.299R'  + 0.587G' + 0.114B'
        Chroma1 = -0.299R' - 0.587G' + 0.886B'
        Chroma2 = 0.701R'  - 0.587G' - 0.114B'

Finally the floating point values are stored as 8 bit integers;

        Luma_8bit       = (255 / 1.402)Luma
        Chroma1_8bit    = 111.40*Chroma1 + 156
        Chroma2_8bit    = 135.64*Chroma2 + 137

The unbalanced scale difference between Chroma1 and Chroma2 is designed, 
according to Kodak, to follow the typical distribution of colours in 
real scenes.

Kodak YCC can store more information than current display devices can cope 
with (it allows negative RGB values), the transforms from YCC to RGB are 
therefore not simply the inverse of RGB to YCC, they depend on the target 
display system.

Converting PhotoYCC data to RGB 24bit data for display by computers on CRT's 
is achieved as follows;

Firstly normal Luma and Chroma data are recovered;

        Luma            = 1.3584 * Luma_8bit
        Chroma1         = 2.2179 * (Chroma1_8bit - 156)
        Chroma2         = 1.8215 * (Chroma2_8bit - 137)

For display primaries that are, or are very close to, CCIR 
Recommendation 709 primaries in their chromaticities, then 

        R'      = L + Chroma2
        G'      = L - 0.194Chroma1 - 0.509Chroma2
        B'      = L + Chroma1
Two things to watch are;

a) this results in RGB values from 0 to 346 (instead of the more usual 0 to 
255) a look-up-table is usually used to convert these through a non-linear 
function to 8 bit data. For example;

    Y  = (255/1.402)*Y'                                 
    C1 = 111.40*C1'+156                                 
    C2 = 135.64*C2'+137 

b) if the display phosphors differ from CCIR 709 primaries then further 
conversion will be necessary, possibly through an intermediate device 
independent colour space such as CIE XYZ.

Converting stored PhotoYCC data to RGB signal voltages for display on  
television's is achieved as follows;

Firstly  normal Luma and Chroma data are recovered;

        Luma            = 1.3584 * Luma_8bit
        Chroma1         = 2.2179 * (Chroma1_8bit - 156)
        Chroma2         = 1.8215 * (Chroma2_8bit - 137)

the RGB display voltages are then as follows;

        R'      = (L + Chroma2) / 353.2
        G'      = (L - 0.194Chroma1 - 0.509Chroma2) / 353.2
        B'      = (L + Chroma1) / 353.2

Note, the gamma corrected RGB values (R'G'B') are fine so long as you haven't
done anything silly - like calibrate you CRT for linear luminance :)

Section [7] The references and bedtime reading.

 "An inexpensive scheme for calibration of a colour monitor in terms of CIE
standard coordinates" W.B. Cowan, Computer Graphics, Vol. 17 No. 3, 1983

"Calibration of a computer controlled color monitor", Brainard, D.H, Color 
Research & Application, 14, 1, pp 23-34 (1989).

"Colour Science in Television and Display Systems" Sproson, W, N, 
Adam Hilger Ltd, 1983. ISBN 0-85274-413-7

"CIE Colorimetry" Official recommendations of the International Commission
 on Illumination, Publication 15.2 1986

"CRT Colorimetry:Part 1 Theory and Practice, Part 2 Metrology", Berns,
R.S., Motta, R.J. and Gorzynski, M.E., Color Research and Application, 18, 
(1993).

"Effective Color Displays. Theory and Practice", Travis, D, Academic Press,
1991. ISBN 0-12-697690-2
  
Field, G.G., Color and Its Reproduction, Graphics Arts Technical
Foundation, 1988, pp. 320-9  (Read this about CMY/CMYK)

"Gamma and its disguises: The nonlinear mappings of intensity in
perception, CRT's, Film and Video" C. A. Poynton, SMPTE Journal, December 
1993

"Measuring Colour" second edition, R. W. G. Hunt, Ellis Horwood 1991,

"On the Gun Independence and Phosphor Consistency of Color Video Monitors"
W.B. Cowan N. Rowell, Color Research and Application, V.11 Supplement 1986

"Precision requirements for digital color reproduction" M Stokes
MD Fairchild RS Berns, ACM Transactions on graphics, v11 n4 1992

"The colorimetry of self luminous displays - a bibliography" CIE
Publication n.87, Central Bureau of the CIE, Vienna 1990

"The Reproduction of Colour in PhotoGraphy, Printing and Television", R. W.
G. Hunt, Fountain Press, Tolworth, England, 1987

"Fully Utilizing Photo CD Images, Article No. 4, 
PhotoYCC Colour Encoding and Compression Schemes" Eastman Kodak Company, 
Rochester NY (USA) (1993).

Section [8] Footnotes.

Thanks to everyone who contributed, especially to Charles Poynton. Feel
free to send comments, suggestions, additions, flames, etc to
ajoec1@wmin.ac.uk.
-----------------------------------------------------------------------
