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 和字符串拼接,无需手动转换。

链式调用(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.1-cp314-cp314-win_amd64.whl (181.9 kB view details)

Uploaded CPython 3.14Windows x86-64

chroma_term_console-0.1.1-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.1-cp313-cp313-win_amd64.whl (176.7 kB view details)

Uploaded CPython 3.13Windows x86-64

chroma_term_console-0.1.1-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.1-cp312-cp312-win_amd64.whl (179.3 kB view details)

Uploaded CPython 3.12Windows x86-64

chroma_term_console-0.1.1-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.1-cp311-cp311-win_amd64.whl (187.2 kB view details)

Uploaded CPython 3.11Windows x86-64

chroma_term_console-0.1.1-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.1-cp310-cp310-win_amd64.whl (186.5 kB view details)

Uploaded CPython 3.10Windows x86-64

chroma_term_console-0.1.1-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.1-cp39-cp39-win_amd64.whl (186.8 kB view details)

Uploaded CPython 3.9Windows x86-64

chroma_term_console-0.1.1-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.1-cp38-cp38-win_amd64.whl (191.7 kB view details)

Uploaded CPython 3.8Windows x86-64

chroma_term_console-0.1.1-cp38-cp38-manylinux_2_17_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 8fe7e115bdfae388bc4961ac52e285c62457633b05dd1f586403af12107e501a
MD5 747bcfdbb4993ce2d992c9c25caac85d
BLAKE2b-256 9eee7e0f8e441c707ba8cf443ff981ad454ba9842c5a6cb5b9891e4196c22093

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 afe250ef492a08b4b57f6f7e15c5cf8b247699947149f51e5ca0c2c49c6c270f
MD5 392805fac81f33f3cddf18acf34d4c1e
BLAKE2b-256 556a78155ec334b773830a056c5f3eaf68398a7fa1e11686ce7993295b835a8e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 90055ee03004ac3f3a68a64dfaf51f12acc9b121caac3c97bb84f6590c640dc5
MD5 45986ac6a4ab4e7eea99d66b7955cd5a
BLAKE2b-256 b6df38f7f8f1b864d0a89c25631ebaed59a324bf50618c04112e39617a898fa7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 504ccec0d013e19022b0e46f4ac9123eaa280c2c9aa10d90496f2affc538d106
MD5 0d878c5429adbe4e8dac9863d2b59157
BLAKE2b-256 295ad9854cafaf2eefb1d196292cffd96b44e1fdea181f9dc2feadcd90d61114

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4173f7bf528c34adc77974ad0b6a2c17976707939ea5e3fa082e322d7533f4ce
MD5 ce9de5caf13dc12d576088a445bafa0e
BLAKE2b-256 6249459102366a777a7480f235120467c8c3f443b3cc89159647e61f6ab50b52

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 863313ec356a7d2d272fdeed0fab5401511a2c0ced66a303f58ae102ff92c1bb
MD5 4188863e2a4d3bc8d71b5052ded2e81d
BLAKE2b-256 19caf9dfe3b3e7229a9fe67eca53a500a4700375a32a63d3dae5e8e1de476735

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 10c2016b2aa4690073ccd7e8874925c65f60866ca4a6bb956a2b40a150aaae75
MD5 dade04ca42b7c7878b9f55e46add7eba
BLAKE2b-256 75e78eaa214c866e2c940105dc58f467578bc4ca8982f9a7e0af6db13901aa13

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 76271451ef3c3063631daa8fbadd197f4acddf22a8820efdc7684bc1cb98d5ab
MD5 f4d8bfccc7979898c6b2b5148cfc0594
BLAKE2b-256 73397938d99783c5ccd8f7204881b3ae11383f6cb6be8bf3a0a03fc81c00d9d5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 660fe4fe329efe4d176d373d5839ff0efb444a1e00c90368cd99c1510f5eac11
MD5 e43e76ef1727963a7330556a63899e99
BLAKE2b-256 eb7d88c28531edaa42e13c26c0d17ecb9073729c1ef0b0ca81f162c6776644f3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7ab2e0c2cd6dcb87c85c55eb6c132a6c77a38fac67ccd752061d41bb59b5f775
MD5 24a51dbb2cfab22ab94ce12ef7230e71
BLAKE2b-256 6245a1013cd5e7c74830a8efb91e6c499a74a75d69dbcb277c8ab822af8c6784

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 63dc1540fd76708b733b4bc29ee8ccb09544fde7bf48b23f8ddc75ea20ff38b0
MD5 6d42ba8904ca869338ff8080431a9972
BLAKE2b-256 39ffc7bdab6d844eebdcbf1ea63b57bb7b29be92d763a99e8705c2e204f19ac7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 72fd98ca8317a5a00ac76030f2ef7cfd07384d935acdc01524774f5d75e6ace2
MD5 37d3ab62b0ae29dd1c9a7ad4e4d5cd57
BLAKE2b-256 af6fabb797f0258dd163b2df035a5d8dc2eab72755cfd7c1a9d38f71846e21cb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 dd984d0ff195cf8422f6eb9d4233a172cf4857bfce3d38c03717698d9eb48290
MD5 a2c67f6219de8d3d4e552757e1d29181
BLAKE2b-256 bc22147c156e8f506544b82eb247b96a802bb84c3b4d473e9871759d3b8189c2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for chroma_term_console-0.1.1-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 52ece64df8dcd4a586d2cbda14c1def5e29fee2016ea0bcb111809d6d09cb01a
MD5 19b35b1f7ddc852e3a3b131d2a2a6f44
BLAKE2b-256 03d6431c2b3a50cb166bdc24f0bd6a1befb5ab3cad51bffa5673839e2dcb2593

See more details on using hashes here.

Provenance

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

This release

0.1.1 This release

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