Skip to content

Commit 69c9a26

Browse files
committed
Expose Colour.BrightenOrDarken (useful for #211).
1 parent 881a441 commit 69c9a26

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

colour.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ func (c Colour) Brighten(factor float64) Colour {
9292
return NewColour(uint8(r), uint8(g), uint8(b))
9393
}
9494

95+
// BrightenOrDarken brightens a colour if it is < 0.5 brighteness or darkens if > 0.5 brightness.
96+
func (c Colour) BrightenOrDarken(factor float64) Colour {
97+
if c.Brightness() < 0.5 {
98+
return c.Brighten(factor)
99+
}
100+
return c.Brighten(-factor)
101+
}
102+
95103
// Brightness of the colour (roughly) in the range 0.0 to 1.0
96104
func (c Colour) Brightness() float64 {
97105
return (float64(c.Red()) + float64(c.Green()) + float64(c.Blue())) / 255.0 / 3.0

formatters/html/html.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,20 @@ func (f *Formatter) Format(w io.Writer, style *chroma.Style, iterator chroma.Ite
9999
return f.writeHTML(w, style, iterator.Tokens())
100100
}
101101

102-
func brightenOrDarken(colour chroma.Colour, factor float64) chroma.Colour {
103-
if colour.Brightness() < 0.5 {
104-
return colour.Brighten(factor)
105-
}
106-
return colour.Brighten(-factor)
107-
}
108-
109102
// Ensure that style entries exist for highlighting, etc.
110103
func (f *Formatter) restyle(style *chroma.Style) (*chroma.Style, error) {
111104
builder := style.Builder()
112105
bg := builder.Get(chroma.Background)
113106
// If we don't have a line highlight colour, make one that is 10% brighter/darker than the background.
114107
if !style.Has(chroma.LineHighlight) {
115108
highlight := chroma.StyleEntry{Background: bg.Background}
116-
highlight.Background = brightenOrDarken(highlight.Background, 0.1)
109+
highlight.Background = highlight.Background.BrightenOrDarken(0.1)
117110
builder.AddEntry(chroma.LineHighlight, highlight)
118111
}
119112
// If we don't have line numbers, use the text colour but 20% brighter/darker
120113
if !style.Has(chroma.LineNumbers) {
121114
text := chroma.StyleEntry{Colour: bg.Colour}
122-
text.Colour = brightenOrDarken(text.Colour, 0.5)
115+
text.Colour = text.Colour.BrightenOrDarken(0.5)
123116
builder.AddEntry(chroma.LineNumbers, text)
124117
builder.AddEntry(chroma.LineNumbersTable, text)
125118
}

0 commit comments

Comments
 (0)