NAME

TColorAttr - color attribute value type for screen cells

SYNOPSIS

use TUI::Drivers;

my $attr = TColorAttr->new(
  bios => 0x3D,
);

my $bios = $attr->asBIOS;

DESCRIPTION

TUI::Drivers::ColorAttr provides TColorAttr, a value type that represents the color attributes of a screen cell.

A TColorAttr stores foreground color, background color, and style information. It can also represent traditional BIOS color attributes used by Turbo Vision color handling.

CONSTRUCTOR

new

Creates a color attribute value.

With no arguments, the value uses default colors and no style flags:

my $attr = TColorAttr->new();

With a BIOS color attribute:

my $attr = TColorAttr->new(
  bios => 0x3D,
);

With explicit foreground, background, and optional style information:

my $attr = TColorAttr->new(
  fg    => TColor->new( rgb => 0x892312 ),
  bg    => TColor->new( rgb => 0x7F00BB ),
  style => slBold | slItalic,
);

The fg and bg arguments must be TColor values.

Convenience syntax

As a shorthand, fg and bg also accept a hash reference or an array reference instead of a TColor object:

[] or {}

The terminal default color.

{ ... }

Any hash reference is forwarded as-is to TColor->new, e.g. { bios => 0xF }, { rgb => 0x7F00BB } or { xterm => 196 }.

['\x7'], ['\x07'], ['\x{07}']

A BIOS color (0-15) from a hexadecimal escape. The bare form reads 1 or 2 hex digits; the braced form \x{...} accepts any number of digits.

['\007'], ['\o{007}']

A BIOS color (0-15) from an octal escape. The bare form reads 1 to 3 octal digits; the braced form \o{...} accepts any number of digits.

['0b0111']

A BIOS color (0-15) from a 4-digit binary literal.

[196]

An xterm-256color palette index (0-255).

['#7fbb00']

An RGB color in #RRGGBB notation, one byte per channel.

['#7fb']

The #RGB shorthand of the above; each hex digit is doubled ('#7fb' is equivalent to '#77ffbb').

[127, 0, 187]

An RGB color as a 3-element array of (r, g, b) byte values.

["\x7"]

A single already-resolved byte, taken directly as a BIOS color via its ordinal value. This is what a double-quoted escape like "\x7" becomes (see note below). Printable digits ('0'..'9') are excluded, since those are already handled above as xterm indices.

Note: the escape forms (\xH, \x{H..}, \OOO, \o{O..}) are plain text and are meant to be written with single quotes, e.g. ['\x7']. In double-quoted strings Perl resolves the escape itself before new ever sees it, turning "\x7" into a single control character instead of the 4-character text \x7. Both spellings still produce the same BIOS color 0x7, but only because of the single-byte abbreviation above; for any other purpose the two are not interchangeable.

For example:

my $attr = TColorAttr->new(
  fg => ['\x7'],
  bg => { rgb => 0x7F00BB },
);

This is convenient for compact palette definitions.

reverseAttribute

my $attr = $self->reverseAttribute();

Returns a new attribute with the visual foreground and background colors swapped.

The slReverse style attribute is interpreted differently by different terminal implementations. Therefore, explicit foreground and background colors are swapped whenever possible.

If either color is the terminal default color, the color values are left unchanged and the slReverse style flag is toggled instead.

Returns a new TColorAttr.

METHODS

asBIOS

my $attr = $self->asBIOS();

Returns a BIOS color attribute equivalent to the current value.

The result is meaningful only when isBIOS returns true.

getBack

my $bg = $self->getBack();

Returns the background color component.

getFore

my $fg = $self->getFore();

Returns the foreground color component.

getStyle

my sytle = $self->getStyle();

Returns the style flags component.

isBIOS

my $bool = $self->isBIOS();

Returns true if this value is represented as a BIOS color attribute.

lshift

my $result = $self->lshift($shift);

Shifts the asBIOS value left by $shift bits.

As a special case, shifting by 8 returns a new TAttrPair whose high attribute is this value and whose low attribute is a BIOS attribute of 0, for compatibility with legacy code that used << 8 on an attribute to construct an attribute pair.

setFore

$self->setFore($color);

Sets the foreground color component.

The argument must be a TColor value.

setBack

$self->setBack($color);

Sets the background color component.

The argument must be a TColor value.

setStyle

$self->setStyle($style);

Sets the style flags component.

Only the style bits defined by the TColorAttr representation are stored.

toBIOS

my $attr = $self->toBIOS();

Returns a BIOS color attribute for this value.

OPERATORS

Numeric equality

$a == $b

Returns true when TColorAttr values contain identical data; support typecast to a BIOS value if one is a number.

Numeric conversion

my $bios = 0+ $a;

Returns the BIOS color attribute equivalent to this value.

Left shift

my $result = $a << $shift;

Calls "lshift".

SEE ALSO

TUI::Drivers::Const, TColor, TAttrPair, TScreenCell

AUTHORS

  • magiblot <magiblot@hotmail.com> (original color attribute design)

  • J. Schneider <brickpool@cpan.org> (Perl implementation and maintenance)

COPYRIGHT AND LICENSE

Copyright (c) 2019-2026 the "AUTHORS" listed above.

This software is licensed under the MIT license (see the LICENSE file, which is part of the distribution).