class Color: def __init__(self, red, green, blue): if not (Color.check_color_value(red) or Color.check_color_value(green) or Color.check_color_value(blue)): print(f"Color is out of range") return self.red = red self.green = green self.blue = blue @classmethod def from_hex(cls, hexstring): "Initialize Color from hexstring (e.g. #ffffff)" if isinstance(hexstring, str): if hexstring[0] == '#': hexstring = hexstring[1:] if len(hexstring) == 6: color = tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) return cls(color[0], color[1], color[2]) @classmethod def from_name(cls, color_name): "Initialize Color from name (e.g. 'white')" color_name = str.lower(color_name) if color_name == 'white': return cls(255,255,255) elif color_name == 'black': return cls(0, 0, 0) elif color_name == 'red': return cls(255, 0, 0) elif color_name == 'green': return cls(0, 255, 0) elif color_name == 'blue': return cls(0, 0, 255) else: raise ValueError(f'Colorname {color_name} was not recognised') @staticmethod def check_color_value(value): if not isinstance(value, int): print(f"Colorvalue '{value}' is not a number") return False if value < 0 or value > 255: print(f"Color value '{value}' is out of bounds (0-255)") return False return True