from dataclasses import dataclass @dataclass class Color: red: float green: float blue: float alpha: float def __mul__(self, o) -> "Color": return Color(self.red * o, self.green * o, self.blue * o, self.alpha * o) def __truediv__(self, o) -> "Color": return Color(self.red / o, self.green / o, self.blue / o, self.alpha / o) def __add__(self, o) -> "Color": return Color(self.red + o.red, self.green + o.green, self.blue + o.blue, self.alpha + o.alpha) def composite1(C_fg: Color, a_fg: float, C_bg: Color): return C_fg * a_fg + C_bg * (1.0 - a_fg) def composite2(C_fg: Color, a_fg: float, C_bg: Color, a_bg: float = 1): a_final = a_fg + a_bg * (1.0 - a_fg) return (C_fg * a_fg + C_bg * a_bg * (1.0 - a_fg)) / a_final C_bg = Color(0.0, 0.0, 0.0, 1.0)