Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: 'rgb_palette' - Term::ANSIColor Helper

by kcott (Archbishop)
on Aug 16, 2022 at 05:50 UTC ( [id://11146155]=note: print w/replies, xml ) Need Help??


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

G'day roho,

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

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

— Ken

Replies are listed 'Best First'.
Re^3: 'rgb_palette' - Term::ANSIColor Helper -- errors on strawberry
by Discipulus (Canon) on Aug 16, 2022 at 09:32 UTC
    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.

      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

        terminfo knows what your terminal is capable of. The max_colors capability looks right ("maximum number of colors on screen").

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        It's down to the version of Term::ANSIColor.

        I had Strawberry 5.30.0.1's default Term::ANSIColor v4.06, and it gave me the same error Discipulus saw. I tried

        @echo =================== 8 v 16 perl -E "use Term::ANSIColor; say my $x=colored('red text', 'red on_wh +ite'); use Data::Dump; dd $x" perl -E "use Term::ANSIColor; say my $x=colored('red text', 'bright_re +d on_white'); use Data::Dump; dd $x" perl -E "use Term::ANSIColor; use Data::Dump; my $x = colored('black o +n white', 'black on_white'); say $x; dd $x" @echo =================== 256 vs 8 or 16 perl -E "use Term::ANSIColor; use Data::Dump; my $x = colored('black o +n white', 'rgb000 on_white'); say $x; dd $x" @echo =================== [id://11146167] perl -E "use Term::ANSIColor; use Data::Dump; my $x = colored('black o +n white', 'r0g0b0 on_white'); say $x; dd $x" perl -E "say qq(\e[38;5;16;47mblack on white\e[0m)" perl -E "say qq(\e[38;2;0;0;0;47mblack on white\e[0m)" perl -E "say qq(\e[38;2;255;0;0;47mred on white\e[0m)" perl -E "say qq(\e[38;2;0;127;0;47mgreen on white\e[0m)" perl -E "say qq(\e[38;2;0;0;255;47mblue on white\e[0m)" perl -E "say qq(\e[38;2;191;127;191;47mpurple on white\e[0m)" perl -E "use Term::ANSIColor; use Config; say for $Term::ANSIColor::VE +RSION, $], $Config{myuname};"
        ... And everything but the r0g0b0 worked†; the r0g0b0 gave Invalid attribute name r0g0b0 at -e line 1.

        I then realized that since kcott was using v5.36, there was probably a newer Term::ANSIColor , so then I ran cpanm Term::ANSIColor, which installed 5.01. When I ran my same batch script with the updated module, it worked without the error.

        So Discipulus, you need to update Term::ANSIColor. Per their change list, rNNNgNNNbNNN notation was added in 5.00.

        (To get the colors to work, I did use the VirtualTerminalLevel = 1 that the AM recommended)


        edit: †: with full 24 bit color working fine for the red, green, blue, and purple, using manual escape sequences.

      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

      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.

        ++ Thanks for the bug report.

        I found an existing bug report, #118255, which refers to a problem when -echo is used with prompt. It looks like -return, as I've used, has the same issue.

        I've changed

        $rgb = prompt 'Convert hex to decimal rgb (or just hit "Enter" to +quit): ', -return => '';

        to

        $rgb = prompt 'Convert hex to decimal rgb (or just hit "Enter" to +quit): ', -return => ''; # Fix for MSWin -- see https://rt.cpan.org/Public/Bug/Display.html +?id=118255 $rgb =~ s/\R\z//;

        The s/\R\z// is a little more generic than s/[\r]*$//; but otherwise it's pretty much the same as your fix.

        I've tested it on my machine (Cygwin) and it hasn't changed the functionality for me. Could you test that for me on your MSWin machine? Assuming it works OK, I'll update the code in the OP. Thanks.

        — Ken

        Enabling ANSI colors in older versions of Windows To use ANSI colours in the Windows terminal requires setting VirtualTe +rminalLevel. VirtualTerminalLevel = 1 is now set by default for the terminal and in + ConPTY. In Windows versions 1511 through to 1903 this had to be enabled in the + registry at: [HKEY_CURRENT_USER\Console] "VirtualTerminalLevel"=dword:00000001 Alternatively it can be enabled by calling the SetConsoleMode API with + the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag 24 bit color The latest W10 Console has full 24-bit color support, this is primaril +y for Linux compatibility so the console properties page and the defa +ult Windows color mappings still use just 16 colors, there is no plan + to add additional console colors to the Win32 API. To take advantage of the new colors use ColorTool.exe and install a th +eme, or use the VT100 sequences described here. When the VT100 sequences are used with a version of the Windows Consol +e that supports 24-bit colors they will display the desired RGB color +, older consoles will choose the nearest appropriate color from the e +xisting 16 color table.
        https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN#extended-colors

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found