本文整理汇总了Python中webcolors.css3_names_to_hex方法的典型用法代码示例。如果您正苦于以下问题:Python webcolors.css3_names_to_hex方法的具体用法?Python webcolors.css3_names_to_hex怎么用?Python webcolors.css3_names_to_hex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类webcolors
的用法示例。
在下文中一共展示了webcolors.css3_names_to_hex方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_css3_color
# 需要导入模块: import webcolors [as 别名]
# 或者: from webcolors import css3_names_to_hex [as 别名]
def is_css3_color(instance):
if instance.lower() in webcolors.css3_names_to_hex:
return True
return is_css_color_code(instance)
示例2: style_to_wdcolor
# 需要导入模块: import webcolors [as 别名]
# 或者: from webcolors import css3_names_to_hex [as 别名]
def style_to_wdcolor(value):
if value == 'none':
return None
try:
if value.startswith('rgb('):
value = WordFormatter.rgbstring_to_hex(value)
elif value in webcolors.css3_names_to_hex:
value = webcolors.css3_names_to_hex[value]
return WordFormatter.hex_to_wdcolor(value)
except Exception:
return None
示例3: is_color
# 需要导入模块: import webcolors [as 别名]
# 或者: from webcolors import css3_names_to_hex [as 别名]
def is_color(s):
""" Do our best to determine if "s" is a color spec that can be converted to hex
Args:
s (str or int): string or integer describing a color
Returns:
bool: True if this can be converted to a hex-compatible color
"""
def in_range(i): return 0 <= i <= int('0xFFFFFF', 0)
try:
if type(s) == int:
return in_range(s)
elif type(s) not in (str, bytes):
return False
elif s in webcolors.css3_names_to_hex:
return True
elif s[0] == '#':
return in_range(int('0x' + s[1:], 0))
elif s[0:2] == '0x':
return in_range(int(s, 0))
elif len(s) == 6:
return in_range(int('0x' + s, 0))
except ValueError:
return False
示例4: translate_color
# 需要导入模块: import webcolors [as 别名]
# 或者: from webcolors import css3_names_to_hex [as 别名]
def translate_color(color, prefix='0x'):
""" Return a normalized for a given color, specified as hex or as a CSS3 color name.
Args:
color (int or str): can be an integer, hex code (with or without '0x' or '#'), or
css3 color name
prefix (str): prepend the raw hex string with this (usually '#' or '0x')
Returns:
str: hex string of the form '0x123abc'
"""
formatter = prefix + '{:06x}'
if isinstance(color, basestring):
if color.lower() in webcolors.css3_names_to_hex:
color = webcolors.css3_names_to_hex[color.lower()]
if len(color) == 7 and color[0] == '#': # hex that starts with '#'
color = color[1:]
elif len(color) == 8 and color[0:2] == '0x': # hex str that starts with '0x'
color = color[2:]
if len(color) == 6: # hex without prefix
color = prefix + color
else:
raise ValueError('Failed to translate color %s' % color)
elif isinstance(color, int):
color = formatter.format(color)
else:
raise ValueError('Unrecognized color %s of type %s' % (color, type(color)))
return color