Skip to main content

Convert any color in RGB or hexadecimal format to ANSI code

Project description

ANSI Converter

PyPI PyPI - License Code style: black PyPI - Python Version PyPI - Status

Convert any colour to the ANSI format to write in colours in your terminal.

Installation

Run this command to install ansiconverter:

python -m pip install -U ansiconverter

To install for development:

git clone https://github.com/thomassamoth/ansiconverter.git
cd ansiconverter
pip install -e .[dev]

 :computer: Usage

:arrow_right: Converter module

Convert any RGB colour to ANSI

# How to print a green text on a white background
from ansiconverter.converter import RGBtoANSI
print(RGBtoANSI(text='Green text on a white background',foregound=[0, 255, 0], background=[255, 255, 255]))

Result:

Convert any hexadecimal colour to ANSI

Warning
Some colour combinations are incompatible, and the result will be slightly different from what is expected.

# How to print a yellow text on a navy blue background, with hexadecimal values.
from ansiconverter.converter import HEXtoANSI

print(HEXtoANSI('Some yellow text on blue background','#fdf31f', '000080'))

Result:

:art: Styles module

Write your text in different styles:

  • bold
  • italic
  • faint
  • underlined
  • bold & underlined
  • strikethrough
  • reversed
from ansiconverter.styles import styles

print(styles.bold("Some text in bold"))

Combination of colours and styles

It is possible to combine text styles with colours by doing so:

from ansiconverter.converter import *
from ansiconverter.styles import styles

print(styles.bold(HEXtoANSI('A yellow text in bold','#f6cf6c')))

Result:

You can replace styles.bold() by any function mentionned above i.e. styles.italic(). You can even combine different styles!

:heavy_plus_sign: Additional features

You can also use RGB to HEX converter or HEX to RGB by themselves like this:

>>> from ansiconverter import converter

>>> print(converter.HEXtoRGB("#0b38c1"))
[11, 59, 193])

>>> print(converter.RGBtoHEX([11, 59, 193]))
"#0b3bc1"

Note
This repository is based on Mark Smith's talk available here and its linked repository

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansiconverter-1.0.0.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

ansiconverter-1.0.0-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page