Browse Source

Added command-line arguments

* The script is now ready for primetime because users can output to
any template they please
pull/1/head
Macoy Madson 2 years ago
parent
commit
352cafc1d7
3 changed files with 45 additions and 17 deletions
  1. +32
    -10
      AutoBase16Theme.py
  2. +1
    -1
      Planning.org
  3. +12
    -6
      ReadMe.org

+ 32
- 10
AutoBase16Theme.py View File

@@ -3,6 +3,8 @@
# https://github.com/makuto/auto-base16-theme
import random
import colorsys
import sys
import argparse

"""
This script generates a "base16" color theme intended for code syntax highlighting.
@@ -26,14 +28,26 @@ Base16 Style (from https://github.com/chriskempson/base16/blob/master/styling.md
base0F - Deprecated, Opening/Closing Embedded Language Tags, e.g. <?php ?>
"""

argParser = argparse.ArgumentParser(
description='This script generates a base16 color theme intended for code syntax highlighting from a source image.')
argParser.add_argument('--inputColorPaletteFile', type=str, dest='inputColorPaletteFile', default='colors.txt',
help='The colors the script will select from will be read in from this file. The file should'
' be a list of hexadecimal color values separated by newlines')
argParser.add_argument('template', type=str,
help='The template which the hex colors will be output to. This template should'
' have 16 curly brace pair (\"{}\") where the 16 colors will be output in order. Due to'
' python formatting, you\'ll need to add another brace to any non-format braces'
' ("{{" will become a single "{")')
argParser.add_argument('outputFile', type=str,
help='Colors will be inserted into outputTemplate then written to this outputFile')
argParser.add_argument('--debugColorsVerbose', action='store_const', const=True, default=False, dest='debugColorsVerbose',
help='Print detailed information about color selection')

"""

Configuration

"""
# TODO: Add other options for templates (take as a command line argument)
outputTemplateFilename = 'emacs-base16-theme-template.el'
outputFilename = 'base16-my-auto-theme.el'

# TODO: Make these into modes that auto-set these constraints
# TODO: Make contrast ratio mode which meets accessibility guidelines (see https://webaim.org/resources/contrastchecker/)?
@@ -219,8 +233,8 @@ Procedure

"""

def main():
colorsFile = open('colors.txt', 'r')
def main(inputColorsFilename, outputTemplateFilename, outputFilename):
colorsFile = open(inputColorsFilename, 'r')
colorsLines = colorsFile.readlines()
colorsFile.close()

@@ -259,13 +273,14 @@ def main():
# base0F - Deprecated, Opening/Closing Embedded Language Tags, e.g. <?php ?>
Base16Color('base0F', pickHighContrastBrightColorUniqueOrRandom)]

# For testing
colorPool = ['#001b8c', '#0a126b', '#010e44', '#772e51', '#ca4733', '#381f4d', '#814174',
'#90142e', '#720d21', '#28217d', '#6d2c88', '#3b0010', '#6e095b', '#827e7b',
'#645361', '#560041']
# The colors we are able to choose from
colorPool = []

if colorsLines:
colorPool = colorsLines
else:
print('Error: Could not parse colors from input colors file {}'.format(inputColorsFilename))
return

# Remove duplicate colors; these throw off the algorithm
colorPool = list(set(colorPool))
@@ -316,4 +331,11 @@ def main():
print('Wrote {} using template {}'.format(outputFilename, outputTemplateFilename))

if __name__ == '__main__':
main()
if len(sys.argv) == 1:
argParser.print_help()
exit()
args = argParser.parse_args()

debugColorsVerbose = args.debugColorsVerbose
main(args.inputColorPaletteFile, args.template, args.outputFile)

+ 1
- 1
Planning.org View File

@@ -6,7 +6,6 @@
**** base08, base0B, base0E should be unique between eachother (diff colors)
**** Always have base08 variables be darker than 0A classes, 0D functions, and 0E keywords?
**** Ensure no other foreground text values are the same as base03 comments?
** TODO Take template to use as a command line argument
** TODO Support different heuristic modes (e.g. light theme, more muted, more contrasty)
** TODO IMPORTANT Make sure things are contrasty relative to line highlight (base01?)
** TODO Figure out how to update base16 theme colors without having to reload emacs (just unset and set in customize-themes?)
@@ -22,3 +21,4 @@
*** blade2.jpg makes it seem like I need some background brightness clamping system
** DONE Fix blade1.jpg backgrounds getting clamped too hard
** DONE Fix DadSuper8Title not having some things show up (it was a duplicate color in the palette)
** DONE Take template to use as a command line argument

+ 12
- 6
ReadMe.org View File

@@ -5,18 +5,24 @@ This script generates a [[https://github.com/chriskempson/base16][base16]] color

This means you can have *beautiful, readable* themes which match your desktop background without having to do any work.

I'm primarily focusing on doing this for Emacs, but it should work for any editor which supports base16.
I currently only have a template for Emacs, but it should work for any editor which supports base16. If you create a template for your format/editor of choice, please feel free to create a pull request adding that template so others can benefit from it.

** Setup
This script relies on [[https://github.com/thefryscorer/schemer2][schemer2]] to choose colors from images. It then takes the output of schemer2 and selects colors which fit the base16 format. Make sure you have Go installed, then install schemer2:
: go get github.com/thefryscorer/schemer2

Additionally, AutoBase16Theme.py requires Python 3.
AutoBase16Theme.py requires Python 3.

** How to Use
Run the following command, replacing "your image" with the image you want the theme's colors to be pulled from:
: schemer2 -format img::colors -in [your image] -out colors.txt && python3 AutoBase16Theme.py
Automating the execution of this so that it updates with your desktop background is left as an exercise for the reader :).
Run the following command, replacing `your image` with the image you want the theme's colors to be pulled from:
: schemer2 -format img::colors -in [your image] -out colors.txt && python3 AutoBase16Theme.py [your template] [your output file]
`schemer2` generates the color palette from the source image and outputs a list of color values to `colors.txt`. We then run AutoBase16Theme.py (which loads `colors.txt` by default). AutoBase16Theme.py selects from these color values (and modifies them, if necessary) such that a good theme is created. It loads `your template` and writes the final theme to `your output file`.
For example, I have an image 'wallpaper.jpg' and I want an [[https://github.com/belak/base16-emacs][Emacs base16]] theme to be generated:
: schemer2 -format img::colors -in wallpaper.jpg -out colors.txt && python3 AutoBase16Theme.py emacs-base16-theme-template.el base16-my-wallpaper-theme.el
I can then copy `base16-my-wallpaper-theme.el` into my Emacs base16 directory and evaluate it, then select it as a theme.
*** Additional Arguments
Note that running
: python3 AutoBase16Theme.py
will output help with argument descriptions.

** Example results
[[./images/Example1.jpg]]


Loading…
Cancel
Save