Color::Calc(3pm)
π NAME π
okay so bestie, meet Color::Calc (you'll see it filed under Color::Calc(3pm) if you're being all formal π
) β it does simple calculations with RGB colors. that's the whole vibe. π¨β¨
π¦ SYNOPSIS π¦
use Color::Calc ();
my $background = 'green';
print 'background: ',Color::Calc::color_html($background),";\n";
print 'border-top: solid 1px ',Color::Calc::light_html($background),";\n";
print 'border-bottom: solid 1px ',Color::Calc::dark_html($background),";\n";
print 'color: ',Color::Calc::contrast_bw_html($background),";\n";
πΈ DESCRIPTION πΈ
The Color::Calc module implements simple calculations with RGB colors. Which is lowkey a superpower, because you can cook up a whole entire color scheme from just a couple of colors. main character energy fr fr π
β¨ USAGE β¨
π Constructors π
Color::Calc->new( ... )β this class method creates a brand newColor::Calcobject. iconic.
use Color::Calc();
my $cc = new Color::Calc( 'ColorScheme' => 'X', OutputFormat => 'HTML' );
print $cc->invert( 'white' );
It accepts the following parameters:
ColorSchemeβ one of the color schemes accepted byGraphics::ColorNames, which is what gets used to interpret color names on input. Valid values includeX(the color names used in X-Windows) andHTML(the color names defined in the HTML 4.0 specification). For a full list of possible values, please refer to the documentation ofGraphics::ColorNames.
Unlike Graphics::ColorNames, barewords are always interpreted as a module name under Graphics::ColorNames. If you really want to use a filename like foo, you have to write it as ./foo.
Default: X (Note: This is incompatible with HTML color names).
OutputFormatβ one of the output formats defined by this module. Possible values are:tupleβ returns a list of three values in the range 0..255. The first value is guaranteed to have alengththat is not a multiple of three.hexβ returns a hexadecimal RGB value as a scalar that contains a string in the formatRRGGBBand a number representing the hexadecimal number0xRRGGBB.htmlβ returns a string compatible with W3C's HTML and CSS specifications, i.e.#RRGGBBor one of the sixteen HTML color names.obj(DEPRECATED) β returns aColor::Objectreference. The moduleColor::Objectmust be installed, of course.objectβ returns aGraphics::ColorObjectreference. The moduleGraphics::ColorObjectmust be installed, of course.pdfβ returns a string compatible withPDF::API2, i.e.#RRGGBB.__MODEvar(DEPRECATED) β uses the value of$Color::Calc::MODEto select one of the above output formats. You should uselocalwhen setting this variable:
local $Color::Calc::MODE = 'html';
Default: __MODEvar (for compatibility)
Color::Calc->import( ... )β this method creates a new, hidden object and binds its methods to the namespace of the calling module.
This method is usually not called directly but from perl's use statement:
use Color::Calc(
'ColorScheme' => 'X',
'OutputFormat' => 'HTML',
'Prefix' => 'cc' );
print cc_invert( 'white' ); # prints 'black'
On import, you can specify the following parameters:
ColorSchemeβ see above. β¬οΈOutputFormatβ see above. β¬οΈPrefixβ adds a prefix to the front of the method names. The calculation methods are bound to the name prefix_method_name (the specified prefix, an underscore, the calculation method's name). Further, prefix is made an alias for prefix"_get".
Default: color
Please note that with perl's use and import statements, omitting the list and specifying an empty list has different meanings:
use Color::Calc; # import with default settings (see below)
use Color::Calc(); # don't import anything
π§ Property "set"/"get" methods π§
These methods are inaccessible without an object reference, i.e. when the functions have been imported.
$cc->set_output_format( $format)β changes the output format for an existingColor::Calcobject.
π Calculation methods π
All calculation methods always accept the following formats for $color or $color1/$color2:
- An arrayref pointing to an array with three elements in the range 0..255 corresponding to the red, green, and blue component.
- A list of three values in the range 0..255 corresponding to the red, green, and blue component where the first value does not have 3 or a multiple of 3 digits (e.g.
('0128',128,128)). - A string containing a hexadecimal RGB value like
#RGB/#RRGGBB/#RRRGGGBBB/..., orRGB/RRGGBB/RRRGGGBBB/... - A color name accepted by
Graphics::ColorNames. The interpretation is controlled by theColorSchemeparameter. - A
Graphics::ColorObjectreference.
The calculation methods can be either accessed through a Color::Calc object reference (here: $cc) or through the method names imported by import (here using the prefix color).
$cc->get($color)/color($color)β returns$coloras-is (but in the selected output format). This function can be used for color format conversion/normalisation. slay, a little transformer β¨$cc->invert($color)/color_invert($color)β returns the inverse of$color.$cc->opposite($color)/color_opposite($color)β returns a color that is on the opposite side of the color wheel but roughly keeps the saturation and lightness.$cc->bw($color)/color_bw($color)$cc->grey($color)/color_grey($color)$cc->gray($color)/color_gray($color)β converts$colorto greyscale.$cc->round($color, $value_count)/color_round($color, $value_count)β rounds each component to the nearest number determined by dividing the range 0..255 into$value_count+1portions.
The default for $value_count is 6, yielding 6^3 = 216 colors. Values that are one higher than divisors of 255 yield the best results (e.g. 3+1, 5+1, 7+1, 9+1, 15+1, 17+1, ...).
$cc->safe($color)/color_safe($color)β rounds each color component to a multiple of0x33(dec. 51) or to a named color defined in the HTML 4.01 specification.
Historically, these colors have been known as web-safe colors. They still provide a convenient color palette.
$cc->mix($color1, $color2 [, $alpha])/color_mix($color1, $color2 [, $alpha])β returns a color that is the mixture of$color1and$color2.
The optional $alpha parameter can be a value between 0.0 (use $color1 only) and 1.0 (use $color2 only), the default is 0.5.
$cc->light($color [, $alpha])/color_light($color [, $alpha])β returns a lighter version of$color, i.e. returnsmix($color,[255,255,255],$alpha).
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use [255,255,255] only), the default is 0.5.
$cc->dark($color [, $alpha])/color_dark($color [, $alpha])β returns a darker version of$color, i.e. returnsmix($color,[0,0,0],$alpha).
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use [0,0,0] only), the default is 0.5.
$cc->contrast($color [, $cut])/color_contrast($color [, $cut])β returns a color that has the highest possible contrast to the input color.
This is done by setting the red, green, and blue values to 0 if the corresponding value in the input is above ($cut * 255) and to 255 otherwise.
The default for $cut is .5, representing a cutoff between 127 and 128.
$cc->contrast_bw($color [, $cut])/color_contrast_bw($color [, $cut])β returns black or white, whichever has the higher contrast to$color.
This is done by returning black if the grey value of $color is above ($cut * 255) and white otherwise.
The default for $cut is .5, representing a cutoff between 127 and 128.
$cc->blend($color [, $alpha])/color_blend($color [, $alpha])β returns a color that blends into the background, i.e. it returnsmix($color,contrast($color),$alpha).
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use contrast($color) only), the default is 0.5.
The idea is that $color is the foreground color, so contrast($color) is similar to the background color. Mixing them returns a color somewhere between them.
You might want to use mix($color, $background, $alpha) instead if you know the real background color.
$cc->blend_bw($color [, $alpha])/color_blend_bw($color [, $alpha])β returns a mix of$colorand black or white, whichever has the higher contrast to$color.
The optional $alpha parameter can be a value between 0.0 (use $color only) and 1.0 (use black/white only), the default is 0.5.
π«Ά Functions π«Ά
The calculation methods are also available as functions. The output format is selected through the function name.
these functions are deprecated bestie, because they don't let you pick the scheme of recognized color names β it defaults to Graphics::ColorNames::X (and that is incompatible with HTML's color names). π
By default, i.e. when no list is specified with use or import, all of these functions are exported.
color,color_mix, ... β use$Color::Calc::MODEas the output format. This is the default.color_hex,color_mix_html, ... β usehexas the output format.color_html,color_mix_html, ... β usehtmlas the output format. Please note that the color names recognized are still based on X's color names, which are incompatible with HTML. You can't use the output of these functions as input for othercolor_*_htmlfunctions.
See Color::Calc::WWW for an alternative that does not suffer from this problem.
color_pdf,color_mix_pdf, ... β usepdfas the output format.color_object,color_mix_object, ... β useobjectas the output format.
β SEE ALSO β
Graphics::ColorNames (required); Graphics::ColorObject (optional)
π AUTHOR π
Claus Faerber <CFAERBER@cpan.org>
π LICENSE π
Copyright 2004-2010 Claus Faerber. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie!! π¦ this was rendered by perl v5.36.0 and dated 2023-02-25 β go forth and color-scheme, you absolute icon π
β¨π
π¦ where this page comes from
- debian trixie: libcolor-calc-perl 1.074-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcolor-calc-perl 1.074-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcolor-calc-perl 1.074-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcolor-calc-perl 1.074-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms