in reply to 'rgb_palette' - Term::ANSIColor Helper

Thanks Ken! This will be very useful. For those running Perl on MS Windows, I found I also need the following additional module to display colors:

use Win32::Console::ANSI;

"It's not how hard you work, it's how much you get done."

Replies are listed 'Best First'.
Re^2: 'rgb_palette' - Term::ANSIColor Helper
by kcott (Archbishop) on Aug 16, 2022 at 05:50 UTC

    G'day roho,

    Your welcome; I'm glad you'll find this useful.

    Thanks for adding the MSWin info — that's appreciated.

    — Ken

      Hello and thanks kcott,

      on strawberry 5.26 I have modified your program first lines as..

      use strict; use warnings; use feature qw (say signatures); no warnings qw(experimental::signatures); # use Win32::Console::ANSI; # nothing change if loaded or not

      but I still receive the error Invalid attribute name r0g0b0 at rgb_palette002.pl line 23. which is the line

      print colored(text("r${r}g${g}b${b}"), join(' on_', fg($r, $g, $b), "r${r}g${g}b${b}"));

      I also changed the TERM env var from dumb to not dumb (used to work) but still receiving the same error.

      L*

      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.

        I had logged off but, as I was shutting down for the evening, I thought of some additional tests you could do (based on the "Term::ANSIColor - Supported Colors" I linked in my last post).

        If these work, your terminal supports 256 colours:

        $ perl -E 'use Term::ANSIColor; use Data::Dump; my $x = colored("black + on white", "rgb000 on_white"); say $x; dd $x' black on white "\e[38;5;16;47mblack on white\e[0m" $ perl -E 'say "\e[38;5;16;47mblack on white\e[0m"' black on white

        If not, these should still work (indicating you have 8 or 16 colour support):

        $ perl -E 'use Term::ANSIColor; use Data::Dump; my $x = colored("black + on white", "black on_white"); say $x; dd $x' black on white "\e[30;47mblack on white\e[0m" $ perl -E 'say "\e[30;47mblack on white\e[0m"' black on white

        For me, all of those give black text on a white background.

        If you're down to determining whether you have 8 or 16 colour support, try these.

        $ perl -E 'use Term::ANSIColor; say colored("red text", "red on_white" +)' red text $ perl -E 'use Term::ANSIColor; say colored("red text", "bright_red on +_white")' red text

        If the second of those looks brighter, you have 16 colour support. If they look the same, it's 8 colour support.

        You might consider putting some, or all, of those in a script. That might help when trying various TERM settings (although, be aware that, without the ability to test on MSWin, I am somewhat guessing).

        — Ken

        G'day Discipulus,

        Unfortunately, it looks like you don't have a true color terminal. See "Term::ANSIColor - Supported Colors" for more about that.

        I got similar errors to what you're seeing when making a typo while developing and testing. This sort of thing:

        $ perl -E 'use Term::ANSIColor; say colored("black on white", "r0g0v0 +on_white")' Invalid attribute name r0g0v0 at -e line 1.

        However, there's no typo that I can see in what you've posted. You could do a couple of tests outside of my script.

        $ perl -E 'use Term::ANSIColor; use Data::Dump; my $x = colored("black + on white", "r0g0b0 on_white"); say $x; dd $x' black on white "\e[38;2;0;0;0;47mblack on white\e[0m" $ perl -E 'say "\e[38;2;0;0;0;47mblack on white\e[0m"' black on white

        Both of those give me the text "black on white" in black on a white background.

        I don't have a Perl on an MSWin platform to test. I don't know about dumb and not dumb TERM values. For what it's worth:

        $ echo $TERM xterm

        Sorry I can't be of more help. Is there anything in AM's post that's useful for you?

        Edit (stupid typo fix): s/white foreground/white background/.

        — Ken

        Two further notes on win32:

        1. Anecdotally, I recommend you don't use Win32::Console::ANSI; if you've got the VirtualTerminalLevel = 1 registry setting. It reduced the functionality of the colors when I enabled that line.
        2. IO::Prompter's prompt() command doesn't seem to handle win32 CRLF newlines correctly: when I hit ENTER like it told me to, it said "' is invalid. Six hexadecimal characters are expected; such as in the table above.". Then when I typed a 6 digit string like 123456 , it said the same thing. I added in some debug statements, and found out the CR was still there. I added $rgb =~ s/[\r]*$//; after $rgb = prompt ... and that fixed the interface for me.