A utility class that supports Tailwind colors with opacity:
ColoredUtility
def ColoredUtility( prefix:str, # The utility prefix (e.g., 'bg', 'text', 'border') color:Union=None, # The color value opacity:Union=None, # Optional opacity value (0-100 or arbitrary)):
Utility class with color and opacity support.
Colored Factory
Factory for creating color-based utilities with convenient API:
A proxy class to handle color family attribute access (e.g., bg.red.500):
ColorFamilyProxy
def ColorFamilyProxy( prefix:str, # The utility prefix color_family:str, # The color family name):
Proxy for accessing color shades via dot notation.
Examples
Test the color system with various use cases:
# Test basic color usagebg = ColoredFactory("bg", "Background color utilities")print(bg.red._500)# Test standard color-shade combinationsassertstr(bg.red._500) =="bg-red-500"assertstr(bg.blue._300) =="bg-blue-300"assertstr(bg.green._950) =="bg-green-950"
bg-red-500
# Test color family proxy accessassertstr(bg.red._500) =="bg-red-500"assertstr(bg.blue._300) =="bg-blue-300"assertstr(bg.slate._950) =="bg-slate-950"# Test with ColorShade enumassertstr(bg.red(ColorShade.SHADE_500)) =="bg-red-500"
# Test special colorsassertstr(bg.transparent) =="bg-transparent"assertstr(bg.black) =="bg-black"assertstr(bg.white) =="bg-white"assertstr(bg.current) =="bg-current"assertstr(bg.inherit) =="bg-inherit"
# Test opacity modifiersassertstr(bg.red._500.opacity(50)) =="bg-red-500/50"assertstr(bg.blue._300.opacity(75)) =="bg-blue-300/75"assertstr(bg.black.opacity(10)) =="bg-black/10"# Test opacity with arbitrary valuesassertstr(bg.red._500.opacity("[0.87]")) =="bg-red-500/[0.87]"
# Test arbitrary color valuesassertstr(bg("#ff0000")) =="bg-[#ff0000]"assertstr(bg("rgb(255, 0, 0)")) =="bg-[rgb(255, 0, 0)]"assertstr(bg("hsl(0, 100%, 50%)")) =="bg-[hsl(0, 100%, 50%)]"
# Test CSS custom propertiesassertstr(bg("--custom-bg")) =="bg-(--custom-bg)"assertstr(bg("--theme-primary")) =="bg-(--theme-primary)"# Test with opacityassertstr(bg("--custom-bg", opacity=50)) =="bg-(--custom-bg)/50"