diff --git a/vardbg/assets/styles/default.py b/vardbg/assets/styles/default.py new file mode 100644 index 0000000..3a9db46 --- /dev/null +++ b/vardbg/assets/styles/default.py @@ -0,0 +1,47 @@ +from pygments.style import Style +from pygments.token import ( + Comment, Error, Keyword, Literal, Name, Number, Operator, String, Text +) +from vardbg.output.video_writer.getstyle import get_style_by_name + +scheme = get_style_by_name('wood') + +class DefaultStyle(Style): + background_color = scheme['base00'] + highlight_color = scheme['base02'] + + styles = { + Text: scheme['base05'], + Error: scheme['base08'], # .err + + Comment: scheme['base03'], # .c + Comment.Preproc: scheme['base0f'], # .cp + Comment.PreprocFile: scheme['base0b'], # .cpf + + Keyword: scheme['base0e'], # .k + Keyword.Type: scheme['base08'], # .kt + + Name.Attribute: scheme['base0d'], # .na + Name.Builtin: scheme['base0d'], # .nb + Name.Builtin.Pseudo: scheme['base08'], # .bp + Name.Class: scheme['base0d'], # .nc + Name.Constant: scheme['base09'], # .no + Name.Decorator: scheme['base09'], # .nd + Name.Function: scheme['base0d'], # .nf + Name.Namespace: scheme['base0d'], # .nn + Name.Tag: scheme['base0e'], # .nt + Name.Variable: scheme['base0d'], # .nv + Name.Variable.Instance: scheme['base08'], # .vi + + Number: scheme['base09'], # .m + + Operator: scheme['base0c'], # .o + Operator.Word: scheme['base0e'], # .ow + + Literal: scheme['base0b'], # .l + + String: scheme['base0b'], # .s + String.Interpol: scheme['base0f'], # .si + String.Regex: scheme['base0c'], # .sr + String.Symbol: scheme['base09'], # .ss + } diff --git a/vardbg/assets/styles/wood.py b/vardbg/assets/styles/wood.py new file mode 100644 index 0000000..783e225 --- /dev/null +++ b/vardbg/assets/styles/wood.py @@ -0,0 +1,18 @@ +scheme = { + 'base00' : '#231e18', + 'base01' : '#302b25', + 'base02' : '#48413a', + 'base03' : '#9d8b70', + 'base04' : '#b4a490', + 'base05' : '#cabcb1', + 'base06' : '#d7c8bc', + 'base07' : '#e4d4c8', + 'base08' : '#d35c5c', + 'base09' : '#ca7f32', + 'base0a' : '#e0ac16', + 'base0b' : '#b7ba53', + 'base0c' : '#6eb958', + 'base0d' : '#88a4d3', + 'base0e' : '#bb90e2', + 'base0f' : '#b49368' + } diff --git a/vardbg/output/video_writer/config.py b/vardbg/output/video_writer/config.py index b901f81..b618ee9 100644 --- a/vardbg/output/video_writer/config.py +++ b/vardbg/output/video_writer/config.py @@ -1,15 +1,18 @@ import collections.abc from pathlib import Path +import math import toml from pygments.formatter import Formatter -from pygments.styles.monokai import MonokaiStyle +from vardbg.assets.styles.default import DefaultStyle from pygments.token import Token FILE_PATH = Path(__file__).parent ASSETS_PATH = FILE_PATH / ".." / ".." / "assets" DEFAULT_CFG_PATH = FILE_PATH / "default_config.toml" +# List for colors in color_scheme +color_list = [] # Source: https://stackoverflow.com/a/3233356 def recursive_update(base, new): @@ -58,6 +61,17 @@ def parse_hex_color(string): return r, g, b, 255 +# Some color_scheme can have same txt and bg color +def color_contrast(body_txt): + contrast = (int(body_txt[0] * 299) + int(body_txt[1] * 587) + int(body_txt[2] * 114)) / 1000 + if (contrast >= 128): + return (50,50,50,255) # return grey + else: + return (255,255,255,255) # return white + +# Calculate distance btw two color +def color_similarity(base_col_val,oth_col_val): + return math.sqrt(sum((base_col_val[i]-oth_col_val[i])**2 for i in range(3))) def load_style(style): styles = {} @@ -65,11 +79,9 @@ def load_style(style): for token, params in formatter.style: color = parse_hex_color(params["color"]) if params["color"] else None - # Italic and underline styles aren't supported, so just use bold for them - bold = params["bold"] or params["italic"] or params["underline"] # Save style - styles[token] = {"color": color, "bold": bold} + styles[token] = {"color": color} return styles @@ -106,16 +118,16 @@ def __init__(self, config_path): self.font_heading = (sub_path(fonts["heading"]), fonts["heading_size"]) self.font_intro = (sub_path(fonts["intro"]), fonts["intro_size"]) - style = MonokaiStyle + style = DefaultStyle self.styles = load_style(style) self.bg = parse_hex_color(style.background_color) self.highlight = parse_hex_color(style.highlight_color) self.fg_divider = self.styles[Token.Generic.Subheading]["color"] self.fg_heading = self.styles[Token.Name]["color"] - self.fg_body = self.styles[Token.Text]["color"] + self.fg_body = color_contrast(self.bg) self.fg_watermark = self.styles[Token.Comment]["color"] - + self.red = self.styles[Token.Operator]["color"] self.green = self.styles[Token.Name.Function]["color"] self.blue = self.styles[Token.Keyword]["color"] diff --git a/vardbg/output/video_writer/default_config.toml b/vardbg/output/video_writer/default_config.toml index cd81c5b..4632afe 100644 --- a/vardbg/output/video_writer/default_config.toml +++ b/vardbg/output/video_writer/default_config.toml @@ -45,3 +45,48 @@ heading_size = 24 # Intro font, used to render the intro screen intro = "$ASSETS/fonts/Inter-Bold.ttf" intro_size = 48 + +[theme] +# Color scheme to use +# Available themes: +# - default +# - emacs +# - friendly +# - colorful +# - autumn +# - murphy +# - manni +# - monokai +# - perldoc +# - pastie +# - borland +# - trac +# - native +# - fruity +# - bw +# - vim +# - vs +# - tango +# - rrt +# - xcode +# - igor +# - paraiso-light +# - paraiso-dark +# - lovelace +# - algol +# - algol_nu +# - arduino +# - rainbow_dash +# - abap +# - solarized-dark +# - solarized-light +# - sas +# - stata +# - stata-light +# - stata-dark +color_scheme = "monokai" + +# Maximum distance color can have with other color +max_red_dist = 360 +max_green_dist = 360 +max_blue_dist = 360 diff --git a/vardbg/output/video_writer/getstyle.py b/vardbg/output/video_writer/getstyle.py new file mode 100644 index 0000000..1bcace2 --- /dev/null +++ b/vardbg/output/video_writer/getstyle.py @@ -0,0 +1,55 @@ +""" + vardbg.styles + ~~~~~~~~~~~~~~~ + +""" + +from pygments.util import ClassNotFound + + +STYLE_ENTRY_POINT = 'vardbg.assets.styles' + + +def iter_entry_points(group_name): + try: + import pkg_resources + except (ImportError, IOError): + return [] + + return pkg_resources.iter_entry_points(group_name) + +def find_plugin_styles(): + for entrypoint in iter_entry_points(STYLE_ENTRY_POINT): + yield entrypoint.name, entrypoint.load() + +#: Maps style names to 'submodule::dictname'. +STYLE_MAP = { + 'wood': 'wood::scheme' +} + + +def get_style_by_name(name): + if name in STYLE_MAP: + mod, cls = STYLE_MAP[name].split('::') + builtin = "yes" + else: + for found_name, style in find_plugin_styles(): + if name == found_name: + return style + # perhaps it got dropped into our styles package + builtin = "" + mod = name + cls = name.title() + "Style" + print(mod) + + try: + mod = __import__('vardbg.assets.styles.' + mod, None, None, [cls]) + except ImportError: + raise ClassNotFound("Could not find style module %r" % mod + + (builtin and ", though it should be builtin") + ".") + try: + return getattr(mod, cls) + except AttributeError: + raise ClassNotFound("Could not find style class %r in style module." % cls) + +