Skip to main content

🌀 ANSI Converter

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

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]

Tests

To ensure the installation of this package has been successful, you can run the tests.

  1. Make sure you have installed the pytest module. Otherwise, run:
pip install pytest
  1. After you downloaded the code and installed the package, run the tests by executing:
python -m pytest test/

 💻 Usage

Converter module

⚠️ Warning
Some colour combinations between background and foreground are incompatible. The result can be slightly different from what is expected.

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

Result:

Result:

Convert from hexadecimal to ANSI
# How to print a yellow text on a navy blue background, with hexadecimal values.
from ansiconverter import HEXtoANSI
print(HEXtoANSI('Some yellow text on blue background','#fdf31f', '000080'))

Result:

Result:

ℹ️ Note
Another little tool has been added to convert RGB to hexadecimal and vice versa. It can't be used to write in color in the terminal but could be useful for other applications.

Convert from hexadecimal to RGB
from ansiconverter import HEXtoRGB
print(HEXtoRGB("#0b38c1"))

Result :

[11, 59, 193]
Convert from RGB to hexadecimal
from ansiconverter import RGBtoHEX
print(RGBtoHEX([11, 59, 193]))

Result :

"#0b3bc1"

🎨 Styles module

Several text styles are available as well. You can even combine them with colours

Style Method
bold bold
italic italic
fainted faint
underlined underline
bold & underlined bold_and_underline
strikethroughed strikethrough
reversed (colours inverted) reverse
from ansiconverter import bold

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

Replace .bold in the example with any method above to get the desired style.


Combination of colours and styles

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

from ansiconverter import HEXtoANSI, bold
print(bold(HEXtoANSI('A yellow text in bold','#f6cf6c')))

Result:

N.B: the order between the style and the text format isn't important and you can switch them.

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

will work the same as

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

Note

This structure of this repository is based on a talk by Mark Smith, which is available here, and its linked repository

License

This repository is licensed under the MIT license. See the LICENSE file for more information.

Metadata

Release files for ansiconverter 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ansiconverter 2.0.0
File Size Uploaded
ansiconverter-2.0.0.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ansiconverter 2.0.0
File Interpreter ABI Platform
ansiconverter-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / ansiconverter-2.0.0.tar.gz

Download URL ansiconverter-2.0.0.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
23566676eb5cdd81440844e1940ab5320ad8067fdbd4e00dab24d0401d616082
BLAKE2b-256 checksum
How to use checksums
45d6fceb3613c3250600ab2360ee85903c83cf04a93f4aba197d1de762044045
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / ansiconverter-2.0.0-py3-none-any.whl

Download URL ansiconverter-2.0.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ad9649a80ae129617ee03c3e5661eefc99df61d14819658c60092705583564a
BLAKE2b-256 checksum
How to use checksums
47efc2d15ce88c5d31734bd510e6dfd50083a94bd75888a50a25fc39bf2df4da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page