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)))

链式调用(Chroma 语法糖)

Chroma.样式名('文本') 返回 ChromaText 对象,可直接链式叠加任意颜色与样式,无需嵌套:

from chroma_term_console import Chroma

# 样式 + 颜色
print(Chroma.bold('粗体').red())           # 粗体 + 红色
print(Chroma.underline('下划线').bg_blue()) # 下划线 + 蓝色背景

# 多重叠加
print(Chroma.bold('警告').red().blink())    # 粗体 + 红色 + 闪烁
print(Chroma.italic('提示').cyan().dim())   # 斜体 + 青色 + 暗色

# 链式 + 256 色 / RGB
print(Chroma.bold('橙色').c256(fg=208))
print(Chroma.underline('自定义').rgb(fg=(255, 128, 0)))

# 兼容 print / f-string / 字符串拼接
msg = Chroma.bold('错误').red()
print(f"结果: {msg}")
print("前缀 " + msg + " 后缀")

# 获取原始文本
print(Chroma.bold('hello').red().text)  # 'hello'

ChromaText 实现了 __str__ / __format__ / __repr__,可直接用于 print()、f-string 和字符串拼接,无需手动转换。

自动输出:当表达式结果未被赋值时,ChromaText 对象在语句结束后自动打印到终端,无需 print()

# 无需 print(),直接自动输出
Chroma.bold('粗体').red()
Chroma.italic('提示').cyan().dim()

# 赋值给变量则不自动输出,需手动 print()
msg = Chroma.bold('粗体').red()
print(msg)

链式调用(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 显示方式(返回 ChromaText,可链式调用)
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 环境(管道/重定向)自动禁用颜色

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.2-cp314-cp314-win_amd64.whl (184.2 kB view details)

Uploaded CPython 3.14Windows x86-64

chroma_term_console-0.1.2-cp314-cp314-manylinux_2_17_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.2-cp313-cp313-win_amd64.whl (179.0 kB view details)

Uploaded CPython 3.13Windows x86-64

chroma_term_console-0.1.2-cp313-cp313-manylinux_2_17_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.2-cp312-cp312-win_amd64.whl (181.2 kB view details)

Uploaded CPython 3.12Windows x86-64

chroma_term_console-0.1.2-cp312-cp312-manylinux_2_17_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.2-cp311-cp311-win_amd64.whl (189.1 kB view details)

Uploaded CPython 3.11Windows x86-64

chroma_term_console-0.1.2-cp311-cp311-manylinux_2_17_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.2-cp310-cp310-win_amd64.whl (188.4 kB view details)

Uploaded CPython 3.10Windows x86-64

chroma_term_console-0.1.2-cp310-cp310-manylinux_2_17_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.2-cp39-cp39-win_amd64.whl (188.7 kB view details)

Uploaded CPython 3.9Windows x86-64

chroma_term_console-0.1.2-cp39-cp39-manylinux_2_17_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.2-cp38-cp38-win_amd64.whl (193.4 kB view details)

Uploaded CPython 3.8Windows x86-64

chroma_term_console-0.1.2-cp38-cp38-manylinux_2_17_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 cda53876e86084dc04811d38dc95defa84134109f75f2936916866f7139d9f05
MD5 91747bd5150bf037b5d50e2efed78fe3
BLAKE2b-256 9a50ad719a3ac97908e1482454df47145f13ca93971eb32508f1cbf3dfaf91fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 ae2876e0a83fa88700d8dc70e877750a367858ac87d179da4bf724216174b965
MD5 433b67245a997d6cb7c02a1c48ae6aac
BLAKE2b-256 c1e0876bb1bf4d36f5ce72ebea7edd93c5958ab327cba1f30a910afbaf358820

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 5dbe2756976541cc4438b69ec68ea31323559514e847b4adfdbbd30e96409663
MD5 a538fed1ef98eeb9c0d9eae254d42d86
BLAKE2b-256 190bbe0ef8ae562f4333190a40558f7e19f14bdd8d45b7115cfaf40940f82225

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 ce77e834465b0910038d15b51fd91e961192a7c8147f61da466b225d405e60d9
MD5 b0cabbc3aaf05f4d896bdde6c563ab9a
BLAKE2b-256 8d5bf2bd267b8147cbbcc311d1399490557c2bc9d32baf33cce6a43267962b50

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c86074a02ff2646099784ef56e9a35b21c34b5052f66e887eb4d22c148263f1b
MD5 5378c540a7ea2d7988afef8bc11ce0b2
BLAKE2b-256 08b1ffc0280ad2291958072a51ff423b0e8d506fdcbe4da39e790dcaa6c44607

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 cc5ce7483990479edccbc253fb1252816a4051fec077034dc02665db8badfedc
MD5 b9ae148c9818b234480c308f2a9b651f
BLAKE2b-256 de243dd91929824e4400838573e506fc43fd4d49083c506d3ad652a12a160f54

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d3e79cb6c12842d63d2e4e4762a03ecb3b425b3a19e5842259dd8ef71bc1b0fd
MD5 ed21647339202e07a86287cf3f276025
BLAKE2b-256 c5a3380e7e33428b301860c7e09fc31ed262b10425945455380d7f77dde4b311

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 5da291df7216b1f677ef02e7ebed1c81bbf879c9c8d5f3bfe4d84cd4626b5b70
MD5 6d991bcf3f29bf2a17cafa62f6cb0565
BLAKE2b-256 d67a0090f50298ece1aa0b094b7efc922c76661477dd076210c1b1f6c3a5d284

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e947c26e15b9a721439cd59f74707248ea34f2495a77bfb75eee9d73c642dd10
MD5 94611a0d03247d75f35ec4ce62d2849f
BLAKE2b-256 7ac799e1da3b5fb655c0b412646586d059a9b6ac19d932137097e62da83bad62

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 49f40104be59e36c9de6b6356deb01478aff44ab2914411709c4bd83e1178b7d
MD5 cdba3c09fcf0a439bddecaaf3db26bbf
BLAKE2b-256 305310daab70e11cf58e4d7c594ea093315b652181c95abe6d5f10237e64f1aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 31a1f5860fc8ab102648449352238baae054eaef562ac14fe167e59359434089
MD5 0c2efab09678638dd3826f056e0d922a
BLAKE2b-256 4fe8ac3a35deb4b047cedf7f96c64b4750c7b06b5b7caaea8b2e112be474d487

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 49553b4fb5d82e84ed2a18370a95a6d586e1915c44fded18daff67ce508d233e
MD5 692e20ba291a37cd2916123aa90d20a0
BLAKE2b-256 ef58ff31e848f161f78ecd4bca0a25e08eedbbb745c016e2fbc8b8f4830da003

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 aa232959b7e3895ea8bab06007fb237a858e8f6e83789b954e206a837f0f3097
MD5 6f200bff9b7c70ebc6cc999ff8594471
BLAKE2b-256 d7b97fb9317c3e19fe53b2558d00827fb0cdef666c2bf32e0797f1b6f3402c0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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.2-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for chroma_term_console-0.1.2-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 6a815816b9876ed20da292703d7ae148156b2992387e36f4ec8eae977037a699
MD5 90623d3951f38e272b2eb8664af96056
BLAKE2b-256 9cdf8d0e6266af963f17d9afb0a5e9fd49b5ecdcaef45e1094d90df0f1c863a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for chroma_term_console-0.1.2-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

This release

0.1.2 This release

14 files

0.1.1

14 files

0.1.0

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