Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created November 5, 2018 06:34
Show Gist options
  • Select an option

  • Save OdatNurd/23830bf8166ea0aafb0cd83e37ff019f to your computer and use it in GitHub Desktop.

Select an option

Save OdatNurd/23830bf8166ea0aafb0cd83e37ff019f to your computer and use it in GitHub Desktop.
Generating a Sublime color scheme
import sublime
import sublime_plugin
import os
_scheme_mappings = {
"entity.anchor.text": "string.quoted",
"punctuation.anchor": "string.quoted",
"entity.link.text": "storage",
"punctuation.link": "storage"
}
def _rule_for_selector(view, scope, selector):
data = view.style_for_scope(selector)
rule = {"scope": scope}
rule["foreground"] = data["foreground"]
if "background" in data:
rule["background"] = data["background"]
font_style = ""
if data["bold"]:
font_style += "bold "
if data["italic"]:
font_style += "italic"
if font_style:
rule["font_style"] = font_style.strip()
return rule
def gen_color_scheme_one(view):
"""
Generate a brand new color scheme for HyperHelp based on the
scheme in use in the existing view.
This returns a color scheme which has global rules and just
the scope rules from _scheme_mappings applied.
"""
scheme = {}
scheme["name"] = "HyperHelp Generated Color Scheme"
scheme["author"] = "Vaguely Clever Code"
rules = []
scheme["rules"] = rules
global_colors = view.style()
scheme["globals"] = {k: v for k, v in global_colors.items()
if not k.endswith("ish")}
for scope, selector in _scheme_mappings.items():
rules.append(_rule_for_selector(view, scope, selector))
return sublime.encode_value(scheme, True)
def gen_color_scheme_two(view):
color_scheme = view.settings().get("color_scheme")
res_file = sublime.find_resources(color_scheme)[0]
json_data = sublime.load_resource(res_file)
base_scheme = sublime.decode_value(json_data)
scheme = {}
scheme["name"] = "HyperHelp Generated Color Scheme"
scheme["author"] = "Vaguely Clever Code"
rules = []
scheme["rules"] = rules
for scope, selector in _scheme_mappings.items():
score = 0
match = None
for rule in base_scheme["rules"]:
this_score = sublime.score_selector(selector, rule["scope"])
if this_score > score:
score = this_score
match = rule
print(score, match)
if match is not None:
new_rule = {"scope": scope}
if "foreground" in match:
new_rule["foreground"] = match["foreground"]
if "background" in match:
new_rule["background"] = match["background"]
if "font_style" in match:
new_rule["font_style"] = match["font_style"]
rules.append(new_rule)
return (color_scheme, sublime.encode_value(scheme, True))
def gen_color_scheme_three(view):
color_scheme = os.path.split(view.settings().get("color_scheme"))[1]
try:
res_file = sublime.find_resources(color_scheme)[0]
json_data = sublime.load_resource(res_file)
base_scheme = sublime.decode_value(json_data)
scheme = {}
scheme["name"] = "HyperHelp Generated Color Scheme"
scheme["author"] = "Vaguely Clever Code"
rules = []
scheme["rules"] = rules
for scope, selector in _scheme_mappings.items():
score = 0
match = None
for rule in base_scheme["rules"]:
this_score = sublime.score_selector(selector, rule["scope"])
if this_score > score:
score = this_score
match = rule
print(score, match)
if match is not None:
new_rule = {"scope": scope}
if "foreground" in match:
new_rule["foreground"] = match["foreground"]
if "background" in match:
new_rule["background"] = match["background"]
if "font_style" in match:
new_rule["font_style"] = match["font_style"]
rules.append(new_rule)
return (color_scheme, sublime.encode_value(scheme, True))
except:
color_scheme = os.path.splitext(color_scheme)[0] + ".sublime-color-scheme"
scheme = {}
scheme["name"] = "HyperHelp Generated Color Scheme"
scheme["author"] = "Vaguely Clever Code"
rules = []
scheme["rules"] = rules
# global_colors = view.style()
# scheme["globals"] = {k: v for k, v in global_colors.items()
# if not k.endswith("ish")}
for scope, selector in _scheme_mappings.items():
rules.append(_rule_for_selector(view, scope, selector))
return (color_scheme, sublime.encode_value(scheme, True))
class ExampleCommand(sublime_plugin.WindowCommand):
def run(self):
color_scheme, scheme_data = gen_color_scheme_three(self.window.active_view())
res_file = os.path.join(sublime.packages_path(),
"User",
color_scheme)
with open(res_file, 'w') as handle:
handle.write(scheme_data)
class ExampleTwoCommand(sublime_plugin.WindowCommand):
def run(self):
color_scheme, scheme_data = gen_color_scheme_two(self.window.active_view())
res_file = os.path.join(sublime.packages_path(),
"User",
color_scheme)
with open(res_file, 'w') as handle:
handle.write(scheme_data)
class ExampleOneCommand(sublime_plugin.WindowCommand):
def run(self):
scheme_data = gen_color_scheme_one(self.window.active_view())
res_file = os.path.join(sublime.packages_path(),
"User",
"HyperHelp.sublime-color-scheme")
with open(res_file, 'w') as handle:
handle.write(scheme_data)
@OdatNurd

OdatNurd commented Nov 5, 2018

Copy link
Copy Markdown
Author

This is example code from the November 4th live stream on my personal YouTube channel. The overall topic of the night was programatically generating a color scheme for use in a package, based on an existing color scheme.

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