4. 自定义色带¶
演示如何创建、修改、删除自定义色带。
添加色带¶
cmap.add() 将自定义色带存入数据库。内置色带不可覆盖。
In [1]:
Copied!
from multicolor import cmap
cmap.add(
name="SunsetGlow",
colors=["#2d004e", "#8b2fc9", "#d4a0ff", "#f9a825", "#ff5722"],
cmap_type="diverging",
tags=["custom", "warm"],
colorblind_safe="none",
description="Purple-to-orange diverging colormap for custom use.",
source=["user"],
)
custom = cmap.get("SunsetGlow")
cmap.preview(custom, title="SunsetGlow")
from multicolor import cmap
cmap.add(
name="SunsetGlow",
colors=["#2d004e", "#8b2fc9", "#d4a0ff", "#f9a825", "#ff5722"],
cmap_type="diverging",
tags=["custom", "warm"],
colorblind_safe="none",
description="Purple-to-orange diverging colormap for custom use.",
source=["user"],
)
custom = cmap.get("SunsetGlow")
cmap.preview(custom, title="SunsetGlow")
输入验证¶
cmap.add() 会验证输入,防止无效数据进入数据库。
In [2]:
Copied!
# 无效 hex 颜色
try:
cmap.add(name="Bad", colors=["not-a-color"], cmap_type="sequential")
except ValueError as e:
print(f"Rejected: {e}")
# 无效的 cmap_type
try:
cmap.add(name="Bad", colors=["#ff0000"], cmap_type="gradient")
except ValueError as e:
print(f"Rejected: {e}")
# 空名称
try:
cmap.add(name="", colors=["#ff0000"], cmap_type="sequential")
except ValueError as e:
print(f"Rejected: {e}")
# 无效 hex 颜色
try:
cmap.add(name="Bad", colors=["not-a-color"], cmap_type="sequential")
except ValueError as e:
print(f"Rejected: {e}")
# 无效的 cmap_type
try:
cmap.add(name="Bad", colors=["#ff0000"], cmap_type="gradient")
except ValueError as e:
print(f"Rejected: {e}")
# 空名称
try:
cmap.add(name="", colors=["#ff0000"], cmap_type="sequential")
except ValueError as e:
print(f"Rejected: {e}")
Rejected: Invalid hex color: 'not-a-color'. Expected format: '#RRGGBB', e.g. '#ff0000'.
Rejected: Invalid cmap_type: 'gradient'. Must be one of {'diverging', 'sequential', 'qualitative'}.
Rejected: Colormap name must be a non-empty string.
覆盖自定义色带¶
In [3]:
Copied!
cmap.add(
name="SunsetGlow",
colors=["#004e1a", "#2fc98b", "#d4f0ff", "#a825f9", "#2257ff"],
cmap_type="sequential",
tags=["custom", "updated"],
description="Updated version.",
)
cmap.preview(cmap.get("SunsetGlow"), title="SunsetGlow (updated)")
cmap.add(
name="SunsetGlow",
colors=["#004e1a", "#2fc98b", "#d4f0ff", "#a825f9", "#2257ff"],
cmap_type="sequential",
tags=["custom", "updated"],
description="Updated version.",
)
cmap.preview(cmap.get("SunsetGlow"), title="SunsetGlow (updated)")
内置色带受保护¶
In [4]:
Copied!
try:
cmap.add(name="Viridis", colors=["#000000", "#ffffff"], cmap_type="sequential")
except ValueError as e:
print(f"Blocked: {e}")
try:
cmap.remove("Viridis")
except ValueError as e:
print(f"Blocked: {e}")
try:
cmap.add(name="Viridis", colors=["#000000", "#ffffff"], cmap_type="sequential")
except ValueError as e:
print(f"Blocked: {e}")
try:
cmap.remove("Viridis")
except ValueError as e:
print(f"Blocked: {e}")
Blocked: Cannot overwrite built-in colormap 'Viridis'. Use a different name. Blocked: Cannot delete built-in colormap 'Viridis'.
删除自定义色带¶
In [5]:
Copied!
# 先确认存在
print("Before:", "SunsetGlow" in cmap.names())
# 删除
cmap.remove("SunsetGlow")
# 确认已删除
print("After:", "SunsetGlow" in cmap.names())
# 先确认存在
print("Before:", "SunsetGlow" in cmap.names())
# 删除
cmap.remove("SunsetGlow")
# 确认已删除
print("After:", "SunsetGlow" in cmap.names())
Before: True After: False