def __init__(self, r: float, g: float, b: float, a: float): super().__init__(r, g, b) utils.validate(a) self._a = a
def vals(self, values: Sequence[float]): if len(values) != 4: raise ValueError("Needs exactly 4 floats") utils.validate(values[0]) utils.validate(values[1]) utils.validate(values[2]) utils.validate(values[3]) self.r, self.g, self.b, self.a = values
def vals(self, values: Sequence[float]): if len(values) != 4: raise ValueError("Needs exactly 4 floats") utils.validate(values[0]) utils.validate(values[1]) utils.validate(values[2]) utils.validate(values[3]) rgb = colorsys.hsv_to_rgb(values[0], values[1], values[2]) self.color = cast("colors.AlphaColor", self.color) self.color.rgba.vals = rgb + (values[3], )
def from_name(cls, name: str, a: float = 0.0) -> "AlphaColor": """ Creates an AlphaColor object based on the given color name. Only CSS3 extended color keyword names are recognized. Arguments: name: Name of color. a: Alpha value of color as a float in the range [0.0, 1.0]. Default value is 0.0 Returns: AlphaColor object with value corresponding to the given color name and alpha value. """ utils.validate(a) a_int = int(a * 0xff) name = name.lower() rgb_intval = colorviews.names.COLORS[name] rgba_intval = (rgb_intval << 8) | a_int return cls.from_int(rgba_intval)
def vals(self, values: Sequence[float]): if len(values) != 3: raise ValueError("Needs exactly 3 floats") utils.validate(values[0]) utils.validate(values[1]) utils.validate(values[2]) rgb = colorsys.hls_to_rgb(values[0], values[2], values[1]) self.color = cast("colors.Color", self.color) self.color.rgb.vals = rgb
def __init__(self, r: float, g: float, b: float): utils.validate(r) utils.validate(g) utils.validate(b) self._r = r self._g = g self._b = b
def from_hsva(cls, h: float, s: float, v: float, a: float) -> "AlphaColor": """ Creates an AlphaColor object from the given HSVA float values. Arguments: h: Hue component of HSVA value as a float in the range [0.0, 1.0] s: Saturation component of HSVA value as a float in the range [0.0, 1.0]. v: Value component of HSVA value as a float in the range [0.0, 1.0]. a: Alpha component of HSVA value as a float in the range [0.0, 1.0]. Returns: AlphaColor object with given HSVA values. Raises: ValueError: If value is outside the valid range. """ utils.validate(h) utils.validate(s) utils.validate(v) utils.validate(a) rgba = colorsys.hsv_to_rgb(h, s, v) + (a, ) return cls(*rgba)
def from_hsv(cls, h: float, s: float, v: float) -> "Color": """ Creates a Color object from the given HSV float values. Arguments: h: Hue component of HSV value as a float in the range [0.0, 1.0] s: Saturation component of HSV value as a float in the range [0.0, 1.0] v: Value component of HSV value as a float in the range [0.0, 1.0] Returns: Color object with given HSV values. Raises: ValueError: If any argument is outside valid range. """ utils.validate(h) utils.validate(s) utils.validate(v) rgb = colorsys.hsv_to_rgb(h, s, v) return cls(*rgb)
def from_hsl(cls, h: float, s: float, l: float) -> "Color": """ Creates a Color object from the given HSL float values. Arguments: h: Hue component of HSL value as a float in the range [0.0, 1.0] s: Saturation component of HSL value as a float in the range [0.0, 1.0] l: Lightness component of HSL value as a float in the range [0.0, 1.0] Returns: Color object with given HSL values. Raises: ValueError: If any argument is outside valid range. """ utils.validate(h) utils.validate(s) utils.validate(l) rgb = colorsys.hls_to_rgb(h, l, s) return cls(*rgb)
def b(self, val: float): utils.validate(val) self.color._b = val
def g(self, val: float): utils.validate(val) self.color._g = val
def test_valid(self, val): assert validate(val) is None
def a(self, val: float): utils.validate(val) self.color = cast("colors.AlphaColor", self.color) self.color._a = val
def v(self, val: float): utils.validate(val) rgb = (self.color._r, self.color._g, self.color._b) hsv = colorsys.rgb_to_hsv(*rgb) rgb = colorsys.hsv_to_rgb(hsv[0], hsv[1], val) self.color._r, self.color._g, self.color._b = rgb
def l(self, val: float): utils.validate(val) rgb = (self.color._r, self.color._g, self.color._b) hls = colorsys.rgb_to_hls(*rgb) rgb = colorsys.hls_to_rgb(hls[0], val, hls[1]) self.color._r, self.color._g, self.color._b = rgb
def r(self, val: float): utils.validate(val) self.color._r = val
def test_invalid(self, val): with pytest.raises(ValueError): validate(val)