我正在尝试制作一个名为Color
的简单对象。我希望它具有与编码为十六进制颜色的字符串相同的功能。看起来像在工作,但是功能不一样。
import pandas as pd
from matplotlib.colors import to_rgb, rgb2hex
class Color(str):
def __new__(cls, value, *args, **kwargs):
# explicitly only pass value to the str constructor
cls.color = rgb2hex(to_rgb(value))
return super(Color, cls).__new__(cls, cls.color)
x = pd.Series([Color("fuchsia"), Color("black")])
print(x)
# 0 #ff00ff
# 1 #000000
# dtype: object
print("Should be fuchsia but it's black", x[0].color)
# Should be fuchsia but it's black #000000
print("Should be black", x[0].color)
# Should be black #000000
例如,以下操作有效:
print(color)
print(pd.Series(color))
print(to_rgb(str(color)))
但这不是:
print(to_rgb(color))
我不知道为什么这不起作用。我已经尝试过了,但是不适合这种情况:Python, represent non-string object as string?
from matplotlib.colors import hex2color, to_rgb, to_rgba
class Color(object):
# Built in
def __init__(self, color):
self.color = rgb2hex(to_rgb(color))
def __repr__(self):
return self.color
def __str__(self):
return self.color
color = Color("black")
print(color)
# #000000
print(pd.Series(color))
# 0 #000000
# dtype: object
print(to_rgb(str(color)))
# (0.0, 0.0, 0.0)
print(to_rgb(color))
这是错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/anaconda/envs/µ_env/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba(c, alpha)
173 try:
--> 174 rgba = _colors_full_map.cache[c, alpha]
175 except (KeyError, TypeError): # Not in cache, or unhashable.
KeyError: (#000000, None)
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-35-a478f942e1d2> in <module>
16 print(pd.Series(color))
17 print(to_rgb(str(color)))
---> 18 print(to_rgb(color))
~/anaconda/envs/µ_env/lib/python3.6/site-packages/matplotlib/colors.py in to_rgb(c)
279 def to_rgb(c):
280 """Convert *c* to an RGB color, silently dropping the alpha channel."""
--> 281 return to_rgba(c)[:3]
282
283
~/anaconda/envs/µ_env/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba(c, alpha)
174 rgba = _colors_full_map.cache[c, alpha]
175 except (KeyError, TypeError): # Not in cache, or unhashable.
--> 176 rgba = _to_rgba_no_colorcycle(c, alpha)
177 try:
178 _colors_full_map.cache[c, alpha] = rgba
~/anaconda/envs/µ_env/lib/python3.6/site-packages/matplotlib/colors.py in _to_rgba_no_colorcycle(c, alpha)
225 # float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
226 # Test dimensionality to reject single floats.
--> 227 raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
228 # Return a tuple to prevent the cached value from being modified.
229 c = tuple(c.astype(float))
ValueError: Invalid RGBA argument: #000000
答案 0 :(得分:2)
也许您想继承str
来提供您的字符串。
import pandas as pd
from matplotlib.colors import to_rgb, rgb2hex
class Color(str):
def __new__(cls, value, *args, **kwargs):
# explicitly only pass value to the str constructor
v = rgb2hex(to_rgb(value))
return super(Color, cls).__new__(cls, v)
color = Color("fuchsia")
print(color)
print(pd.Series(color))
print(to_rgb(str(color)))
print(to_rgb(color))
输出
#ff00ff
0 #ff00ff
dtype: object
(1.0, 0.0, 1.0)
(1.0, 0.0, 1.0)
如果要将初始值存储在字符串中,可以这样做
class Color(str):
def __new__(cls, value, *args, **kwargs):
# explicitly only pass value to the str constructor
v = rgb2hex(to_rgb(value))
return super(Color, cls).__new__(cls, v)
def __init__(self, value):
self.color = value