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 处理
  • VS Code 集成终端自动启用(VT 优先,失败时回退为 ANSI→Win32 转换)
  • 非 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.3-cp314-cp314-win_amd64.whl (228.2 kB view details)

Uploaded CPython 3.14Windows x86-64

chroma_term_console-0.1.3-cp314-cp314-manylinux_2_17_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.3-cp313-cp313-win_amd64.whl (221.5 kB view details)

Uploaded CPython 3.13Windows x86-64

chroma_term_console-0.1.3-cp313-cp313-manylinux_2_17_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.3-cp312-cp312-win_amd64.whl (224.4 kB view details)

Uploaded CPython 3.12Windows x86-64

chroma_term_console-0.1.3-cp312-cp312-manylinux_2_17_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.3-cp311-cp311-win_amd64.whl (233.7 kB view details)

Uploaded CPython 3.11Windows x86-64

chroma_term_console-0.1.3-cp311-cp311-manylinux_2_17_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.3-cp310-cp310-win_amd64.whl (232.7 kB view details)

Uploaded CPython 3.10Windows x86-64

chroma_term_console-0.1.3-cp310-cp310-manylinux_2_17_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.3-cp39-cp39-win_amd64.whl (233.4 kB view details)

Uploaded CPython 3.9Windows x86-64

chroma_term_console-0.1.3-cp39-cp39-manylinux_2_17_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.3-cp38-cp38-win_amd64.whl (239.6 kB view details)

Uploaded CPython 3.8Windows x86-64

chroma_term_console-0.1.3-cp38-cp38-manylinux_2_17_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 365e0470c9252c2a83ca3c13b47c497548e730c72fcafd8a970121cd8b6ce67a
MD5 841efb9b1624e03e95ed9e530bafe59a
BLAKE2b-256 94836aafcf19472a9793a6532f4d1b0493bb851a358707c27dff0db191b3046f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 540c352a137e0189b2f0598ef747a02d00c5d5980e478f2b9ed7eb64b789cc73
MD5 0cfee28a1bf7b45a70eb4931e82fc1aa
BLAKE2b-256 33509206e166d77ff41eecb05ffed52bfe8852979de0dbc84b9c139e4df9c2da

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 dd2f67dd5fcb8e03fae119a4a3f55dae7a16eeb9e15df15b28dd6596e11aee86
MD5 374bfa81b883e3c90ece807a0f9ca127
BLAKE2b-256 81a4e6b221a54fa25a95018690353464bc1366fba13d639910c6b4116f8400c3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e75ce1c885e0190f83372406f1b9f28c4727187904d93428f62c6fd777a91f85
MD5 4c1b358d3d1dee8d22287f9e2f717aee
BLAKE2b-256 7d194ed412869745f213770311e88f68ad8bc0032df1541dd1bfc22570b0f207

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 366e6a50f6b80c4e1ae4d0b501617bc6639152281aaa372c834e875bda81d2be
MD5 f8fa136ae438bffd1d1e19eb12456041
BLAKE2b-256 e255206e4611c4bb4bf405e3a6eec8140a2b294dd8a23a449b02606acac00560

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3986f6d284e08732fe077283dff51807ccc4ffce93bee2670bb40203b7fb1891
MD5 25cf1550fa473caaa82bf67f74717160
BLAKE2b-256 456ff1f96f71f87b65c909fdb786bad59cbb6eeac4d09220c2cc6cd14bac8777

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bc23ba46f17994ecfc08598a52edd2bcfc08ac8d5e5ce0a3878fbe1dc813dbed
MD5 fd66a23b09c4c82e578ff8d9c4199b66
BLAKE2b-256 9e74f39e91a6776ed7c273c5cce0f9244b34723c2f8c70218cc5bc3cf5b3150a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 44067c4183608ea2e726128b8cf292c34f1d8b4f5416fa38d10aad4718a92a23
MD5 4439bdcf441ec8a94fbcc451d07720c0
BLAKE2b-256 f97911334bd9fe3de326594fca3bc2fcd3ee48884915db01166db4f9b118ee16

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3437e8151a9829828169edc656892fe1a78c6144a544cfc71b119de4c1e15f7f
MD5 8b38f50558165449cd197350e428340b
BLAKE2b-256 5282d903d56722dc7d39106a3d1ab55ae0a154b18b72ae233791012a1dce8130

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 0b709ecc7764e6e070475ab0754e829b893079f874c450e56f5e1a56bd764176
MD5 80a3b04ed615c80dcaca8f3ab3b1b146
BLAKE2b-256 77e1bc3c0ca8bd47f6a7ae9f247be5c0a8e78c7947e8b977acc168969aa7a988

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 76e95becf956ee7b1de7abe3adde246c033290d449a010cf9b59f8841f793bde
MD5 02080424db636158f63cffe66359f5d5
BLAKE2b-256 36ba169e60eaa7d92def728ab76da8393426b8a352d725fe8375086c6ab08ec3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 af24e67cac3eed6017020c7901c7ad7768820dbe511650de060e21b115691870
MD5 c7d718d20fffce00a03d5918aac47fb6
BLAKE2b-256 0a4e0122e82c20a2b0e12a3a8c6a007b3848bbe8e508e7d4aa0af2b90dac30ea

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 8ec0606d1435ec4e718729eec8a0f22d9ededce9e7f32e3914032b2298a6566e
MD5 8d8287c4f48fa7d0a13f95cac37bbe12
BLAKE2b-256 e2b838f153e206b9934d087b2a3c3463514c8985571834ae24f8557fe7295b2d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.3-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 786b773c67d6ecfafcc7d9ae8b247d8c292285a3ac1c7ec14e69b8df41361cd4
MD5 37cb44ecc5d7fb94c37295095c25c5c8
BLAKE2b-256 4824313bc5ffb30c73bff487cc869f9a3677eac2d2688c9e3d644330b0e4a462

See more details on using hashes here.

Provenance

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

This release

0.1.3 This release

14 files

0.1.2

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