How to convert colours from RGB to hue values

To convert colours to and from different colour spaces in Perl, use the Convert::Color module. It allows conversion to and from RGB and HSL.

#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use Convert::Color;
my %rgbs = (
    black => {r => 0, g => 0, b => 0},
    white => {r => 255, g => 255, b => 255},
    chartreuse => {r => 127, g => 255, b => 0},
);
for my $color (sort keys %rgbs) {
    my $r = $rgbs{$color}{r} / 255;
    my $g = $rgbs{$color}{g} / 255;
    my $b = $rgbs{$color}{b} / 255;
    my $rgb = "rgb:$r,$g,$b";
    print "$color $rgb\n";
    my $cc = Convert::Color->new ($rgb);
    my ($hue, $saturation, $lightness) = $cc->as_hsl->hsl ();
    printf "%.3g %.3g %.3g\n", $hue, $saturation, $lightness;
}

(download)

This outputs something like

black rgb:0,0,0
0 0 0
chartreuse rgb:0.498039215686275,1,0
90.1 1 0.5
white rgb:1,1,1
0 0 1


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer