ColorObject(3pm)
okay bestie, welcome to ColorObject(3pm) β the Graphics::ColorObject manual page. it's giving color-conversion main character energy, and we're gonna get you all the way together. π
β¨ NAME β¨
Graphics::ColorObject β convert between color spaces. periodt.
π¦ SYNOPSIS π¦
First, you bring the module in:
use Graphics::ColorObject;
Then, like, do the thing:
# rgb to hsv
$color = Graphics::ColorObject->new_RGB([$r, $g, $b]);
($h, $s, $v) = @{ $color->as_HSV() };
# one rgb space to another (NTSC to PAL)
$color = Graphics::ColorObject->new_RGB([$r, $g, $b], space=>'NTSC');
($r, $g, $b) = @{ $color->as_RGB(space=>'PAL') };
πΈ ABSTRACT πΈ
Use this module to convert between all the common color spaces. As a pure Perl module, it is not very fast, and so it you want to convert entire images quickly, this is probably not what you want. The emphasis is on completeness and accurate conversion.
Supported color spaces are: RGB (including sRGB, Apple, Adobe, CIE Rec 601, CIE Rec 709, CIE ITU, and about a dozen other RGB spaces), CMY, CMYK, HSL, HSV, XYZ, xyY, Lab, LCHab, Luv, LCHuv, YPbPr, YCbCr, YUV, YIQ, PhotoYCC.
Conversion between different RGB working spaces, and between different white-points, is fully supported.
π DESCRIPTION π
For any supported color space XXX, there is one constructor new_XXX that creates a color using data in that color space, and one method as_XXX that returns the current color as expressed in that color space. For example, for RGB there is new_RGB and as_RGB. The color data is always passed as an array reference to a three-element array (four-element in the case of CMYK). Thus, to convert from RGB to HSL, you can use:
$color = Graphics::ColorObject->new_RGB([$r, $g, $b]);
($h, $s, $l) = @{ $color->as_HSL() };
The constructor can always take a hash of optional arguments in addition to the color value, namely the working RGB space and the white point. For example:
$color = Graphics::ColorObject->new_RGB([$r, $g, $b], space=>'Adobe', white_point=>'D65');
For a list of all supported color spaces, call Graphics::ColorObject->list_colorspaces(). For a list of all RGB working spaces and of all white points that this module supports, call Graphics::ColorObject->list_rgb_spaces() and Graphics::ColorObject->list_white_points().
If not specified, the working RGB space will be sRGB. Many non-RGB conversions also rely on an implicit RGB space, and passing an RGB space as an option (either to the constructor or later) will have an effect on the values.
π VARIOUS NOTES AND GOTCHAS π
Most conversions will return out-of-gamut values if necessary, because that way they are lossless and can be chained in calculations, or reversed to produce the original values. Many conversion methods will take an optional boolean "clip" parameter to restrict the returned values to be within gamut:
($r, $g, $b) = @{ $color->as_RGB(space=>'sRGB', clip=>1) };
Currently clipping is supported in RGB, RGB-derived (HSL, CMY) and chroma-luma separated (YUV, etc) spaces, but not in XYZ-derived spaces. The only way to check whether a value is within gamut is to convert it with and without the clip option and compare the two results. An RGB value is within gamut simply if R, G and B are between 0 and 1, but other spaces can be much harder to check.
RGB values are non-linear (gamma-adjusted) floating-point values scaled in the range from 0 to 1. If you want integer values in the range 0..255, use the new_RGB255/as_RGB255 functions instead. If you want linear RGB (not gamma-adjusted) use RGB_to_linear_RGB([$r, $g, $b]).
Functions that use an angle value always express it in degrees from 0 to 360. That includes the hue H in HSL, HSV, LCHab and LCHuv. Use rad2deg and deg2rad from Math::Trig to convert to/from degrees if necessary.
There is some confusion in the naming of YUV and related (Y-something-something) colorspaces. Most of the time when "YUV" or "YCC" is used in software, for example in JPEG and MPEG2, that is actually YCbCr, a chroma-luma separated space with integer values of Y in the range [16..235], Cb and Cr in [16..240]. JPEG uses a modified YCbCr with values in [0..255] (which is not implemented in this module). As used here, YUV is a floating-point representation of the analog signal in PAL TV, YIQ is the same for NTSC TV, YPbPr is component analog video, and PhotoYCC or YCC is the Kodak PhotoCD standard.
The set_white_point() function can take arbitrary temperatures as well as the predefined standard illuminants. The valid range of temperatures is from 4000K to 25000K.
π RECOMMENDATIONS π
Aside from converting from one space to another, what colorspace is the best one to use for a particular task? This section attempts to answer that question.
- For "generic" RGB values, use
sRGB(which is the default). - For 2D effects filters, use
Lab(orLCHab). - For adjustment of brightness, saturation and hue, use
LCHaborLSHab. - For compression, use
YCbCr, or useYPbPrand convert to integer values in a way that makes sense in your application. - For representing data as colors, use
Lab(straight lines between points inLabare more-or-less uniform gradients, unlike straight lines in RGB, for example).
π UPGRADING FROM 0.3a2 AND OLDER VERSIONS π
Version 0.4 and later are a complete rewrite from the previous major version, 0.3a2. The API is completely changed. The old API should be emulated exactly in all cases. Please test any code that uses this module when upgrading. If you encounter any strange behavior, please downgrade to 0.3a2 and email me a bug report. Additionally, the exact values returned by some functions may be slightly different, this is not a bug - the new values are (more) correct.
π METHODS π
okay so here's the whole roster, bestie. Constructors first:
$color = Graphics::ColorObject->new_XYZ([$X, $Y, $Z])$color = Graphics::ColorObject->new_xyY([$x, $y, $Y])$color = Graphics::ColorObject->new_RGB([$R, $G, $B])$color = Graphics::ColorObject->new_RGB255([$R, $G, $B])$color = Graphics::ColorObject->new_RGBhex($rgbhex)$color = Graphics::ColorObject->new_Lab([$L, $a, $b])$color = Graphics::ColorObject->new_LCHab([$L, $C, $H])$color = Graphics::ColorObject->new_Luv([$L, $u, $v])$color = Graphics::ColorObject->new_LCHuv([$L, $C, $H])$color = Graphics::ColorObject->new_HSL([$H, $S, $L])$color = Graphics::ColorObject->new_HSV([$H, $S, $V])$color = Graphics::ColorObject->new_CMY([$C, $M, $Y])$color = Graphics::ColorObject->new_CMYK([$C, $M, $Y])$color = Graphics::ColorObject->new_YPbPr([$Y, $Pb, $Pr])$color = Graphics::ColorObject->new_YCbCr([$Y, $Cb, $Cr])$color = Graphics::ColorObject->new_YUV([$Y, $Cb, $Cr])$color = Graphics::ColorObject->new_YIQ([$Y, $I, $Q])$color = Graphics::ColorObject->new_PhotoYCC([$Y, $C1, $C2])
And then the as_ methods, which hand you back the goods:
($X, $Y, $Z) = @{ $color->as_XYZ() }($R, $G, $B) = @{ $color->as_RGB() }($R, $G, $B) = @{ $color->as_RGB255() }$hex = $color->as_RGBhex()($x, $y, $Y) = @{ $color->as_xyY() }($L, $a, $b) = @{ $color->as_Lab() }($L, $C, $H) = @{ $color->as_LCHab() }($L, $u, $v) = @{ $color->as_Luv() }($L, $C, $H) = @{ $color->as_LCHuv() }($H, $S, $L) = @{ $color->as_HSL() }($H, $S, $V) = @{ $color->as_HSV() }($C, $M, $Y) = @{ $color->as_CMY() }($C, $M, $Y, $K) = @{ $color->as_CMYK() }($Y, $Pb, $Pr) = @{ $color->as_YPbPr() }($Y, $Cb, $Cr) = @{ $color->as_YCbCr() }($Y, $U, $V) = @{ $color->as_YUV() }($Y, $I, $Q) = @{ $color->as_YIQ() }($Y, $C1, $C2) = @{ $color->as_PhotoYCC() }
And the rest of the methods, with their whole vibe:
$white_point = $color->get_white_point()β Returns the name of the current white point. Value is one of the entries returned fromlist_white_points, such as"D65", or a color temperature.$color->set_white_point("D65")β Sets the current white point by name. Argument is one of the entries returned fromlist_white_points, or a temperature value like"6800K". This changes the current color slightly since white-point adaptation is not completely reversible. This does not affect the current RGB space, thus it is possible to use RGB spaces at whitepoints other than those they were defined at.$rgb_space = $color->get_rgb_space()β Returns the name of the current RGB color space. Value is one of the entries returned fromlist_rgb_spaces, such as"NTSC".$color->set_rgb_space("NTSC")β Sets the current RGB color space by name. Argument is one of the entries returned fromlist_rgb_spaces. This may change the current color if the old and new spaces have different white points.$color2 = $color->copy()β Creates an exact duplicate of the current color.if ($color->equals($color2)) { ... }β Checks if another color is the same as this one. Optionally takes an accuracy parameter which is the distance between the two colors as measured by the city-block metric in XYZ space (default accuracy is0.01%).$d = $color->difference($color2)β Calculates the difference between this color and another one. The difference measure is (approximately) perceptually uniform.@colorspaces = &Graphics::ColorObject->list_colorspaces()β Returns a list of all supported colorspaces.@rgb_spaces = &Graphics::ColorObject->list_rgb_spaces()β Returns a list of all supported RGB spaces. Some items are aliases, so the same space may be listed more than once under different names.@white_points = &Graphics::ColorObject->list_white_points()β Returns a list of all supported white points.
π EXPORT π
None by default. The 'all' tag causes the non-object-oriented interface to be exported, and you get all the XXX_to_YYY functions, for example RGB_to_XYZ. Please note that some of these functions need extra arguments in addition to the color value to be converted.
π BUGS π
Backwards compatibility with versions before 0.4 is not very well tested.
This module will produce results that are, in some cases, different from other software. Most of the time that is not a bug in this module, but rather a case where the other software uses an approximate (trading accuracy for speed) algorithm. That is particularly true for YUV and related conversions which are often implemented using integer-math approximations. As far as possible, this module produces results which are exact according to the definitions in the relevant CIE/ITU or other standards.
Some color transformations are not exactly reversible. In particular, conversions between different white points are almost but not exactly reversible. This is not a bug.
There is no way to choose any other color-adaptation algorithm than the Bradford algorithm. That is probably ok since the Bradford algorithm is better than other algorithms (such as Von Kries or simple scaling).
There is no way to choose a RGB space other than the built-in ones.
Support for CMYK is very basic, it relies on assumptions that completely do not work in the physical world of subtractive pigment mixtures. If you tried to convert an image to CMYK directly for printing using these functions, the results will not be very good, to say the least.
π TODO π
okay so here's the lil wishlist of stuff that still needs to happen, bestie β future plans, no cap:
- Add clipping to gamut for every color space. β¨
- Choose between several clipping algorithms (nearest, luminance-preserving, hue-preserving). π
- Add a simpler way to check whether something is within gamut. π
- Add user-defined RGB spaces. π¦
- Calculate RGB matrices from chromaticity coordinates. π
- Only load non-RGB matrices once at startup. π§
- Add colorspaces:
uvw,YOZ,RYB, others? π - Add RGB spaces:
ROMM, others? π - Convert arrays of colors efficiently (maybe someday in C). π₯
π SEE ALSO π
- The Color FAQ by Charles Poynton is, like, the definitive reference on the subject β an actual icon:
http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.txtπ - Bruce Lindbloom's web site has a tremendous amount of color info, fr fr:
http://www.brucelindbloom.com/index.html?Math.htmlπ
π AUTHOR π
Alex Izvorski, <izv@dslextreme.com> β main character energy.
Alfred Reibenschuh <alfredreibenschuh@yahoo.com> was the original author for versions up to 0.3a2. the OG, we love her.
many many thanks to: π«Ά
- Alfred Reibenschuh
<alfredreibenschuh@yahoo.com>for the previous versions ofGraphics::ColorObject, and for theHSL/HSV/CMYKcode. she did that π - Bruce Lindbloom
<info@brucelindbloom.com>for providing a wealth of information on color space conversion and color adaptation algorithms, and for the precalculated RGB conversion matrices. literally carrying this whole thing π - Charles Poynton
<colorfaq@poynton.com>for the Color FAQ. iconic reference, no notes β¨ - Timo Autiokari
<timo.autiokari@aim-dtp.net>for information on white points. π€
π COPYRIGHT AND LICENSE π
Copyright 2003-2005 by Alex Izvorski
Portions Copyright 2001-2003 by Alfred Reibenschuh
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
aaaand that's a wrap bestie π this page was for perl v5.36.0, dated 2023-09-29, and it's ColorObject(3pm) π go forth and convert some colors, you got this π¦β¨
π¦ where this page comes from
- debian trixie: libgraphics-colorobject-perl 0.5.0-10.1 Β· Artistic-1.0
- ubuntu noble: libgraphics-colorobject-perl 0.5.0-10.1 Β· Artistic-1.0
- ubuntu resolute: libgraphics-colorobject-perl 0.5.0-10.1 Β· Artistic-1.0
- ubuntu stonking: libgraphics-colorobject-perl 0.5.0-10.1 Β· Artistic-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms