colorutils
色彩模块
ttkbootstrap.colorutils
color_to_hex()
color_to_hex(color, model='rgb')
将颜色值转换为十六进制。
颜色和模型参数表示要转换的颜色。 该值应为“名称”和“十六进制”模型的字符串,并且 a “rgb”和“hsl”型号的元组或列表。
参数:
名字 | 类型 | 描述 | 违约 |
---|---|---|---|
color | Any | 正在转换的模型的颜色值。 | 必填 |
model | str | 正在转换的颜色模型。 | 'rgb' |
返回:
类型 | 描述 |
---|---|
str | 十六进制颜色值。 |
源码(ttkbootstrap/colorutils.py):
def color_to_hex(color, model=RGB):
"""将颜色值转换为十六进制.
T颜色和模型参数表示要转换的颜色。
该值预计为 “名称” 和 “十六进制” 模型的字符串,并且
“rgb” 和 “hsl” 模型的元组或列表。
参数:
color (Any):
正在转换的模型的颜色值。
model (str):
正在转换的颜色模型。
Returns:
str:
十六进制颜色值。
"""
r, g, b = color_to_rgb(color, model)
return f'#{r:02x}{g:02x}{b:02x}'
color_to_hsl()
color_to_hsl(color, model='hex')
将颜色值转换为 HSL。
颜色和模型参数表示要转换的颜色。 该值应为“名称”和“十六进制”模型的字符串,并且 a “rgb”和“hsl”型号的元组或列表。
参数:
名字 | 类型 | 描述 | 违约 |
---|---|---|---|
color | Any | 正在转换的模型的颜色值。 | 必填 |
model | str | 正在转换的颜色模型。 | 'hex' |
返回:
类型 | 描述 |
---|---|
Tuple[int, int, int] | hsl 颜色值。 |
源码(ttkbootstrap/colorutils.py):
def color_to_hsl(color, model=HEX):
"""将颜色值转换为hsl.
颜色和模型参数表示要转换的颜色。
该值预计为 “名称” 和 “十六进制” 模型的字符串,并且
“rgb” 和 “hsl” 模型的元组或列表。.
Parameters:
color (Any):
正在转换的模型的颜色值。
model (str):
正在转换的颜色模型。
Returns:
Tuple[int, int, int]:
hsl颜色值。
"""
r, g, b = color_to_rgb(color, model)
hls = rgb_to_hls(r/255, g/255, b/255)
h = int(hls[0]*HUE)
l = int(hls[1]*LUM)
s = int(hls[2]*SAT)
return h, s, l
color_to_rgb()
color_to_rgb(color, model='hex')
将颜色值转换为 rgb。
颜色和模型参数表示要转换的颜色。 该值应为“名称”和“十六进制”模型的字符串,并且 a “rgb”和“hsl”型号的元组或列表。
参数:
名字 | 类型 | 描述 | 违约 |
---|---|---|---|
color | Any | 正在转换的模型的颜色值。 | 必填 |
model | str | 正在转换的颜色模型。 | 'hex' |
返回:
类型 | 描述 |
---|---|
Tuple[int, int, int] | rgb 颜色值。 |
源码(ttkbootstrap/colorutils.py)
def color_to_rgb(color, model=HEX):
"""将颜色值转换为rgb
颜色和模型参数表示要转换的颜色。
该值预计为 “名称” 和 “十六进制” 模型的字符串,并且
“rgb” 和 “hsl” 模型的元组或列表。
Parameters:
color (Any):
正在转换的模型的颜色值。
model (str):
正在转换的颜色模式
Returns:
Tuple[int, int, int]:
rgb颜色值。
"""
color_ = conform_color_model(color, model)
try:
return ImageColor.getrgb(color_)
except:
print('this')
conform_color_model()
conform_color_model(color, model)
使颜色值符合可解释的字符串 由 .PIL.ImageColor.getrgb method
参数:
名字 | 类型 | 描述 | 违约 |
---|---|---|---|
color | Union[Tuple[int, int, int], str] | 要符合的颜色值。 | 必填 |
model | str | “HSL”、“RGB”或“HEX”之一 | 必填 |
返回:
类型 | 描述 |
---|---|
str | 可用作参数的颜色值字符串 必。ImageColor.getrgb 方法。 |
源码(ttkbootstrap/colorutils.py)
def conform_color_model(color, model):
"""使颜色值符合可以解释的字符串通过 “pil.ImageColor.getrgb方法”.
Parameters:
color (Union[Tuple[int, int, int], str]):
要符合的颜色值。
model (str):
“Hsl”,“ rgb ”或“ hex” 之一
Returns:
str:
颜色值字符串,可以用作 PIL.ImageColor.getrgb方法
"""
if model == HSL:
h, s, l = color
return f'hsl({h},{s}%,{l}%)'
elif model == RGB:
r, g, b = color
return f'rgb({r},{g},{b})'
else:
return color
contrast_color()
contrast_color(color, model='rgb', darkcolor='#000', lightcolor='#fff')
返回最佳匹配的对比浅色或深色 给定的颜色。
参数:
名字 | 类型 | 描述 | 违约 |
---|---|---|---|
color | Any | 要评估的颜色值。 | 必填 |
model | str | 要评估的颜色值的模型。“RGB” by 违约。 | 'rgb' |
darkcolor | Any | 颜色相控时要返回的颜色值 应该是黑暗的。 | '#000' |
lightcolor | Any | 颜色相控时要返回的颜色值 应该是轻的。 | '#fff' |
返回:
类型 | 描述 |
---|---|
str | 匹配的颜色值。 |
源码(ttkbootstrap/colorutils.py)
def contrast_color(color, model=RGB, darkcolor='#000', lightcolor='#fff'):
"""返回最佳匹配的对比浅色或深色给定的颜色。
Parameters:
color (Any):
要评估的颜色值。
model (str):
要评估的颜色值的模型。'rgb' 默认。
darkcolor (Any):
约束颜色时要返回的颜色值应该是黑暗的。
lightcolor (Any):
约束颜色时要返回的颜色值应该是亮色
Returns:
str:
匹配的颜色值。
"""
if model != RGB:
r, g, b = color_to_rgb(color, model)
else:
r, g, b = color
luminance = ((0.299 * r) + (0.587 * g) + (0.114 * b))/255
if luminance > 0.5:
return darkcolor
else:
return lightcolor
update_hsl_value()
update_hsl_value(color, hue=None, sat=None, lum=None, inmodel='hsl', outmodel='hsl')
根据 提供色相、饱和、流明参数。
参数:
名字 | 类型 | 描述 | 违约 |
---|---|---|---|
color | Any | 颜色 | 必填 |
hue | int | 介于 0 和 360 之间的数字。 | None |
sat | int | 介于 0 和 100 之间的数字。 | None |
lum | int | 介于 0 和 100 之间的数字。 | None |
inmodel | str | 要更改的颜色所使用的颜色模型。其中之一 HSL,RGB,十六进制,名称。 | 'hsl' |
outmodel | str | 颜色为时要返回的颜色值模型 改变。HSL,RGB,十六进制之一。 | 'hsl' |
返回:
类型 | 描述 |
---|---|
Union[Tuple[int, int, int], str] | 基于所选颜色模型的颜色值。 |
源码(ttkbootstrap/colorutils.py)
def update_hsl_value(color, hue=None, sat=None, lum=None, inmodel=HSL, outmodel=HSL):
"""根据提供色调、sat、lum参数。
Parameters:
color (Any):
The color
hue (int):
0和360之间的数字。
sat (int):
0和100之间的数字。
lum (int):
0和100之间的数字。
inmodel (str):
要更改的颜色使用的颜色模型。其中一个hsl,rgb,十六进制,名称。
outmodel (str):
当颜色为变。hsl之一,rgb,十六进制。
Returns:
Union[Tuple[int, int, int], str]:
基于所选颜色模型的颜色值。
"""
h, s, l = color_to_hsl(color, inmodel)
if hue is not None:
h = hue
if sat is not None:
s = sat
if lum is not None:
l = lum
if outmodel == RGB:
return color_to_rgb([h, s, l], HSL)
elif outmodel == HEX:
return color_to_hex([h, s, l], HSL)
else:
return h, s, l