Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 8-bit colors #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

troya2
Copy link

@troya2 troya2 commented Nov 24, 2024

Add Support for 8-bit ANSI Colors

Overview

This pull request enhances the existing parser to support 8-bit ANSI colors, extending its functionality beyond the previously supported basic colors. With this update, the parser now handles the full 256-color palette, enabling applications to render both basic (0-15) and 8-bit extended (16-255) ANSI color codes.


Key Features

  1. Extended Foreground and Background Colors:

    • Added support for foreground (38;5;<n>) and background (48;5;<n>) 8-bit ANSI color codes.
    • These include:
      • Basic Colors (0-15): Black, Red, Green, Yellow, Blue, Magenta, Cyan, White (standard and bright variants).
      • 6x6x6 Color Cube (16-231): A palette of RGB colors.
      • Grayscale Colors (232-255): Shades of gray.
  2. Mixed Color Types:

    • Supports sequences with both basic and extended colors for foreground and background.
    • Allows colors to appear in any order (e.g., \x1B[38;5;196;48;5;46m or \x1B[48;5;46;38;5;196m).
  3. Backwards Compatibility:

    • The parser retains full support for basic ANSI colors (30-37 for foreground, 40-47 for background, and their bright equivalents 90-97 and 100-107).
  4. Reset Handling:

    • Properly resets colors when encountering \x1B[0m.

Technical Changes

  • Updated the AnsiParser:
    • Introduced extendedFgColor and extendedBgColor parsers to handle 8-bit ANSI codes.
    • Enhanced the color parser to handle sequences with mixed foreground and background color types.
    • Ensured order independence for foreground and background color codes.
  • Enhanced the AnsiColor class:
    • Added isFgExtended and isBgExtended flags to distinguish between basic and extended colors.
  • Updated tests:
    • Added comprehensive test cases to verify support for basic colors, extended colors, mixed types, and reset sequences.

Examples

Input: \x1B[38;5;196;48;5;46mHello, World!\x1B[0m
Output: Renders "Hello, World!" with a bright red foreground and green background.

Input: \x1B[31;48;5;46mError\x1B[0m
Output: Renders "Error" with a red foreground and green background.


Impact

This update significantly improves the versatility of the parser by enabling support for richer color schemes. Applications using the parser can now accurately render text with complex ANSI color codes commonly used in modern terminal outputs.


How to Test

  1. Run the included test cases to verify parsing behavior for:
    • Basic colors.
    • Extended 8-bit colors.
    • Mixed sequences.
    • Reset handling.
  2. Test the parser with real-world ANSI sequences from terminal logs or colored output.

Additional Notes

This enhancement aligns the parser with the ANSI 8-bit color standard, ensuring compatibility with tools and libraries that generate extended color codes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant