Dynamic theme facades (Styles, Colors), immutable styling and color models (Style, Color), linear RGBA color interpolation helpers, and extensible theme base classes (BaseColors, BaseStyles, FontBase). Import active tokens via from drawlib.styles import Colors, Style, Styles and architectural base classes via from drawlib.types import BaseColors, BaseStyles, Color, FontBase, Style.
Universal Style & Color Token System — Neutral Baseline, Focal Flat Tokens, .patch() Derivation, and Color InterpolationSource Code
fromdrawlib.canvasimportsave,setupfromdrawlib.iconsimportphosphorfromdrawlib.linesimportlinefromdrawlib.shapesimportrectanglefromdrawlib.stylesimportColors,Styles,get_intermediate_colorsfromdrawlib.textimporttextsetup(width=120,height=58)# Outer boundary containerrectangle((60,29),width=112,height=50,style=Styles.MutedDashed.patch(shape_r=2.5))phosphor.palette((11.5,49.0),width=4.2,style=Styles.PrimaryBold)text((15.0,49.0),"Style & Color Token System (Styles · Colors · .patch())",style=Styles.DarkBold.patch(text_size=12.0,halign="left"),)# Row 1: Semantic & Neutral Token Hierarchy (50%+ neutral baseline + 1 focal hero)rectangle((20.0,35.5),width=24,height=13.5,style=Styles.PrimaryFlat.patch(shape_r=2.0))phosphor.sparkle((11.5,38.0),width=3.4,style=Styles.WhiteBold)text((21.5,38.0),"PrimaryFlat",style=Styles.WhiteBold.patch(text_size=10.5))text((20.0,32.2),"Focal Hero",style=Styles.White.patch(text_size=10.0))rectangle((46.5,35.5),width=24,height=13.5,style=Styles.Neutral.patch(shape_r=2.0))text((46.5,38.0),"Neutral",style=Styles.DarkBold.patch(text_size=10.5))text((46.5,32.2),"Base Card",style=Styles.Dark.patch(text_size=10.0))rectangle((73.5,35.5),width=24,height=13.5,style=Styles.PrimaryNeutral.patch(shape_r=2.0))text((73.5,38.0),"PrimaryNeutral",style=Styles.DarkBold.patch(text_size=10.2))text((73.5,32.2),"Tinted Card",style=Styles.Dark.patch(text_size=10.0))rectangle((100.0,35.5),width=24,height=13.5,style=Styles.PrimaryDashed.patch(shape_r=2.0))text((100.0,38.0),"PrimaryDashed",style=Styles.PrimaryBold.patch(text_size=10.2))text((100.0,32.2),"Zone Outline",style=Styles.Dark.patch(text_size=10.0))# Row 2 Left: Immutable .patch() derivationrectangle((19.5,15.0),width=23,height=13.5,style=Styles.Neutral.patch(shape_r=2.0))text((19.5,17.5),"Style",style=Styles.DarkBold.patch(text_size=10.8))text((19.5,11.8),"Immutable",style=Styles.Dark.patch(text_size=10.0))line((31.5,15.0),(41.0,15.0),arrow_head="->",style=Styles.PrimaryBold)text((36.2,19.2),".patch()",style=Styles.PrimaryBold.patch(text_size=10.0))derived_style=Styles.SecondaryNeutral.patch(shape_line_color=Colors.Primary,shape_line_width=2.2,shape_r=2.5,)rectangle((54.0,15.0),width=25,height=13.5,style=derived_style)text((54.0,17.5),"Derived Style",style=Styles.DarkBold.patch(text_size=10.5))text((54.0,11.8),"r=2.5, w=2.2",style=Styles.Dark.patch(text_size=10.0))# Row 2 Right: Linear RGBA color ramp via get_intermediate_colors()rectangle((91.5,15.0),width=41,height=13.5,style=Styles.Neutral.patch(shape_r=2.0))text((91.5,19.0),"get_intermediate_colors()",style=Styles.DarkBold.patch(text_size=10.0))ramp=get_intermediate_colors(Colors.Blue1,Colors.Blue6,num=3,include_ends=True)foridx,swatchinenumerate(ramp):sx=76.3+idx*7.6swatch_style=Styles.NeutralFlat.patch(shape_fill_color=swatch,shape_line_color=Colors.Gray4,shape_line_width=0.6,shape_r=1.0,)rectangle((sx,11.8),width=6.4,height=4.8,style=swatch_style)save()
Immutable RGBA color model (frozen=True) exported by drawlib.styles, drawlib.types, and drawlib.preset_colors. Color validates 8-bit integer RGB channels (0–255) and floating-point opacity (0.0–1.0), parses 3/4/6/8-digit hexadecimal strings, and supports immutable channel derivation via .patch().
fromdrawlib.stylesimportColor,Colors# Construct from RGBA channels, hex string, or tuplebrand_blue=Color(26,115,232,alpha=1.0)from_hex=Color.from_hex("#1a73e8",alpha=0.85)shorthand=Color("#3498db80")# Derive semi-transparent color without mutating the original tokenbackdrop=Colors.Primary.patch(alpha=0.15)
Active project-wide color palette singleton in drawlib.styles (Colors: DefaultColors). By default, Colors resolves to an instance of DefaultColors, and is automatically replaced whenever a project styles.py (or CLI --styles / -s) defines a custom Colors object.
Immutable universal styling model (frozen=True, extra="forbid") exported by drawlib.styles, drawlib.types, and drawlib.preset_styles. Properties are segregated by target domain (shape_*, line_*, text_*, icon_*, image_*) alongside shared alignment and transform controls so a single Style token can safely configure shapes, connectors, text, icons, and images without attribute collisions.
fromdrawlib.fontsimportFontRobotofromdrawlib.stylesimportColors,Style,Styles# Derive from an existing preset token (recommended)card_style=Styles.Neutral.patch(shape_line_color=Colors.Primary,shape_line_width=2.0,shape_r=2.5,)# Construct a standalone Style from scratchbadge_style=Style(shape_fill_color=Colors.Primary,shape_line_width=0.0,shape_r=3.0,text_color=Colors.White,text_font=FontRoboto.ROBOTO_BOLD,text_size=11.5,)
Active project-wide style catalog singleton in drawlib.styles (Styles: DefaultStyles). Always import uppercase Styles from drawlib.styles rather than hardcoding DefaultStyles in diagram scripts so an entire project can switch themes (DefaultStyles, GoogleStyles, MonochromeStyles, or custom subclasses) via styles.py without code edits.
Generate a list of evenly spaced linear RGBA Color steps between color1 and color2. Useful for custom chart ramps, heatmaps, and multi-frame animation transitions.
fromdrawlib.stylesimportColors,get_intermediate_colors# 3 interior steps between Neutral and Primary -> returns 3 Color objectsinterior=get_intermediate_colors(Colors.Neutral,Colors.Primary,num=3)# Include endpoints -> returns 5 Color objects (color1, 3 interior, color2)full_ramp=get_intermediate_colors(Colors.Blue1,Colors.Blue6,num=3,include_ends=True)
Pydantic-backed base class (drawlib.types.BaseColors) for preset and custom color catalogs. Provides attribute access, dictionary indexing (palette["BrandBlue"]), membership checks, (name, color) iteration, and immutable .patch() derivation.
Pydantic-backed base class (drawlib.types.BaseStyles) for preset and custom theme catalogs (DefaultStyles, GoogleStyles, MonochromeStyles). Encapsulates default canvas dimensions, DPI, background color, source-code font, associated BaseColors palette, and typed Style tokens.
fromdrawlib.fontsimportFontRoboto,FontSourceCodefromdrawlib.preset_stylesimportDefaultStylesfromdrawlib.typesimportBaseStyles,Style# Replace typography across every style token in a theme at onceStyles=DefaultStyles().patch_font(FontRoboto.ROBOTO_REGULAR,bold=FontRoboto.ROBOTO_BOLD,thin=FontRoboto.ROBOTO_THIN,sourcecode=FontSourceCode.ROBOTO_MONO,)
Abstract StrEnum base class (drawlib.types.FontBase and drawlib.fonts.FontBase) inherited by all built-in font enumerations (Font, FontSansSerif, FontSerif, FontMonoSpace, FontRoboto, FontSourceCode, FontJapanese, FontChinese, FontKorean, FontArabic, FontThai, FontBrahmic).