mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-19 21:10:15 +02:00
Expose Colour.BrightenOrDarken (useful for #211).
This commit is contained in:
parent
881a441774
commit
69c9a262c3
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user