Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^6: 'rgb_palette' - Term::ANSIColor Helper -- errors on strawberry

by Discipulus (Canon)
on Aug 18, 2022 at 07:51 UTC ( #11146213=note: print w/replies, xml ) Need Help??

in reply to Re^5: 'rgb_palette' - Term::ANSIColor Helper -- errors on strawberry
in thread 'rgb_palette' - Term::ANSIColor Helper


yes it works nice for me: the following version is also agnostic about the perl version:

use strict; use warnings; use feature qw (say signatures); no warnings qw(experimental::signatures); use IO::Prompter [ -style => 'bold blue', -echostyle => 'bold magenta', ]; use Term::ANSIColor 5.0; my @nums_under_255 = qw{0 26 51 77 102 127 153 179 204 230 243}; say ''; for my $r (@nums_under_255, 255) { for my $g (@nums_under_255, 255) { print ' '; for my $b (@nums_under_255) { print colored(text("r${r}g${g}b${b}"), join(' on_', fg($r, + $g, $b), "r${r}g${g}b${b}")); } say colored(text("r${r}g${g}b255"), join(' on_', fg($r, $g, 25 +5), "r${r}g${g}b255")); } } say ''; my $rgb; while (1) { $rgb = prompt 'Convert hex to decimal rgb (or just hit "Enter" to +quit): ', -return => ''; # Fix for MSWin -- see +?id=118255 $rgb =~ s/\R\z//; unless (length $rgb) { say ''; last; } if ($rgb =~ /^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/) +{ my $ansi_rgb = sprintf 'r%dg%db%d', map hex, $1, $2, $3; print colored(' --> [', 'bold white'); print colored(' ' x 8, "$ansi_rgb on_$ansi_rgb"); print colored('] --> ', 'bold white'); say colored($ansi_rgb, 'bold cyan'); } else { say colored("\nERROR: '", 'r255g0b0 on_r51g51b51'), colored($rgb, 'r255g255b0 on_r51g51b51'), colored( "' is invalid. Six hexadecimal characters are expected +; such as in the table above.", 'r255g0b0 on_r51g51b51' ); } } sub fg ($r, $g, $b) { #return $r + 2 * $g + $b > 204 ? 'black' : 'white'; return "r${r}g${g}b${b}"; } sub text ($str) { return sprintf ' %02x%02x%02x ', $str =~ /^r(\d+)g(\d+)b(\d+)$/; }

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re^6: 'rgb_palette' - Term::ANSIColor Helper -- errors on strawberry
  • Download Code

Replies are listed 'Best First'.
Re^7: 'rgb_palette' - Term::ANSIColor Helper -- errors on strawberry
by kcott (Archbishop) on Aug 18, 2022 at 10:53 UTC

    Firstly, I'm glad this finally worked for you.

    "the following version is also agnostic about the perl version"

    Leaving out a "use VERSION;" statement does not achieve this; nor does adding a "use feature LIST;" statement.

    Your code will have problems with older Perl versions. Here's a non-exhaustive list:

    • Subroutine signatures were introduced in v5.20.0 (see "perl5200delta: Experimental Subroutine signatures"). I added information in the OP on how to deal with this.
    • I didn't really expect anyone to wind back v5.36 to a pre-5.10 version, so I made no comment about that in the OP; however, say() was introduced in v5.10.0 (see "perl5100delta: say()").
    • And for the same reason as the above point, I didn't mention that IO::Prompter requires v5.10.0 (see the first line of its source).
    • And, in the later fix for the MSWin bug, \R requires v5.10.0 (see "perlrebackslash: \R").

    — Ken

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146213]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2023-09-21 22:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found