def addColorModifier(self, colorName, modName, modValues): print( f' Modifier "{modName}" detected, attempting to add modValues {modValues} to colorName {colorName}' ) colorName = colorName.replace(modName, '', 1) try: colorObject = Color(colorName) newValues = [] for oldValue, newValue in zip(colorObject.rgb, modValues): # TODO: max(0, <- CAUSES STROBE EFFECT!!! REMEMBER THIS if isinstance(newValue, int): print(10) newValues.append( max(0.05, min(1, ((oldValue * 255) + newValue) / 255))) elif isinstance(newValue, float): print(20) newValues.append(max(0.05, min(1, oldValue * newValue))) print(colorObject.rgb, newValues) colorObject.rgb = newValues return colorObject except ValueError as error: print( f' Color is still invalid, giving up. Non-fatal error: "{error}"' ) return None
def pick_font_color(rgb_tuple: tuple, luminance_threshold: float = 0.5) -> tuple: """ Given a background color, determine whether a font should be black or white based on calculated luminance :param color_str: Color string such as hex, RGB, etc :param luminance_threshold: Luminances above this threshold return 'black', below returns 'white' :return: 'black' or 'white' """ c = Color() c.rgb = rgb_tuple lum = c.get_luminance() if lum >= luminance_threshold: return [0, 0, 0, 1] else: return [1, 1, 1, 1]