Graphics::Toolkit::Color::Space::Hub(3)

NAME
Graphics::Toolkit::Color::Space::Hub - (de-)convert and deformat color value tuples

SYNOPSIS
Central store for all color space objects, which hold color space specific information and algorithms. Home to all methods that have to iterate over all color spaces.

    use Graphics::Toolkit::Color::Space::Hub;
    my $true = ...::Space::Hub::is_space_name( 'HSL' );
    my $HSL = ..Space::Hub::get_space( 'HSL');
    my $RGB = Graphics::Toolkit::Color::Space::Hub::default_space();
    ...Space::Hub::all_space_names();                        # all space names and aliases

    ...::Space::Hub::convert(   'HSL' [0, 0, 1]);               # [2/3, 1, 0]
    ...::Space::Hub::deconvert( 'HSL' [2/3, 1, 0]);             # [  0, 0, 1]
    ...::Space::Hub::deformat(  '#0000ff' );                    # [  0, 0, 1], 'RGB' , 'hex_string'
    ...::Space::Hub::distance(  [2/3, 1, 0], [0, 1, 0], 'HSL' );# 1/3

DESCRIPTION
This module is supposed to be used only internally and not directly by the user, unless he wants to add his own color space. Therefore it exports no symbols and the methods are much less DWIM then the main module. But lot of important documentation is still here.

COLOR SPACES
Up next, a listing of all supported color spaces. These are mathematical constructs that associate each color with a point inside this space. The numerical values of a color definition become coordinates along axis that express different properties. The closer two colors are along an axis the more similar are they in that property. All color spaces are finite and only certain value ranges along an axis are acceptable. Most spaces have 3 dimensions (axis) and are completely linear like in Euclidean (everyday) geometry. A few spaces have more axis and some spaces are cylindrical. That means that some axis are not lines but circles and the associated value describes an angle.

Color definitions contain either the name of a space or the names of its axis (long or short). If the space name or its long alias is used, the values have to be provided in the same order as the axis described here.

Color space or axis names may be written in any combination of upper and lower case characters, but I recommend using the spelling presented here. Each axis has also two specific names, one long and one short, which are in rare cases equal. To define a color according to a space you need to provide for each axis one value, that is inside the required value range and of a specified type (int or real with amount of decimals).

While I acknowledge that some of the spaces below should be called systems to be technically correct, they still will be called spaces here, because the main goal of this software is seamless interoperability between them.

  RGB
... (alias SRGB - standard RGB) is the default color space of this CPAN module. It is used by most computer hardware like monitors and follows the logic of additive color mixing as produced by an overlay of three colored light beams. The sum of all colors will be white, as in opposite to subtractive mixing. It is a completely Cartesian (Euclidean) 3D space and thus a RGB tuple consists of three integer values:

red (short r) range: 0 .. 255, green (short g) range: 0 .. 255 and blue (short b) range: 0 .. 255. A higher value means a stronger beam of that base color flows into the mix above a black background, so that black is (0, 0, 0), white (255, 255, 255) and a pure red (fully saturated color) is (255, 0, 0). RGB has a gamma of 2.4, which is clipped in the corners (piecewise transfer function).

  LinearRGB
Same as "RGB" but with normal value ranges (0 .. 1) and removed piecewise gamma correction. That means there is a linear correlation between the shown values and the RGB brightness of the pixel on the screen without any adaptation to human sensibilities. Its alias name is LinRGB.

  CMY
is the opposite of "RGB" since it follows the logic of subtractive color mixing as used in printing. Think of it as the amount of colored ink on white paper, so that white is (0,0,0) and black (1,1,1). It uses normalized real value ranges: 0 .. 1. An CMY tuple has also three values:

cyan (short c) is the inverse of red, magenta (short m ) is inverse to green and yellow (short y) is inverse of blue.

  CMYK
is an extension of "CMY" with a fourth value named key (short k), which is the amount of black ink mixed into the CMY color. It also has an normalized range of 0 .. 1. This should not bother you since you are free to change the range at your preference.

  HSL
.. is a cylindrical space that orders colors along cognitive properties. The first dimension is the angular one and it rotates in 360 degrees around the rainbow of fully saturated colors: 0 = red, 15 approximates orange, 60 - yellow 120 - green, 180 - cyan, 240 - blue, 270 - violet, 300 - magenta, 330 - pink. 0 and 360 points to the same coordinate. This module only outputs 0, even if accepting 360 as input. The second, linear dimension (axis) measures the distance between a point the center column of the cylinder at the same height, no matter in which direction. The center column has the value 0 (white .. gray .. black) and the outer mantle of the cylinder contains the most saturated, purest colors. The third, vertical axis reaches from bottom value 0 (always black no matter the other values) to 100 (always white no matter the other values). In summary: HSL needs three integer values: hue (short h) (0 .. 359), saturation (short s) (0 .. 100) and lightness (short l) (0 .. 100).

  HSV
... is also cylindrical but can be shaped like a cone. Similar to HSL we have hue and saturation, but the third axis is named value (short v). In HSL we always get white, when lightness is 100. In HSV additionally saturation has to be zero to get white. When saturation is 100 and value is 100 we have the purest, most saturated color of whatever hue sets. So unlike in "HSL", here every color has its unique coordinates.

  HSB
Is an alias to "HSV", just the value axis is renamed with brightness (b).

  HWB
An inverted "HSV", where the saturated, pure colors are on the center column of the cylinder. It still has the same circular hue dimension with an integer range of 0 .. 360. The other two, linear dimensions (also 0 .. 100 integer range with optional suffix '%') are whiteness (w) and blackness (b). They describe how much white or black are mixed into the pure hue. If both are zero, you get a pure color. whiteness of 100 always leads to white and blackness of 100 always leads to black. The space is truncated as a cone so the sum of whiteness and blackness can never be greater than 100.

  NCol
Is a more human readable variant of the "HWB" space with an altered hue values, that consists of a letter and two digits. The letter demarks one of the six areas around the rainbow. R is Red, Y (Yellow), G (Green), C (Cyan), B (Blue), M (Magenta). The two digits after this letter are an angular value, measuring the distance between the pure color (as stated by the letter) and the described color (toward the next color on the rainbow). The whiteness and blackness axis have integer values with the suffix '%', since they are percentual values as well.

  YIQ
Is a space developed for NTSC to broadcast a colored television signal, which is still compatible with black and white TV. It achieves this by sending the luminance (short y) (sort of brightness with real range of 0 .. 1) in channel number one, which is all black and white TV needs. Additionally we have the axis of in-phase (short i) (cyan - orange - balance, range -0.5959 .. 0.5959) and quadrature (short q) (magenta - green - balance, range: -0.5227 .. 0.5227).

  YUV
Is a slightly altered version of YIQ for the PAL TV standard. We use a variant called YPbPr, which can also be used as space name. It has computation friendly value ranges and is still relevant in video and image formats and compression algorithms, but under the name YCbCr. The only difference is that YCbCr works with digital values but this module computes with real (analogue) values to enable any precision the user might prefer. To make this clear, this space holds the name YPbPr. It has three Cartesian axis: 1. luma (short y) with a real value range of 0..1, 2. Pb (short u, -0.5 .. 0.5) and 3. Pr (short v, -0.5 .. 0.5). (see also "CIELUV")

  HunterLAB
predecessor of "CIELAB" by Richard S. Hunter with no alias name and slightly different color transitions on yellow-blue-direction. The axis have same long and short names: L with normal values (0 .. 1), a -172.30 .. 172.30 and b -67.20 .. 67.20.

  CIEXYZ
this space (alias XYZ) has the axis X, Y and Z (long and short axis names are this time the same), that refer to the red, green and blue receptors (cones) in the retina (on the back side of the eye). Because those cones measure a lot more left and right than just exactly those colors, they got these technical names. The values in that space tell you about the amount of chemical and neurological activity a color produces inside the eye. The values ranges are always from zero and reach 95.047 on the "X" - axis, 100 on "Y" and 108.883 on "Z", due to the used white point of D65 which has these values divided by 100. The illuminant D65 is standard for all CIE spaces except CIERGB.

  CIELAB
(alias LAB) is a derivate of "CIEXYZ" that reorders the colors along axis that reflect how the brain processes them. It uses three information channels. One named L (lightness) with a real value range of (0 .. 100). Second is channel a, that reaches from red to green (-500 .. 500) and thirdly b from yellow to blue (-200 .. 200). Values will be displayed with three decimals. The long names of the axis names contain a '*' and are thus: L*, a* and b*. The a and b axis reflect the opponent color theory.

  CIELUV
(alias LUV) is a more perceptually uniform version of "CIELAB" and the axis a and b got renamed to u and v (see "YUV") but did not change their meaning. It has also three Cartesian dimension named L*, u* and v*, (short names have only the first letter of these names). They have real valued ranges, which are 0 .. 100, -134 .. 220 and -140 .. 122.

  CIELCHab
(alias LCH) is the cylindrical version of the "CIELAB" with the dimensions luminance, chroma and hue - in short l, c and h. The real valued ranges are from zero to 100, 539 and 360 respectively. Like with the "HSL" and "HSV", hue is the circular dimensions and its values are meant as degrees in a circle. For gray colors in the middle column the value chroma has no importance and will be in this implementation always be zero.

  CIELCHuv
(alias LCHuv) is the cylindrical version of the "CIELUV" and works similar to CIELCHab except the real valued range of chroma is (0 .. 261) and the space has no alias name.

  CIERGB
1931 standardized, normal valued (0 .. 1) space with same axis as "RGB": red (r), green (g) and blue (b). It has the illuminant E and no gamma (linear RGB).

  AdobeRGB
(alias name opRGB) is a normalized "RGB" variant with the CIE white point D65 a gamma of about 2.2.

  AppleRGB
normal "RGB" variant with white point of D65 and gamma is 1.8. It is the legacy color space Apple used in the 90ies.

  ProPhotoRGB
(alias name ROMMRGB) normal "RGB" variant with a white point of D50, gamma = 1.8 and a wide gamut.

  WideGamutRGB
Even greater gamut then previous spaces with white point D50 and gamma of ~2.2.

  Display P3
wide gamut RGB variant with same gamma function as "RGB", has alias name P3.

  Display P3 Linear
P3 variant without any gamma - wider gamut variant of \LinearRGB.

  DCI-P3
Simply Display P3 Linear with a gamma of 2.4, has alias name SMPTE P3.

  Rec.709
RGB space, has alias name BT.709.

  Rec.2020
RGB space, has alias name BT.2020.

  OKLAB
is a modern improvement of "CIELAB" by Bjoern Ottosson with no alias name and for nicer color transitions and better numeric behaviour. The axis long names are same as the same ones: L with values (0 .. 1), a and b with both (-0.5 .. 0.5). If you want to use it like in CSS, just add "range => [100, [-120,120], [-120,120]], suffix => '%'".

  OKLCH
is the cylindrical variant of "OKLAB" just parallels "CIELCHab". The axis names are again: luminance, chroma and hue - in short: l, c and h. Value ranges are similar as in "OKLAB": luminance 0 .. 1 (normal), chroma 0 .. 0.5 and hue are angles of 0 .. 360 degrees. Also if you prefer a CSS compatible format, use "range => [100, 120, 360]" and a preferred suffix.

RANGES
As pointed out in the previous paragraph, each dimension of color space has its default range. However, one can demand custom value ranges, if the method accepts a range descriptor as argument. If so, the following values are accepted:

    'normal'          real value range from 0 ..   1 (default)
    'percent'         real value range from 0 .. 100
     number           integer range from zero to that number
    [0 1]             real number range from 0 to 1, same as 'normal'
    [min max]         range from min .. max, int if both numbers are int
    [min max 'int']   integer range from min .. max
    [min max 'real']  real number range from min .. max

The whole definition has to be an ARRAY ref. Each element is the range definition of one dimension. If the definition is not an ARRAY but a single value it is applied as definition of every dimension.

FORMATS
Unless stated otherwise, these formats are available in all color spaces.

  list
A list of values, the first being the name of the color space. The name can be omitted, if it is the default color space ("RGB"). Default format of the output method "values".

    (10, 20, 30)
    ('XYZ', 15, 3.53, 37.1)

  named_array
The same with squared brackets around.

    [RGB => 10, 20, 30]

  named_string
Same inside a quotes.

    'RGB: 10, 20, 30'

  css_string
Strings for usage in CSS, SVG files and alike. Here are commas optional. There are two spots where space is not allowed: 1. Between the space name and opening bracket and between axis value and value suffix (here '%').

    'rgb(10, 20, 30)'
    'hsl(10  20%  30%)'

  hex_string
String for websites and alike, RGB only. Long and short form can be read and output is long form only.

    '#1020FF'
    '#12F'

  hash
Hash reference with long axis names.

    { red => 10, green => 20, blue => 30 }

  char_hash
Hash reference with short axis names.

    { r => 10, g => 20, b => 30 }

ROUTINES
This package provides two sets of routines. The first is just a lookup of what color space objects are available. What are their names to retrieve them? The second set consists of 5 routines that can handle a lot of unknowns. The are:

    1. convert               (RGB -> any)
    2. deconvert             (any -> RGB)
    3. deformat              (extract values)
    4. deformat_partial_hash (deformat hashes with missing axis)
    5. distance              (distance between 2 colors in any space)

  all_space_names
Returns a list of string values, which are the names of all available color space. See "COLOR-SPACES".

  is_space_name
Needs one argument, that is supposed to be a color space name. If it is, the result is an 1, otherwise 0 (perlish pseudo boolean).

  get_space
Needs one argument, that is supposed to be a color space name. If it is, the result is the according color space object, otherwise undef.

  try_get_space
Same thing but if nothing is provided it returns the default space.

  default_space
Return the color space object of (currently) RGB name space. This name space is special since every color space object provides converters from and to RGB, but the RGB itself has no converter.

  convert
Converts a value tuple (first argument) from base space (RGB) into any space mentioned space (second argument - see "COLOR-SPACES"). The values have to be normalized (inside 0..1). If there are outside the acceptable range, there will be clamped, so that the result will also normal. If the third argument is positive (pseudo boolean true), the output will also be normal. Arguments four and five are for internal use to omit rounding errors. They are the original, normalized values and their color space. When during the conversion, the method tries to convert into the space of origin, it replaces the current values with the source values.

    # convert from RGB to  HSL
    my @hsl = Graphics::Toolkit::Color::Space::Hub::convert( [0.1, 0.5, .7], 'HSL' );

  deconvert
Converts the result of "deformat" into a RGB value tuple.

    # convert from HSL to RGB
    my @rgb = Graphics::Toolkit::Color::Space::Hub::deconvert( [0.9, 0.5, 0.5], 'HSL' );

  deformat
Extracts the values of a color definition in any space or format. That's why it takes only one argument, a scalar that can be a string, ARRAY ref or HASH ref. The result will be three values. The first is a ARRAY (tuple) with all the unaltered, not clamped and not rounded and not normalized values. The second is the name of the recognized color name space. Third is the format name.

    my ($values, $space) =  Graphics::Toolkit::Color::Space::Hub::deformat( 'ff00a0' );
    # [255, 10, 0], 'RGB'
    ($values, $space) =  Graphics::Toolkit::Color::Space::Hub::deformat( [255, 10 , 0] ); # same result

  deformat_partial_hash
This is a special case of the deformat routine for the hash and char_hash format (see "FORMATS"). It can tolerate missing values. The result will also be a tuple (ARRAY) with missing values being undef. Since there is a given search order, a hash with only a hue value will always assume a HSL space. To change that you can provide the space name as a second, optional argument.

  distance

SEE ALSO

•   Convert::Color

COPYRIGHT & LICENSE
Copyright 2023-26 Herbert Breunung.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR
Herbert Breunung, <lichtkind@cpan.org>