Conver...::RGB16(3pm)

✨ Convert::Color::RGB16 β€” a color value represented as red/green/blue in 16-bit integers ✨

okay bestie, so this is Convert::Color::RGB16, a lil Perl module (from the Convert::Color family, 3pm energy) that lets you hold a color as three chunky 16-bit integers. red, green, blue, but make it πŸ’₯bigπŸ’₯. let's get into it 🫢


🌈 SYNOPSIS 🌈

Directly, like:

use Convert::Color::RGB16;

my $red = Convert::Color::RGB16->new( 65535, 0, 0 );

# Can also parse strings
my $pink = Convert::Color::RGB16->new( '65535,49152,49152' );

# or
$pink = Convert::Color::RGB16->new( 'ffffc000c000' );

Or via Convert::Color, if you're feeling fancy:

use Convert::Color;

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

πŸ’– DESCRIPTION πŸ’–

so. objects in this class represent a color in RGB space as a set of three integer values, each one in the range 0 to 65535 β€” that's 16 bits each, hence the name, no cap.

if you want representations using floating point values instead, go see Convert::Color::RGB. if you want 8-bit integers, go see Convert::Color::RGB8. this one is the 16-bit girlie πŸ’…


πŸŽ€ CONSTRUCTOR πŸŽ€

new

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

Returns a new object representing the set of values you gave it. those values should be integers between 0 and 65535 β€” and if you hand it anything outside that range, it gets clamped. it's giving "we'll take what we can get" 😌

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

This one parses $string for values and builds a new object, same vibe as the three-argument form above. the string should be in the form

red,green,blue

with those three integers in decimal notation. but she can ALSO accept a hex encoded string β€” like the kind you'd get back from the rgb16_hex method:

rrrrggggbbbb

so many ways to serve, honestly πŸ’…βœ¨


πŸ¦‹ METHODS πŸ¦‹

red

$r = $color->red;

green

$g = $color->green;

blue

$b = $color->blue;

Accessors for the three components of the color. each one just hands you its piece of the vibe. πŸ“

rgb16

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

Returns the individual red, green and blue color components of the color value, expressed in RGB16 space. all three at once, list-style, iconic.

hex

$str = $color->hex;

Returns a string representation of the color components in RGB16 space, in a handy little "RRRRGGGGBBBB" hex string. yes, four hex digits per channel, that's the 16-bit lifestyle bestie.

alpha_blend

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

Returns a new color that's a blended combination of the two colors you passed in. the optional $alpha parameter sets the mix ratio between them, and it defaults to 0.5 if you don't define it. values closer to 0 blend more of $color, and values closer to 1 blend more of $other. choose your fighter 😭

alpha16_blend

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

Similar to alpha_blend (see above bestie, it's right there) but this one works with integer arithmetic. so $alpha should be an integer in the range 0 to 65535. math but make it whole numbers 🧚

dst_rgb16

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

Returns a measure of the distance between the two colors. this is the unweighted Euclidean distance of the three color components. two identical colors get a measure of 0, pure black and pure white have a distance of 1, and everybody else lands somewhere inbetween. it's a spectrum, literally πŸ’€

dst_rgb16_cheap

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

Returns a measure of the distance between the two colors β€” but 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_rgb16, but since it involves no square root it's cheaper to compute. perfect for when 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*(65535^2) for the distance between pure black and pure white. budget-friendly and still correct, we love to see it 🫢


πŸ‘€ SEE ALSO πŸ‘€

  • Convert::Color β€” color space conversions

πŸ‘‘ AUTHOR πŸ‘‘

Paul Evans <leonerd@leonerd.org.uk>


okay that's the whole thing bestie, you're officially fluent in 16-bit RGB πŸ’…

little sign-off moment: this was perl, v5.40.0, dated 2024-09-16, from the User Contributed Perl Documentation, page Conver...::RGB16(3pm). go forth and render colors, mwah πŸ’–βœ¨
πŸ“¦ where this page comes from