The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

PDL::Graphics::ColorSpace

VERSION

0.1.0

DESCRIPTION

Does image color space conversions such as RGB to XYZ and Lab to LCH. Derived from Graphics::ColorObject (Izvorski & Reibenschuh, 2005) but since it's implemented in C and PDL, it runs *much* faster.

Often the conversion will return out-of-gamut values. Retaining out-of-gamut values allows chained conversions to be lossless and reverse conversions to produce the original values. You can clip the values to be within-gamut if necessary. Please check the specific color space for the gamut range.

COLOR SPACES

RGB

An RGB color space is any additive color space based on the RGB color model. A particular RGB color space is defined by the three chromaticities of the red, green, and blue additive primaries, and can produce any chromaticity that is the triangle defined by those primary colors. The complete specification of an RGB color space also requires a white point chromaticity and a gamma correction curve.

For more info on the RGB color space, see http://en.wikipedia.org/wiki/RGB_color_space.

This module expects and produces RGB values normalized to be in the range of [0,1]. If you have / need integer value between [0,255], divide or multiply the values by 255.

CMYK

CMYK refers to the four inks used in some color printing: cyan, magenta, yellow, and key (black). The CMYK model works by partially or entirely masking colors on a lighter, usually white, background. The ink reduces the light that would otherwise be reflected. Such a model is called subtractive because inks "subtract" brightness from white.

In additive color models such as RGB, white is the "additive" combination of all primary colored lights, while black is the absence of light. In the CMYK model, it is the opposite: white is the natural color of the paper or other background, while black results from a full combination of colored inks. To save money on ink, and to produce deeper black tones, unsaturated and dark colors are produced by using black ink instead of the combination of cyan, magenta and yellow.

For more info, see http://en.wikipedia.org/wiki/CMYK_color_model.

HSL

Hue, Saturation and Luminance (or brightness).

The HSL color space defines colors more naturally: Hue specifies the base color, the other two values then let you specify the saturation of that color and how bright the color should be.

Hue is specified here as degrees ranging from 0 to 360. There are 6 base colors:

    0    red
    60    yellow
    120    green
    180    cyan
    240    blue
    300    magenta
    360    red

Saturation specifies the distance from the middle of the color wheel. So a saturation value of 0 (0%) means "center of the wheel", i.e. a grey value, whereas a saturation value of 1 (100%) means "at the border of the wheel", where the color is fully saturated.

Luminance describes how "bright" the color is. 0 (0%) means 0 brightness and the color is black. 1 (100%) means maximum brightness and the color is white.

For more info, see http://www.chaospro.de/documentation/html/paletteeditor/colorspace_hsl.htm.

XYZ and xyY

The CIE XYZ color space was derived the CIE RGB color space. XYZ are three hypothetical primaries. Y means brightness, Z is quasi-equal to blue stimulation, and X is a mix which looks like red sensitivy curve of cones. All visible colors can be represented by using only positive values of X, Y, and Z. The main advantage of the CIE XYZ space (and any color space based on it) is that this space is completely device-independent.

For more info, see http://en.wikipedia.org/wiki/CIE_1931_color_space.

Lab

A Lab color space is a color-opponent space with dimension L for lightness and a and b for the color-opponent dimensions, based on nonlinearly compressed CIE XYZ color space coordinates. It's derived from the "master" space CIE 1931 XYZ color space but is more perceptually uniform than XYZ. The Lab space is relative to the white point of the XYZ data they were converted from. Lab values do not define absolute colors unless the white point is also specified.

For more info, see http://en.wikipedia.org/wiki/Lab_color_space.

LCH

This is possibly a little easier to comprehend than the Lab colour space, with which it shares several features. It is more correctly known as L*C*H*. Essentially it is in the form of a sphere. There are three axes; L* and C* and H°.

The L* axis represents Lightness. This is vertical; from 0, which has no lightness (i.e. absolute black), at the bottom; through 50 in the middle, to 100 which is maximum lightness (i.e. absolute white) at the top.

The C* axis represents Chroma or "saturation". This ranges from 0 at the centre of the circle, which is completely unsaturated (i.e. a neutral grey, black or white) to 100 or more at the edge of the circle for very high Chroma (saturation) or "colour purity".

If we take a horizontal slice through the centre, we see a coloured circle. Around the edge of the circle we see every possible saturated colour, or Hue. This circular axis is known as H° for Hue. The units are in the form of degrees° (or angles), ranging from 0 (red) through 90 (yellow), 180 (green), 270 (blue) and back to 0.

For more info, see http://www.colourphil.co.uk/lab_lch_colour_space.html.

SYNOPSIS

    use PDL::LiteF;
    use PDL::IO::Pic;
    use PDL::Graphics::ColorSpace;

    my $image_rgb = PDL->rpic('photo.jpg') if PDL->rpiccan('JPEG');

    # convert RGB value from [0,255] to [0,1]
    $image_rgb = $image_rgb->double / 255;

    my $image_xyz = $image_rgb->rgb_to_xyz( 'sRGB' );

Or

    my $image_xyz = rgb_to_xyz( $image_rgb, 'sRGB' );

OPTIONS

Some conversions require specifying the RGB space which includes gamma curve and white point definitions. Supported RGB space include (aliases in square brackets):

    Adobe RGB (1998) [Adobe]
    Apple RGB [Apple]
    BestRGB
    Beta RGB
    BruceRGB
    CIE
    ColorMatch
    DonRGB4
    ECI
    Ekta Space PS5
    NTSC [601] [CIE Rec 601]
    PAL/SECAM [PAL] [CIE ITU]
    ProPhoto
    SMPTE-C [SMPTE]
    WideGamut
    sRGB [709] [CIE Rec 709]

You can also add custom RGB space definitions via the function add_rgb_space.

CONVERSIONS

The full list of exported functions include rgb_to_cmyk, cmyk_to_rgb, rgb_to_hsl, hsl_to_rgb, rgb_to_hsv, hsv_to_rgb, rgb_to_xyz, xyz_to_rgb, xyY_to_xyz, xyz_to_lab, lab_to_xyz, lab_to_lch, lch_to_lab, rgb_to_lch, lch_to_rgb, lch_to_lab.

Some conversions, if not already included as functions, can be achieved by chaining existing functions. For example, RGB to Lab conversion can be achieved by chaining rgb_to_xyz and xyz_to_lab.

    my $lab = xyz_to_lab( rgb_to_xyz( $rgb, 'sRGB' ), 'sRGB' );

FUNCTIONS

rgb_to_cmyk

  Signature: (double rgb(c=3); double [o]cmyk(d=4))

Converts an RGB color triple to an CYMK color quadruple.

The first dimension of the piddles holding the rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...). The first dimension of the piddles holding the cmyk values must be size 4.

Usage:

    my $cmyk = rgb_to_cmyk( $rgb );

If rgb_to_cmyk encounters a bad value in any of the R, G, or B values the output piddle will be marked as bad and the associated C, M, Y, and K values will all be marked as bad.

cmyk_to_rgb

  Signature: (double cmyk(d=4); double [o]rgb(c=3))

Converts an CYMK color quadruple to an RGB color triple

The first dimension of the piddles holding the cmyk values must be size 4, i.e. the dimensions must look like (4, m, n, ...). The first dimension of the piddle holding the rgb values must be 3.

Usage:

    my $rgb = cmyk_to_rgb( $cmyk );

If cmyk_to_rgb encounters a bad value in any of the C, M, Y, or K quantities, the output piddle will be marked as bad and the associated R, G, and B color values will all be marked as bad.

rgb_to_hsl

  Signature: (double rgb(c=3); double [o]hsl(c=3))

Converts an RGB color triple to an HSL color triple.

The first dimension of the piddles holding the hsl and rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

Usage:

    my $hsl = rgb_to_hsl( $rgb );

If rgb_to_hsl encounters a bad value in any of the R, G, or B values the output piddle will be marked as bad and the associated H, S, and L values will all be marked as bad.

hsl_to_rgb

  Signature: (double hsl(c=3); double [o]rgb(c=3))

Converts an HSL color triple to an RGB color triple

The first dimension of the piddles holding the hsl and rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

Usage:

    my $rgb = hsl_to_rgb( $hsl );

If hsl_to_rgb encounters a bad value in any of the H, S, or V quantities, the output piddle will be marked as bad and the associated R, G, and B color values will all be marked as bad.

rgb_to_hsv

  Signature: (double rgb(c=3); double [o]hsv(c=3))

Converts an RGB color triple to an HSV color triple.

The first dimension of the piddles holding the hsv and rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

Usage:

    my $hsv = rgb_to_hsv( $rgb );

If rgb_to_hsv encounters a bad value in any of the R, G, or B values the output piddle will be marked as bad and the associated H, S, and V values will all be marked as bad.

hsv_to_rgb

  Signature: (double hsv(c=3); double [o]rgb(c=3))

Converts an HSV color triple to an RGB color triple

The first dimension of the piddles holding the hsv and rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

Usage:

    my $rgb = hsv_to_rgb( $hsv );

If hsv_to_rgb encounters a bad value in any of the H, S, or V quantities, the output piddle will be marked as bad and the associated R, G, and B color values will all be marked as bad.

xyY_to_xyz

  Signature: (double xyY(c=3); double [o]xyz(c=3))

Internal function for white point calculation. Use it if you must.

xyY_to_xyz processes bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

lab_to_lch

  Signature: (double lab(c=3); double [o]lch(c=3))

Converts an Lab color triple to an LCH color triple.

The first dimension of the piddles holding the lab values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

Usage:

    my $lch = lab_to_lch( $lab );

If lab_to_lch encounters a bad value in any of the L, a, or b values the output piddle will be marked as bad and the associated L, C, and H values will all be marked as bad.

lch_to_lab

  Signature: (double lch(c=3); double [o]lab(c=3))

Converts an LCH color triple to an Lab color triple.

The first dimension of the piddles holding the lch values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

Usage:

    my $lab = lch_to_lab( $lch );

If lch_to_lab encounters a bad value in any of the L, C, or H values the output piddle will be marked as bad and the associated L, a, and b values will all be marked as bad.

rgb_to_xyz

Converts an RGB color triple to an XYZ color triple.

The first dimension of the piddles holding the rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

If rgb_to_xyz encounters a bad value in any of the R, G, or B values the output piddle will be marked as bad and the associated X, Y, and Z values will all be marked as bad.

Usage:

    my $xyz = rgb_to_xyz( $rgb, 'sRGB' );

xyz_to_rgb

Converts an XYZ color triple to an RGB color triple.

The first dimension of the piddles holding the xyz and rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

If xyz_to_rgb encounters a bad value in any of the X, Y, or Z values the output piddle will be marked as bad and the associated R, G, and B values will all be marked as bad.

Usage:

    my $rgb = xyz_to_rgb( $xyz, 'sRGB' );

xyz_to_lab

Converts an XYZ color triple to an Lab color triple.

The first dimension of the piddles holding the xyz values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

If xyz_to_lab encounters a bad value in any of the X, Y, or Z values the output piddle will be marked as bad and the associated L, a, and b values will all be marked as bad.

Usage:

    my $lab = xyz_to_lab( $xyz, 'sRGB' );

lab_to_xyz

Converts an Lab color triple to an XYZ color triple.

The first dimension of the piddles holding the lab values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

If lab_to_xyz encounters a bad value in any of the L, a, or b values the output piddle will be marked as bad and the associated X, Y, and Z values will all be marked as bad.

Usage:

    my $xyz = lab_to_xyz( $lab, 'sRGB' );

rgb_to_lch

Converts an RGB color triple to an LCH color triple.

The first dimension of the piddles holding the rgb values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

If rgb_to_lch encounters a bad value in any of the R, G, or B values the output piddle will be marked as bad and the associated L, C, and H values will all be marked as bad.

Usage:

    my $lch = rgb_to_lch( $rgb, 'sRGB' );

lch_to_rgb

Converts an LCH color triple to an RGB color triple.

The first dimension of the piddles holding the lch values must be size 3, i.e. the dimensions must look like (3, m, n, ...).

If lch_to_rgb encounters a bad value in any of the L, C, or H values the output piddle will be marked as bad and the associated R, G, and B values will all be marked as bad.

Usage:

    my $rgb = lch_to_rgb( $lch, 'sRGB' );

add_rgb_space

Supports adding custom RGB space definitions.

Usage:

    my %custom_space = (
        custom_1 => {
          'gamma' => '2.2',
          'm' => [
                   [
                     '0.467384242424242',
                     '0.240995',
                     '0.0219086363636363'
                   ],
                   [
                     '0.294454030769231',
                     '0.683554',
                     '0.0736135076923076'
                   ],
                   [
                     '0.18863',
                     '0.075452',
                     '0.993451333333334'
                   ]
                 ],
          'mstar' => [
                       [
                         '2.74565437614039',
                         '-0.969256810842655',
                         '0.0112706581772173'
                       ],
                       [
                         '-1.1358911781912',
                         '1.87599300082369',
                         '-0.113958877125197'
                       ],
                       [
                         '-0.435056564214666',
                         '0.0415556222493375',
                         '1.01310694059653'
                       ]
                     ],
          'white_point' => [
                             '0.312713',
                             '0.329016'
                           ],
        },
        custom_2 => { ... },
    );

    add_rgb_space( \%custom_space );

    my $rgb = lch_to_rgb( $lch, 'custom_1' );

SEE ALSO

Graphics::ColorObject

AUTHOR

~~~~~~~~~~~~ ~~~~~ ~~~~~~~~ ~~~~~ ~~~ `` ><(((">

Copyright (C) 2012 Maggie J. Xiong <maggiexyz+github@gmail.com>

Original work sponsored by Shutterstock, LLC http://www.shutterstock.com/

All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation as described in the file COPYING in the PDL distribution.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 79:

Non-ASCII character seen before =encoding in 'H°.'. Assuming UTF-8