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 / PyCharm 集成终端及运行控制台均支持颜色输出
  • 非 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.4-cp314-cp314-win_amd64.whl (232.9 kB view details)

Uploaded CPython 3.14Windows x86-64

chroma_term_console-0.1.4-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.4-cp313-cp313-win_amd64.whl (226.1 kB view details)

Uploaded CPython 3.13Windows x86-64

chroma_term_console-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

chroma_term_console-0.1.4-cp312-cp312-win_amd64.whl (229.2 kB view details)

Uploaded CPython 3.12Windows x86-64

chroma_term_console-0.1.4-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.4-cp311-cp311-win_amd64.whl (239.2 kB view details)

Uploaded CPython 3.11Windows x86-64

chroma_term_console-0.1.4-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.4-cp310-cp310-win_amd64.whl (238.3 kB view details)

Uploaded CPython 3.10Windows x86-64

chroma_term_console-0.1.4-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.4-cp39-cp39-win_amd64.whl (239.0 kB view details)

Uploaded CPython 3.9Windows x86-64

chroma_term_console-0.1.4-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.4-cp38-cp38-win_amd64.whl (245.1 kB view details)

Uploaded CPython 3.8Windows x86-64

chroma_term_console-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 8992af32489ab5d168711fe3941758ac87b55c5b211e6ea33449035082ac88e3
MD5 04bbfc8bc40e32aa5e0ee4aaa862aea6
BLAKE2b-256 208c45d2ebd9f4ce979231e495226a15a4b1ab57591d44fe6fec4c398dd2f658

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4141671d62276227a1f1d7a9fa4a4ae309ebcac87052cb9917d47e465e3103c6
MD5 cf95dbb34cb076c756d26bb1718ec610
BLAKE2b-256 617f5c658088b7a9b839284c7161f491748cfb568d9dc9143e7886c5bd7ea25d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c8ed90ba9ab4ac9704450bb0c596f12d9ffcd574f712a79a3ff8a6c19e85767c
MD5 346034cf852471efcd5687f559f01b26
BLAKE2b-256 882af200d0f55591b32a4e70ca49d0d26ea76365d9e69612fe56825d3e218ef6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 62b596aa5b6c2ff32de1a23ccffd497aa05d2943fa0c86810c93eed8ff1e22f8
MD5 b458df75dba3108d2b67703db7148398
BLAKE2b-256 3e379675ff4ea7e7e1898e8c595467433a54fc257398e337af562783a21b6d83

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 66bac9f9ce337a5f5d7d78cb9e55965030b8902f07c8d67909849adb6cc93ae4
MD5 b8a3d8863f43880fecf92cc5d0807bc2
BLAKE2b-256 7f547e10b52b6b1d67adb1bf5abcf37fe5a76d2deef6156b800afb307cec1dde

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 184666a8dece64d0b9b14a2f0e032983962ba00d285157d1b0d0c99dddd1756f
MD5 af39a13e93acb9de61ff0bc9062c45b2
BLAKE2b-256 9c8db84bd74c9c338fadcc7300f70a64d2a7ddc208e1cfb05c9b3d2a6e62eb74

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ffb031373e1943524f24e550b75328d62ae706b865e2d99aca238cf162c2d72d
MD5 4a15b610264633d78ca912fc53388162
BLAKE2b-256 5423d183331bd4989746f508168097264b3ce57c40627180b075878ca20d453b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 48fdcc2a091f852a5a1643cafa9afb1cf70d28d5da2ac1ca35639441e2242e67
MD5 93f87e3509b51697a662b1f6156e07b2
BLAKE2b-256 af7c5291ab924c74e755bb52c0f3cfa6dacb595ff035cda6ae4d2acca056351a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 354eceb64f3cf52464e60edee9a07fd09d71742118769fa3d5b34a32ac694021
MD5 44435fccca0f4803b2f75bd08ca25099
BLAKE2b-256 e28194184e16b3a4da6a9e29863996f3927228cc4ea1040b8c79e2dbb57c4631

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 24ccfcd05a76035220b127dca19204ee5f91e6b78863cfa5a03d12dc8aadf303
MD5 742ae5fc28c6015fa979b086f0275c22
BLAKE2b-256 46bb174725ad330dd30f86fd37a7e2a6518dc0abebe95eeda6a3bf37e74fe476

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8a17f66e3d2ffb071ce7e96cbc83ed16d9f7c3209a132e9770b21b5bd30b0f51
MD5 c6075a310f10aeef7fee410b6d811548
BLAKE2b-256 423433fc521a22aeadef57589eb7f8fcaa08908b703c9af99ec4f3761bc2fd65

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 040e04fac47083d69cab22af8595bf9387419345e13ac5cff3737878664fc2bd
MD5 0d3db0f6ca270e3b9042f8cee1f3c786
BLAKE2b-256 c946f69274a8506637023df9515b30e2ada3e877bf1fc44e3364c8ca3e6d43ca

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 6ceedb04e7beed1525e7f603ffdecaf45c9ed98ee8b93d4f9f184f93b80cd58e
MD5 b86cf6078f3dfc6fb03608f44631e1a6
BLAKE2b-256 44fe3f658d80d44bfdde8b7c0918166226634015dfdae8ba399b362f553242e7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.4-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c5e672b95d963daf312fd2458ee9034669d4e614f69abcd95e434423d1ccb235
MD5 06abd70a25ed8a8e716bd6402271838e
BLAKE2b-256 f4720a5f1e600740d3e19cefa74d46e5f5d4ab7cdb57df41318d756a93863aee

See more details on using hashes here.

Provenance

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

This release

0.1.4 This release

14 files

0.1.3

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