Conver...or::RGB(3pm)

πŸ’– Convert::Color::RGB β€” the pink one (3pm) πŸ’–

okay bestie so this is a User Contributed Perl Documentation page, and it's giving "my color is a color" realness πŸ’… This is Convert::Color::RGB, no cap, and it's literally just a color value represented as red/green/blue πŸŽ€


✨ NAME ✨

"Convert::Color::RGB" β€” a color value represented as red/green/blue πŸ¦‹


🌈 SYNOPSIS 🌈

Directly, like, the most straightforward way:

use Convert::Color::RGB;

my $red = Convert::Color::RGB->new( 1, 0, 0 );

# Can also parse strings
my $pink = Convert::Color::RGB->new( '1,0.7,0.7' );

Or via Convert::Color, if you wanna be, you know, indirect about it:

use Convert::Color;

my $cyan = Convert::Color->new( 'rgb:0,1,1' );

πŸŽ€ DESCRIPTION πŸŽ€

So the objects in this class represent a color in RGB space, and it's literally just a set of three floating-point values in the range 0 to 1. That's it, that's the whole vibe 🌸

Now β€” for representations using 8- or 16-bit integers, you're gonna wanna go see Convert::Color::RGB8 and Convert::Color::RGB16 instead. Different girls, different lore πŸ’œ


🌟 CONSTRUCTOR 🌟

πŸ’… new

$color = Convert::Color::RGB->new( $red, $green, $blue );

Returns a new object representing the set of values you gave it 🫢 These values should be floating-point numbers between 0 and 1. And if you go outside that range? ⚠️ Not a vibe β€” the values will be clamped.

bestie, clamping means it gets squished back into range. it does NOT error out. it just quietly fixes your nonsense πŸ’€

There's also this form:

$color = Convert::Color::RGB->new( $string );

This one parses $string for values and makes a new object just like the three-argument form above 😌 The string needs to be in the form:

red,green,blue

containing the three floating-point values in decimal notation. periodt ✨


πŸ¦‹ METHODS πŸ¦‹

🌸 red

$r = $color->red;

🌸 green

$g = $color->green;

🌸 blue

$b = $color->blue;

Accessors for the three components of the color πŸ‘‘ one for each, as the universe intended.

✨ rgb

( $red, $green, $blue ) = $color->rgb;

Returns the individual red, green and blue color components of the color value πŸ’– all three at once, in a cute lil list.

πŸ’œ alpha_blend

$mix = $color->alpha_blend( $other, [ $alpha ] );

Return a new color which is a blended combination of the two passed into it 🎨 The optional $alpha parameter defines the mix ratio between the two colors, and it defaults to 0.5 if not defined. Values closer to 0 will blend more of $color, and closer to 1 will blend more of $other 🫢 literally the slay button for gradients, iykyk.

πŸŽ€ dst_rgb

$measure = $color->dst_rgb( $other );

Return a measure of the distance between the two colors 🌈 This is the unweighted Euclidean distance of the three color components. Two identical colors will have a measure of 0, pure black and pure white have a distance of 1, and all others will lie somewhere inbetween πŸ’«

🧚 dst_rgb_cheap

$measure = $color->dst_rgb_cheap( $other );

Return a measure of the distance between the two colors πŸ’… This one is the sum of the squares of the differences of each of the color components. It's part of the value used to calculate "dst_rgb", but since it involves no square root it will be cheaper to calculate β€” so use it in cases where only the relative values matter, like when you're picking the "best match" out of a set of colors πŸ‘‘

It ranges between 0 for identical colours and 3 for the distance between pure black and pure white 😌 no square roots, no stress, we love a budget queen.


πŸ’– EXAMPLES πŸ’–

🌈 Generating Gradients

The "alpha_blend" method can be used to generate a smooth gradient between two colours 🌸 iconic behaviour, honestly:

use Convert::Color;

my $blue = Convert::Color->new("vga:blue");
my $cyan = Convert::Color->new("vga:cyan");

say $blue->alpha_blend( $cyan, $_/10 )->as_rgb8->hex for 0 .. 10

πŸ‘€ SEE ALSO πŸ‘€

  • Convert::Color β€” color space conversions πŸ’«
  • Convert::Color::HSV β€” a color value represented as hue/saturation/value 🌈
  • Convert::Color::HSL β€” a color value represented as hue/saturation/lightness 🌸

🫢 AUTHOR 🫢

Paul Evans <leonerd@leonerd.org.uk> β€” thank you king πŸ’–


so yeah, that's the whole page bestie, and lemme tell you the receipts: this was perl v5.40.0, dated 2024-09-16, and it lives in the Conver...or::RGB(3pm) section of your friendly neighborhood manual 🌟 go forth and blend ur colors, you got this βœ¨πŸ’…πŸŽ€

πŸ“¦ where this page comes from