Skip to content

Commit

Permalink
Merge pull request #15 from CIAvash/chroma_styles
Browse files Browse the repository at this point in the history
Add style and list-styles options
  • Loading branch information
matsuyoshi30 authored May 9, 2021
2 parents 4dbb84b + 00dbdb4 commit 9801564
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
germanium
!germanium/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ FLAGS:
-b, --background <COLOR> Background color of the image [default: #aaaaff]
-f, --font <FONT> Specify font eg. 'Hack-Bold'
-l, --language <LANG> The language for syntax highlighting eg. 'go'
-s, --style <STYLE> The style for syntax highlighting eg. 'dracula'
--list-styles List all available styles for syntax highlighting
--list-fonts List all available fonts in your system
--no-line-number Hide the line number
--no-window-access-bar Hide the window access bar
-v, --version Show Version
```

### Example
Expand All @@ -39,6 +42,12 @@ Generate image From Stdin (need to add option `-l` or `--language`)
cat main.go | germanium -l go -o main.png -
```

Generate image with another style (you can get a list of them with `--list-styles`)

```
germanium -s solarized-dark -o main.png main.go
```

Generate image without line number

```
Expand Down
4 changes: 2 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- [] upgrade accuracy about drawing circle
- [] shadow blur
- [] padding between line
- [] chroma theme
- [x] chroma theme
- [] clipboard
- [] language
- [x] language
- [] background image
17 changes: 15 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

flags "github.com/jessevdk/go-flags"
"github.com/alecthomas/chroma/styles"
"github.com/matsuyoshi30/germanium"
)

Expand Down Expand Up @@ -46,6 +47,13 @@ func Run() (err error) {
filename = args[0]
}

if opts.ListStyles {
for _, name := range styles.Names() {
fmt.Printf("%s ", name)
}
return nil
}

if opts.ListFonts {
germanium.ListFonts()
return nil
Expand Down Expand Up @@ -99,11 +107,16 @@ func run(r io.Reader, filename string) error {
return err
}

// set default style to dracula
style := `dracula`
if opts.Style != `` {
style = opts.Style
}
image := germanium.NewImage(src, face, opts.NoWindowAccessBar)
if err := image.Draw(opts.BackgroundColor, opts.NoWindowAccessBar); err != nil {
if err := image.Draw(opts.BackgroundColor, style, opts.NoWindowAccessBar); err != nil {
return err
}
if err := image.Label(out, filename, src, opts.Language, face, !opts.NoLineNum); err != nil {
if err := image.Label(out, filename, src, opts.Language, style, face, !opts.NoLineNum); err != nil {
return err
}

Expand Down
2 changes: 2 additions & 0 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type Options struct {
BackgroundColor string `short:"b" long:"background" default:"#aaaaff" description:"Background color of the image"`
Font string `short:"f" long:"font" default:"Hack-Regular" description:"Specify font eg. 'Hack-Bold'"`
Language string `short:"l" long:"language" description:"The language for syntax highlighting"`
Style string `short:"s" long:"style" description:"The style for syntax highlighting"`
ListStyles bool `long:"list-styles" description:"List all available styles for syntax highlighting"`
ListFonts bool `long:"list-fonts" description:"List all available fonts in your system"`
NoLineNum bool `long:"no-line-number" description:"Hide the line number"`
NoWindowAccessBar bool `long:"no-window-access-bar" description:"Hide the window access bar"`
Expand Down
2 changes: 2 additions & 0 deletions cli/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ FLAGS:
-b, --background <COLOR> Background color of the image [default: #aaaaff]
-f, --font <FONT> Specify font eg. 'Hack-Bold'
-l, --language <LANG> The language for syntax highlighting eg. 'go'
-s, --style <STYLE> The style for syntax highlighting eg. 'dracula'
--list-styles List all available styles for syntax highlighting
--list-fonts List all available fonts in your system
--no-line-number Hide the line number
--no-window-access-bar Hide the window access bar
Expand Down
Binary file added cmd/germanium/testdata/broken-style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions cmd/germanium/testdata/broken-style.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# Test a style that has no background color or missing colors for some tokens
../../../germanium -s pygments main.go -o broken-style-gen.png
Binary file added cmd/germanium/testdata/light-style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions cmd/germanium/testdata/light-style.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
# Test a style that has a light background color, and missing colors for some tokens
# The line numbers should be black
../../../germanium -s autumn main.go -o light-style-gen.png
Binary file added cmd/germanium/testdata/style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions cmd/germanium/testdata/style.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../../../germanium -s solarized-dark main.go -o style-gen.png
36 changes: 33 additions & 3 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (f *PNGFormatter) format(w io.Writer, style *chroma.Style, tokens []chroma.
if f.hasLineNum {
f.drawer.Dot.X = left
f.drawer.Dot.Y = y
f.drawer.Src = image.NewUniform(color.White)
f.drawer.Src = image.NewUniform(chooseColorBasedOnContrast())
f.drawer.DrawString(fmt.Sprintf(format, i+1))
}

Expand All @@ -64,8 +64,26 @@ func (f *PNGFormatter) format(w io.Writer, style *chroma.Style, tokens []chroma.

f.drawer.Dot.X = sx
for _, t := range tokens {
s := style.Get(t.Type)
f.drawer.Src = image.NewUniform(color.RGBA{s.Colour.Red(), s.Colour.Green(), s.Colour.Blue(), 255})
var tokenColor color.Color
chromaTokenColor := style.Get(t.Type).Colour
if chromaTokenColor == 0 {
// if token has no color, try to use the color of the Text token
chromaTokenColor = style.Get(chroma.Text).Colour
}
if chromaTokenColor != 0 {
tokenColor = color.RGBA{
chromaTokenColor.Red(),
chromaTokenColor.Green(),
chromaTokenColor.Blue(),
255,
}
} else {
// found no suitable color for token, so use white if background color is close
// to black and use black if background color is close to white
tokenColor = chooseColorBasedOnContrast()
}

f.drawer.Src = image.NewUniform(tokenColor)

for _, c := range t.String() {
if c == '\n' {
Expand All @@ -88,3 +106,15 @@ func (f *PNGFormatter) format(w io.Writer, style *chroma.Style, tokens []chroma.

return png.Encode(w, f.drawer.Dst)
}

// Choose white or black color based on window background color
func chooseColorBasedOnContrast() color.Color {
black := color.Black
white := color.White
colorIndex := color.Palette{black, white}.Index(windowBackgroundColor)
if colorIndex == 0 {
return white
} else {
return black
}
}
35 changes: 22 additions & 13 deletions panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var (

radius = 10

// image color
dracula = color.RGBA{40, 42, 54, 255}
// default window background color
windowBackgroundColor = color.RGBA{40, 42, 54, 255}

// button color
close = color.RGBA{255, 95, 86, 255}
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewPanel(sx, sy, ex, ey int) *Panel {
return &Panel{img: image.NewRGBA(image.Rect(sx, sy, ex, ey))}
}

func (base *Panel) Draw(backgroundColor string, noWindowAccessBar bool) error {
func (base *Panel) Draw(backgroundColor string, style string, noWindowAccessBar bool) error {
bg, err := ParseHexColor(backgroundColor)
if err != nil {
return err
Expand All @@ -84,6 +84,18 @@ func (base *Panel) Draw(backgroundColor string, noWindowAccessBar bool) error {
// base image
base.fillColor(bg)

// use the background color of the Chroma style, if it exists
chromaStyle := styles.Get(style)
chromaBackgroundColor := chromaStyle.Get(chroma.Background).Background
if chromaBackgroundColor != 0 {
windowBackgroundColor = color.RGBA{
R: chromaBackgroundColor.Red(),
G: chromaBackgroundColor.Green(),
B: chromaBackgroundColor.Blue(),
A: 255,
}
}

base.drawWindowPanel(width, height)

// window control bar
Expand All @@ -101,13 +113,13 @@ func (base *Panel) Draw(backgroundColor string, noWindowAccessBar bool) error {

func (p *Panel) drawWindowPanel(w, h int) {
window := NewPanel(paddingWidth, paddingHeight, w-paddingWidth, h-paddingHeight)
window.fillColor(dracula)
window.fillColor(windowBackgroundColor)
draw.Draw(p.img, p.img.Bounds(), window.img, image.Point{0, 0}, draw.Over)
}

func (p *Panel) drawWindowControlPanel(w, h int) {
wc := NewPanel(paddingWidth, paddingHeight, w-paddingWidth, paddingHeight+windowHeight)
wc.fillColor(dracula)
wc.fillColor(windowBackgroundColor)

wc.drawControlButtons()

Expand Down Expand Up @@ -135,7 +147,7 @@ func (p *Panel) drawRound(w, h int) {
image.Point{w - paddingWidth, h - paddingHeight},
}
for _, c := range corners {
round.drawCircle(c, radius, dracula)
round.drawCircle(c, radius, windowBackgroundColor)
}
draw.Draw(p.img, p.img.Bounds(), round.img, image.Point{0, 0}, draw.Over)
}
Expand All @@ -148,7 +160,7 @@ func (p *Panel) drawAroundBar(w, h int) {
NewPanel(paddingWidth, h-paddingHeight, w-paddingWidth, h-paddingHeight+radius),
}
for _, ab := range aroundbars {
ab.fillColor(dracula)
ab.fillColor(windowBackgroundColor)
draw.Draw(p.img, p.img.Bounds(), ab.img, image.Point{0, 0}, draw.Over)
}
}
Expand Down Expand Up @@ -205,7 +217,7 @@ func (p *Panel) drawCircle(center image.Point, radius int, c color.RGBA) {
}

// Label labels highlighted source code on panel
func (p *Panel) Label(out io.Writer, filename, src, language string, face font.Face, hasLineNum bool) error {
func (p *Panel) Label(out io.Writer, filename, src, language string, style string, face font.Face, hasLineNum bool) error {
var lexer chroma.Lexer
if language != "" {
lexer = lexers.Get(language)
Expand All @@ -217,10 +229,7 @@ func (p *Panel) Label(out io.Writer, filename, src, language string, face font.F
}
lexer = chroma.Coalesce(lexer)

style := styles.Get("dracula")
if style == nil {
style = styles.Fallback
}
chromaStyle := styles.Get(style)

iterator, err := lexer.Tokenise(nil, src)
if err != nil {
Expand All @@ -241,7 +250,7 @@ func (p *Panel) Label(out io.Writer, filename, src, language string, face font.F
formatter = formatters.Fallback
}

if err := p.Formatter.Format(out, style, iterator); err != nil {
if err := p.Formatter.Format(out, chromaStyle, iterator); err != nil {
return err
}

Expand Down

0 comments on commit 9801564

Please sign in to comment.