Keep in mind, this is sort of what's happening in the background. This function takes a three-digit hexadecimal number as the first argument. The first digit is the style, second digit is the bg color and the third digit is the text color. The bg color goes from 0 to 7, I think. Then the text color goes from 0 to F. cprintf(0x07F, 'Hello World') will print on grey background using white letters. Why do you insist using color names instead of numbers?
#
# Usage: cprintf(COLOR, TEXT, [ARGS])
#
sub cprintf
{
@_ > 1 or return;
my $E = shift;
my $A = ($E & 0xF00) >> 8; # Get font style
my $B = ($E & 0x0F0) >> 4; # Get background color
my $C = ($E & 0x00F); # Get text color
# Linux/OSX cprintf solution using ANSI codes:
$E = '2648375vnrptosqu'; # Color code translation table
$E = "\x1B[" . (vec($E, $C, 8) - 20) . "m\x1B[" . (vec($E, $B, 8) -
+10) . 'm';
if ($A & 1) { $E .= "\x1B[05m"; } # BLINKING
if ($A & 2) { $E .= "\x1B[04m"; } # UNDERLINE
if ($A & 4) { $E .= "\x1B[03m"; } # ITALIC
if ($A & 8) { $E .= "\x1B[01m"; } # BOLD
print $E; # Set color
printf(@_);
print "\x1B[0m"; # Reset color
return;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.