Skip to main content

chroma-term-console

PyPI version Python License Documentation Status

轻量级 Python ANSI 终端色彩库,支持 4/8/256 色、24 位真 RGB 色彩与文本样式,用于控制台文本美化。

安装

pip install chroma-term-console

快速开始

Chroma 类(推荐)

最直观的方式,直接 Chroma.颜色名('文本') 即可看到彩色输出:

from chroma_term_console import Chroma

# 前景色
print(Chroma.red('错误'))
print(Chroma.green('成功'))
print(Chroma.yellow('警告'))
print(Chroma.blue('信息'))
print(Chroma.magenta('紫红色'))
print(Chroma.cyan('青蓝色'))

# 高亮前景色
print(Chroma.bright_red('高亮红'))
print(Chroma.bright_green('高亮绿'))

# 背景色
print(Chroma.bg_red('红色背景'))
print(Chroma.bg_green('绿色背景'))
print(Chroma.bg_yellow('黄色背景'))

# 显示方式
print(Chroma.bold('粗体'))
print(Chroma.underline('下划线'))
print(Chroma.blink('闪烁'))
print(Chroma.reverse('反显'))

# 组合样式
print(Chroma.bold_red('高亮红色'))
print(Chroma.bold_green('高亮绿色'))
print(Chroma.underline_cyan('下划线青蓝'))

# 256 色
print(Chroma.c256('橙色', fg=208))
print(Chroma.c256('粉色', fg=213))

# 24 位真彩 RGB
print(Chroma.rgb('自定义粉', fg=(255, 121, 198)))
print(Chroma.rgb('暗底亮字', fg=(255, 255, 255), bg=(40, 42, 54)))

快捷函数

from chroma_term_console import fg, bg, style, bold, underline, italic

# 前景色 / 背景色
print(fg('red', '红色文字'))
print(bg('blue', '蓝色背景'))

# 文本样式
print(bold('粗体'))
print(underline('下划线'))
print(italic('斜体'))

# 多样式组合
print(style('重要通知', fg='yellow', bold=True, underline=True))

color 函数(原始 ANSI 码封装)

直接对应 \033[显示方式;前景色;背景色m***\033[0m 语法:

from chroma_term_console import color, color256, color_rgb

# 基本 4/8 色
print(color('红色文字', fg=31))
print(color('高亮绿色', fg=32, mode=1))
print(color('红字黄底', fg=31, bg=43))
print(color('高亮蓝字白底', fg=34, bg=47, mode=1))

# 256 色
print(color256('橙色', fg=208))

# 24 位真彩 RGB
print(color_rgb('粉色', fg=(255, 121, 198)))

链式调用(Styled 类)

from chroma_term_console import Styled

s = Styled()
print(s.fg('green').bold('成功'))
print(s.fg('#ff79c6').bg('#282a36').italic('Dracula 主题'))
print(s.fg(196).bold('256色红'))
print(s.fg((255, 128, 0)).underline('RGB 橙色'))

API 参考

Chroma 类方法

方法 效果
Chroma.black/red/green/yellow/blue/magenta/cyan/white 基本 8 色前景
Chroma.bright_red/... 高亮 8 色前景
Chroma.bg_red/... 背景色
Chroma.bold/underline/blink/reverse/hidden 显示方式
Chroma.bold_red/... 高亮 + 颜色组合
Chroma.underline_red/... 下划线 + 颜色组合
Chroma.c256(text, fg=N, bg=N) 256 色
Chroma.rgb(text, fg=(R,G,B), bg=(R,G,B)) 24 位真彩

颜色码对照表

前景色 背景色 颜色
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫红色
36 46 青蓝色
37 47 白色
90-97 100-107 高亮色

显示方式码

效果
0 默认值
1 高亮/粗体
4 下划线
5 闪烁
7 反显
8 不可见

工具函数

from chroma_term_console import strip_ansi, supports_color, detect_color_level

# 移除 ANSI 转义序列
plain = strip_ansi('\033[31m红色\033[0m')  # '红色'

# 检测终端色彩支持
level = detect_color_level()  # ColorLevel.NONE / BASIC / EXTENDED / TRUECOLOR
has_color = supports_color()  # True / False

终端兼容性

  • 自动检测终端色彩能力(真彩 / 256色 / 基本色)
  • 支持 NO_COLOR 协议(设置 NO_COLOR 环境变量禁用颜色)
  • 支持 FORCE_COLOR 环境变量强制启用
  • Windows 10+ / Windows Terminal / ConEmu / ANSICON 自动启用 VT 处理
  • 非 TTY 环境(管道/重定向)自动禁用颜色

运行演示

python test.py

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

chroma_term_console-0.1.0-cp314-cp314-win_amd64.whl (153.7 kB view details)

Uploaded CPython 3.14Windows x86-64

chroma_term_console-0.1.0-cp314-cp314-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.0-cp313-cp313-win_amd64.whl (149.2 kB view details)

Uploaded CPython 3.13Windows x86-64

chroma_term_console-0.1.0-cp313-cp313-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.0-cp312-cp312-win_amd64.whl (151.7 kB view details)

Uploaded CPython 3.12Windows x86-64

chroma_term_console-0.1.0-cp312-cp312-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.0-cp311-cp311-win_amd64.whl (154.7 kB view details)

Uploaded CPython 3.11Windows x86-64

chroma_term_console-0.1.0-cp311-cp311-manylinux_2_17_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.0-cp310-cp310-win_amd64.whl (154.2 kB view details)

Uploaded CPython 3.10Windows x86-64

chroma_term_console-0.1.0-cp310-cp310-manylinux_2_17_x86_64.whl (960.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.0-cp39-cp39-win_amd64.whl (154.6 kB view details)

Uploaded CPython 3.9Windows x86-64

chroma_term_console-0.1.0-cp39-cp39-manylinux_2_17_x86_64.whl (951.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.0-cp38-cp38-win_amd64.whl (158.7 kB view details)

Uploaded CPython 3.8Windows x86-64

chroma_term_console-0.1.0-cp38-cp38-manylinux_2_17_x86_64.whl (970.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file chroma_term_console-0.1.0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a18bde2f5abdc3fe86ba193e34d0a569ca6bc6d5505b8e444b80c4bfb5f06d17
MD5 e089dc31d0d6d5c58250acb149676b39
BLAKE2b-256 b07038294c95da411916a18423fb54fe9e613a51e575a8bfd825deb502babec7

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp314-cp314-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7f4312ab7c95e2615eee32e2a2c2513641bf85db392a1985d704511ac8a54991
MD5 4c28831fce518bdf1164b1da1a33484d
BLAKE2b-256 75100825d9c8077ac19e2f20d46704dc51a46532e7438bcf5f2cf3cf557fcf6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp314-cp314-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 12cf991c2d0aa364fface67ca0672119e5e54ae87ed29e764cb77cfc3bd7989e
MD5 5ded1acb108514b19fa5f3b28ff59fa3
BLAKE2b-256 81b2f5c51d94b1532ecc6d72045ca82d2134c682a01e443aeeae09c08e4ce4ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp313-cp313-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3934331abd67a469cd4945b0512615b96196fb3e2b790b8659c5c34d98497442
MD5 ed562e66a892f473be2c3f176a8abe1b
BLAKE2b-256 c84c563a6aaa8ab8ff79dcab41a6f20eaa8b96aac4ce753cf633909e418765f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp313-cp313-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4d1b2b1bf6389f41aa6963992ef1d377d8101987dd366bca51750c64654cd2cb
MD5 65fef7d40a54b0377a6d18b446f3de93
BLAKE2b-256 26e1fb124a51e4a8fa19c800d680df24a1a017ae6b4cbe3c1e10adf6c84c0c52

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp312-cp312-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 11a57f830eda2cbe4382ec8f549f4f1d1c3ee78858cd19eaf73a173c311e7a8d
MD5 686280d5c261dba9caf8eeceea9703b6
BLAKE2b-256 4a9539d5d918bf6c3aec1fd6eae040c3b1919d19948ee4fd9b513de815effd68

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp312-cp312-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4bc4af64205b1a318fd165a9c7cce705d8d15cd1d32a6712d60e8a454b94642c
MD5 107d61561cdd8d7a840959cf957d4b1a
BLAKE2b-256 78235d44623c58cda54a353bdf34f73fe3e8f6d7a8bc7563670da95dc0ce4a07

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp311-cp311-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c9fe6091eef41c8d1ee944834c19cf1b36f788c03eb23229af6a94c187e6d865
MD5 9130a26532d4f52a212e227fda76f42d
BLAKE2b-256 6a7372b3898f5c8e35b7f80ad76dd338ea1c4cdd535283a3f81c356f34b4d178

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp311-cp311-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b51c5e14f4e39370c27d85b1014214477134b520a6b418c5c3045db7b8824a7d
MD5 89e328d6573c20d035ff5bf8cb099243
BLAKE2b-256 f1329175d0b1aa832d57d98978aa485c55bc12d01be23ef58e6445281c75df2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp310-cp310-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 6207f5223682500230302459701cac200fc0ff954d009a174713d8acfe4f49c0
MD5 25cf36118c4045519ced19ecebff17e0
BLAKE2b-256 3b3e9e0ae2c1701d0eda546e99c35b4051cc2ec492f6ee53fb38c852d035d31f

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp310-cp310-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5dbcc4381f6b442681bb2b708a51e38d9b40940205f46009aa8023610ff5f86b
MD5 b314627a6875a4f5c9e3b7f3dccee5c0
BLAKE2b-256 0f0c004cba29455e4b2b0dc91899bb8af96de4d76e05bdf0ecbc57bcf12a5638

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp39-cp39-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 becd7ccb18b63f8a40d6b93e115f017a80d1860d045f17ee434a68bd68b65497
MD5 0461f8c445e5f4ae54234095ddd204d8
BLAKE2b-256 5c8c3939476b17cebc7fd28ee0dd3dbc548a93be1494633d80b353942f137f6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp39-cp39-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 29973fdb928636c3e376e4553aae6242dd4fb7365fe56187abc6caf03567b6e8
MD5 7a2d6ed5bbde68234a475260796679d2
BLAKE2b-256 d24f7a6b0a5f6a6de8feb50708c15edb470dd41844256d10cfdc27693ebc023c

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp38-cp38-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chroma_term_console-0.1.0-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.0-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 eb300be018692ef17206dbbb04f88ead9075e2a4cbc229bcbf7cc01a00410365
MD5 d1130b4aa2d628292bc64c5e102a3b21
BLAKE2b-256 81ea44848681acea513096b31be930c97f4a4755c4ab361ae82dc4ebeb8c7933

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.0-cp38-cp38-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/chroma-term-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.1.4

14 files

0.1.3

14 files

0.1.2

14 files

0.1.1

14 files

This release

0.1.0 This release

14 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page