NAME

TUI::Drivers::Colors - color conversion functions for BIOS, XTerm and RGB colors

SYNOPSIS

use TUI::Drivers::Colors qw(
  BIOStoXTerm16
  RGBtoBIOS
  RGBtoXTerm16
  RGBtoXTerm256
  XTerm16toBIOS
  XTerm256toRGB
  XTerm256toXTerm16
);

my $bios    = RGBtoBIOS(0x7F00BB);
my $xterm16 = RGBtoXTerm16(0x7F00BB);
my $xterm256 = RGBtoXTerm256(0x7F00BB);

my $rgb = XTerm256toRGB(196);

DESCRIPTION

TUI::Drivers::Colors provides a collection of color conversion functions used by the TVision color system.

The functions convert between BIOS colors, XTerm 16-color values, XTerm 256-color values and 24-bit RGB colors.

This module is purely functional and does not define any objects.

RGB to XTerm16 Conversion

XTerm16 colors form a 4-bit RGBI palette and are not a regular RGB color space.

The conversion algorithm implemented by RGBtoXTerm16() follows the approach used by the "A modern port of Turbo Vision 2.0".

The RGB color is first transformed into an intermediate hue, chroma and lightness representation.

The algorithm then:

  • Determines whether the color should be represented as a grayscale value or as a chromatic color.

  • Chooses between normal and bright variants based on lightness.

  • Selects the final XTerm16 color from the hue value.

This approach provides a perceptually closer approximation than a simple nearest-color lookup while remaining efficient enough for real-time use.

FUNCTIONS

BIOStoXTerm16

my $xterm16 = BIOStoXTerm16($bios);

Converts a BIOS color value into the equivalent XTerm16 color index.

RGBtoBIOS

my $bios = RGBtoBIOS($rgb);

Converts a 24-bit RGB color into the closest BIOS color.

RGBtoXTerm16

my $xterm16 = RGBtoXTerm16($rgb);

Converts a 24-bit RGB color into the closest XTerm16 color.

RGBtoXTerm256

my $xterm256 = RGBtoXTerm256($rgb);

Converts a 24-bit RGB color into the closest XTerm256 color.

XTerm16toBIOS

my $bios = XTerm16toBIOS($xterm16);

Converts an XTerm16 color index into the equivalent BIOS color value.

XTerm256toRGB

my $rgb = XTerm256toRGB($xterm256);

Returns the RGB value corresponding to an XTerm256 palette entry.

For compatibility with the original TVision implementation this function is primarily intended for XTerm256 indices 16 through 255.

XTerm256toXTerm16

my $xterm16 = XTerm256toXTerm16($xterm256);

Approximates an XTerm256 color using the closest XTerm16 color.

SEE ALSO

TColor, TColorAttr

AUTHORS

  • magiblot <magiblot@hotmail.com> (original color conversion 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).