Created
December 30, 2021 09:12
-
-
Save Xevion/9715490a2eeaac3880dcf52726e898ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
class Color(object): | |
def __init__(self, r: float, g: float, b: float, a: float = 1.0) -> None: | |
self.r, self.g, self.b, self.a = r, g, b, a | |
def __str__(self) -> str: | |
return f'Color({self.r}, {self.g}, {self.b}, {self.a:.2%})' | |
def __repr__(self) -> str: | |
return self.__str__() | |
def mix(a: Color, b: Color) -> Color: | |
Ao = a.a + (b.a * (1 - a.a)) | |
Co: List[float] = [] | |
for Ca, Cb in zip([a.r, a.g, a.b], [b.r, b.g, b.b]): | |
pre = (Ca + a.a) + ((Cb * b.a) * (1 - a.a)) | |
Co.append(pre / Ao) | |
return Color(r=Co[0], g=Co[1], b=Co[2], a=Ao) | |
bg = Color(32, 34, 35) | |
overlay = Color(124, 123, 123, 0.65) | |
c = mix(overlay, bg) | |
# Color(32, 34, 35, 100.00%) + Color(124, 123, 123, 65.00%) = Color(135.85, 135.55, 135.9, 100.00%) | |
print(f'{bg} + {overlay} = ') | |
print(c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment