Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Color-Scheme Feature #23

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions vardbg/assets/styles/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pygments.style import Style
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assets directory is for static assets. There should not be any Python code in it.

from pygments.token import (
Comment, Error, Keyword, Literal, Name, Number, Operator, String, Text
)
from vardbg.output.video_writer.getstyle import get_style_by_name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use absolute imports unless required. It makes maintenance harder.


scheme = get_style_by_name('wood')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for a separate "default" file. That can be handled as a simple alias in get_style_by_name.


class DefaultStyle(Style):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be built on-the-fly for any style, not statically defined here.

background_color = scheme['base00']
highlight_color = scheme['base02']

styles = {
Text: scheme['base05'],
Error: scheme['base08'], # .err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these comments for?


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
}
18 changes: 18 additions & 0 deletions vardbg/assets/styles/wood.py
Original file line number Diff line number Diff line change
@@ -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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not match Black's code style, which is what this project uses. Please run the pre-commit hooks to format it.

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusing indentation.

26 changes: 19 additions & 7 deletions vardbg/output/video_writer/config.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use absolute imports unless required. It makes maintenance harder.

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):
Expand Down Expand Up @@ -58,18 +61,27 @@ 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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a color scheme loading problem. No real color scheme will have the same foreground and background colors — how are people supposed to read the text if that's the case?

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)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, You are right, Sorry I forgot to remove this part from the code


def load_style(style):
styles = {}
formatter = Formatter(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

Expand Down Expand Up @@ -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"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Base16 colors should be used directly rather than translating them into a Pygments style and referencing that.

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"]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace; please run the pre-commit hooks to take care of this.

self.red = self.styles[Token.Operator]["color"]
self.green = self.styles[Token.Name.Function]["color"]
self.blue = self.styles[Token.Keyword]["color"]
45 changes: 45 additions & 0 deletions vardbg/output/video_writer/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These schemes no longer exist.

# - 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer necessary.

max_green_dist = 360
max_blue_dist = 360
55 changes: 55 additions & 0 deletions vardbg/output/video_writer/getstyle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
vardbg.styles
~~~~~~~~~~~~~~~
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of this docstring?


"""

from pygments.util import ClassNotFound
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid from imports unless necessary to write idiomatic code.



STYLE_ENTRY_POINT = 'vardbg.assets.styles'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for an absolute package path here.



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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for runtime scheme discovery here. For something simple like this, it's fine to just import everything in __init__.py in styles.

yield entrypoint.name, entrypoint.load()

#: Maps style names to 'submodule::dictname'.
STYLE_MAP = {
'wood': 'wood::scheme'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be hard-coded.

}


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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use an internal Python function here.

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)