diff --git a/colour.go b/colour.go index b2417ff..2c77b0e 100644 --- a/colour.go +++ b/colour.go @@ -92,6 +92,14 @@ func (c Colour) Brighten(factor float64) Colour { return NewColour(uint8(r), uint8(g), uint8(b)) } +// BrightenOrDarken brightens a colour if it is < 0.5 brighteness or darkens if > 0.5 brightness. +func (c Colour) BrightenOrDarken(factor float64) Colour { + if c.Brightness() < 0.5 { + return c.Brighten(factor) + } + return c.Brighten(-factor) +} + // Brightness of the colour (roughly) in the range 0.0 to 1.0 func (c Colour) Brightness() float64 { return (float64(c.Red()) + float64(c.Green()) + float64(c.Blue())) / 255.0 / 3.0 diff --git a/formatters/html/html.go b/formatters/html/html.go index 1fbe0c8..d963b28 100644 --- a/formatters/html/html.go +++ b/formatters/html/html.go @@ -99,13 +99,6 @@ func (f *Formatter) Format(w io.Writer, style *chroma.Style, iterator chroma.Ite return f.writeHTML(w, style, iterator.Tokens()) } -func brightenOrDarken(colour chroma.Colour, factor float64) chroma.Colour { - if colour.Brightness() < 0.5 { - return colour.Brighten(factor) - } - return colour.Brighten(-factor) -} - // Ensure that style entries exist for highlighting, etc. func (f *Formatter) restyle(style *chroma.Style) (*chroma.Style, error) { builder := style.Builder() @@ -113,13 +106,13 @@ func (f *Formatter) restyle(style *chroma.Style) (*chroma.Style, error) { // If we don't have a line highlight colour, make one that is 10% brighter/darker than the background. if !style.Has(chroma.LineHighlight) { highlight := chroma.StyleEntry{Background: bg.Background} - highlight.Background = brightenOrDarken(highlight.Background, 0.1) + highlight.Background = highlight.Background.BrightenOrDarken(0.1) builder.AddEntry(chroma.LineHighlight, highlight) } // If we don't have line numbers, use the text colour but 20% brighter/darker if !style.Has(chroma.LineNumbers) { text := chroma.StyleEntry{Colour: bg.Colour} - text.Colour = brightenOrDarken(text.Colour, 0.5) + text.Colour = text.Colour.BrightenOrDarken(0.5) builder.AddEntry(chroma.LineNumbers, text) builder.AddEntry(chroma.LineNumbersTable, text) }