Skip to content

Instantly share code, notes, and snippets.

@davep
Created September 22, 2022 09:25
Show Gist options
  • Save davep/4b8936e3a1d5da6ba1cdc230f8ca3205 to your computer and use it in GitHub Desktop.
Save davep/4b8936e3a1d5da6ba1cdc230f8ca3205 to your computer and use it in GitHub Desktop.
Function to turn a MS/PC-DOS colour code into a Rich colour style markup string
def rich_dos_colour( color: int ) -> str:
"""Convert a MS/PC-DOS colour attribute to Rich style text.
:param int colour: The MS/PC-DOS colour attribute.
:returns: A Rich BBCode-style markup string for the colour.
:rtype: str
"""
assert 0 <= color <= 0xFF, "DOS colour codes must be 0 to 255 inclusive"
return "[color({}) on color({})]".format(
*reversed( divmod( color, 0x10 ) )
)
@davep
Copy link
Author

davep commented Sep 22, 2022

Turns out, this doesn't quite work because the colour numbers used by Rich don't map to those used by 8bit DOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment